2

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

5
  • Does the type NameValueCollection work directly in php or is there an equivalent type. Commented Apr 11, 2011 at 11:55
  • Why do you think there is a NameValueCollection in PHP? Commented Apr 11, 2011 at 12:09
  • am not aware of types in php.generally when a .net type is sent to a different client (e.g. java)its better off to send them in form that the client understands. Commented Apr 11, 2011 at 12:15
  • Sorry, but we're talking about simple HTTP POST requests here. Commented Apr 11, 2011 at 12:58
  • Unique difference from my code is the NameValueCollection values... Have you tried to put some static text and or in debug verifying of value1, value2 and value3? Commented Apr 12, 2011 at 9:57

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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: enter image description here

1 Comment

Hello Achilleterzo, thank you very much for your fast reply. I really don't understand why that's not working, too... :-( But when I'll run that, I only get back Array{} as result of the PHP var_dump() function. But what could be the problem here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.