问题
I'm trying to send some POST data with the .NET WebClient class as following:
WebClient objWebClient = new WebClient();
NameValueCollection objNameValueCollection = new NameValueCollection();
objNameValueCollection.Add("variable1", value1);
objNameValueCollection.Add("variable2", value2);
objNameValueCollection.Add("variable3", value3);
byte[] bytes = objWebClient.UploadValues(objURI, "POST", objNameValueCollection);
MessageBox.Show(Encoding.ASCII.GetString(bytes));
But when I print the POST values in PHP with
var_dump($_POST)
I'll get an empty string.
What I'm doing wrong here? Why are the POST values obviously not submitted to the PHP script?
Thanks in advance for any ideas Andreas
回答1:
I've found the solution myself now and just want to share the result.
It had nothing to do with my code itself. The problem was that I've added a redirect to my Apache configuration to redirect all requests from my domain www.ab-tools.de to www.ab-tools.com.
But the .NET application still posted the data to a script below the old domain.
As a redirect drops all POST data, the script did not get the data from the .NET application.
That was really a stupid mistake - it took me a while till I understood that. ;-)
Best regards and thanks again for all replies Andreas
回答2:
There is nothing wrong on your code... tested it locally:
static void Main(string[] args)
{
WebClient objWebClient = new WebClient();
NameValueCollection objNameValueCollection = new NameValueCollection();
objNameValueCollection.Add("variable1", "test");
objNameValueCollection.Add("variable2", "ast");
objNameValueCollection.Add("variable3", "ost");
byte[] bytes = objWebClient.UploadValues("http://localhost/test.php", "POST", objNameValueCollection);
Console.Write(Encoding.ASCII.GetString(bytes));
Console.WriteLine();
Console.WriteLine("Press any key to exit");
Console.ReadLine();
}
Test file:
<?php
echo "Result:";
print_r($_POST);
?>
Result:
来源:https://stackoverflow.com/questions/5619105/post-values-not-received-in-php-when-submitted-with-net-webclient-class