0

I have a Windows Form which submits a POST request to a web server. This will return an url.

Something like this when you do a curl to web server:

curl -d 'info={ "EmployeeID": [ "1234567", "7654321" ], "Salary": true, "BonusPercentage": 10}' http://example.com/xyz/php/api/createjob.php

URL returned:

http://example.com/xyz#newjobapi:id=19

Now I would like to replicate the above process on a windows form which when user clicks the button, it will POST the required information from the Windows form to the server.

But i dont get any response upon clicking the button. It just shows an empty messageBox.

Kindly let me how to display the url returned by the server to the user as a pop up window. Thanks.

My C# Code:

private void button8_Click(object sender, EventArgs e)
{
    HttpWebRequest webRequest;
    string requestParams = "\'info={ \"EmployeeID\": [ \"1234567\", \"7654321\" ], \"Salary\": true, \"BonusPercentage\": 10}\'"; 
    byte[] byteArray = Encoding.UTF8.GetBytes(requestParams);


    webRequest = (HttpWebRequest)WebRequest.Create("http://example.com/xyz/php/api/createjob.php");

    webRequest.Method = "POST";
    webRequest.ContentType = "application/json";            
    webRequest.ContentLength = byteArray.Length;

    using (Stream requestStream = webRequest.GetRequestStream())
    {
        requestStream.Write(byteArray, 0, byteArray.Length);
    }

    // Get the response.
    using (WebResponse response = webRequest.GetResponse())
    {
        using (Stream responseStream = response.GetResponseStream())
        {
            StreamReader rdr = new StreamReader(responseStream, Encoding.UTF8);
            string Json = rdr.ReadToEnd(); // response from server
            MessageBox.Show("URL Returned: " + Json);

        }
    }
}
blackfury
  • 675
  • 3
  • 11
  • 22

1 Answers1

1

If I execute this:

curl --trace con -d "info={ 'Blah': 10}" http://example.com/xyz/php/api/createjob.php

This is part of the output:

00a0: 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 ..Content-Type:
00b0: 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 77 77 application/x-ww
00c0: 77 2d 66 6f 72 6d 2d 75 72 6c 65 6e 63 6f 64 65 w-form-urlencode
00d0: 64 0d 0a 0d 0a                                  d....

And that means that this:

webRequest.ContentType = "application/json"; 

Isn't sending data in a way that PHP make sense of it. My guess is PHP is doing something like this $info = $_POST['info'] serverside. It is expecting a form values to be posted.

Instead you should send what curl is sending:

webRequest.ContentType = "application/x-www-form-urlencoded"; 

As that will send a form and populate the $_POST array in PHP.

rene
  • 41,474
  • 78
  • 114
  • 152
  • I still dont get any output. Not sure if the response from server is obtained by the Message Box. My question is, whether the message box is the right tool to show the response from server? – blackfury Aug 15 '17 at 04:07
  • The messagebox shouldn't be the problem, install fiddler: http://www.telerik.com/fiddler and compare what is going on the wire with what curl sends. – rene Aug 15 '17 at 07:12