POST values not received in PHP when submitted with .NET WebClient class

天大地大妈咪最大 提交于 2020-01-24 19:45:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!