views:

45

answers:

0

Hi,

I have a problem with sending a POST request from C# to a PHP script on my Apache (Windows) server. Everything works fine, unless I instruct Apache to require a valid user using BASIC authentication through a .htaccess file.

But let's put things in order. I'm using the following code to send a list of key/value pairs to my PHP script using HTTP-POST:

NameValueCollection nameValues = new NameValueCollection();
nameValues["operation"] = ...;
nameValues["order"] = ...;
nameValues["status"] = ...;
nameValues["comment"] = ...;
nameValues["user"] = ...;

WebClient webClient = new WebClient();
try
{
    CredentialCache credentialCache = new CredentialCache();
    credentialCache.Add(new Uri(PERFORM_URL), "Basic", new NetworkCredential(PERFORM_USER, PERFORM_PASSWORD));

    webClient.Credentials = credentialCache;
    byte[] response = webClient.UploadValues(PERFORM_URL, "POST", nameValues);
    string responseString = Encoding.ASCII.GetString(response);
}
finally
{
    webClient.Dispose();
}

The PHP script on the server looks like this:

<?php

print_r($_SERVER);

?>

So I just output some header information here. I turn off basic authentication in my htaccess file, everything's fine. When I turn it on, however, I get the following output:

Array
(
    ...
    [REMOTE_USER] => ****
    [AUTH_TYPE] => Basic
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => operation=updatestatus&order=185&status=17&comment=Test&user=somenamePOST
    [QUERY_STRING] => 
    ...
    [PHP_AUTH_USER] => ****
    [PHP_AUTH_PW] => ****
    [REQUEST_TIME] => 1286889387
    [argv] => Array
        (
        )

    [argc] => 0
)

Where, the REQUEST_METHOD contains all the parameters + the appended POST. This is wrong. What can I do about it? I've also read and tried solutions, where people used HttpWebRequest using the PreAuthenticate property - but that didn't work for me either.