1

I am sending my JSON string to this url http://myipaddress/WindowsApp/Registration?data=

I am using the following code which is as follows :

internal static async Task<String> getHttpResponse(HttpWebRequest request,string postData)
{
    String received = null;

    byte[] requestBody = Encoding.UTF8.GetBytes(postData);

    using(var postStream=await request.GetRequestStreamAsync())
    {
        await postStream.WriteAsync(requestBody, 0, requestBody.Length);
    }

    try
    {
        var response = (HttpWebResponse)await request.GetResponseAsync();
        if(response != null)
        {
            var reader = new StreamReader(response.GetResponseStream());
            received = await reader.ReadToEndAsync();
        }
    }
    catch(WebException ae)
    {
        var reader = new StreamReader(ae.Response.GetResponseStream());
        string responseString = reader.ToString();
        Debug.WriteLine("################ EXCEPTIONAL RESPONSE STRING ################");
        Debug.WriteLine(responseString);
        return responseString;
    }
    return received;
}

and I am calling this method when I click on one of my buttons in XAML as follows :

 HttpWebRequest request = HttpWebRequest.Create(Classes.Constants.SERVER_URL) as HttpWebRequest;

request.Method = "POST";
request.ContentType = "application/json";
postData = JsonConvert.SerializeObject(user);                
string receivedString = await getHttpResponse(request, postData);
Debug.WriteLine("############# RECEIVED STRING #############");
Debug.WriteLine(receivedString);

So, the problem I am facing is that I am unable to get the string on the server.

Note : I am able to get the json string when my server implements its method with a url : http://myipaddress/WindowsApp/Registration (without parameter "?data=") and also sends me response string. But fails when the term "?data=" is implemented and used in the server url.

So what am I going wrong in my code? Please help.

TheBoyan
  • 6,802
  • 3
  • 45
  • 61
  • do you expect a parameter to be in the query string? Is that why you have data= in there? – TheBoyan Apr 30 '15 at 10:59
  • usually after `?data=` you have to put your parameter/argument-value like `?data=42` – Random Dev Apr 30 '15 at 11:01
  • btw: have you tried using the debugger / some logging on your server-site to see if you get an valid request and how your server responses? What if you try 3rd party tools like *Postman*? – Random Dev Apr 30 '15 at 11:03
  • Yes. I am able to hit the server but my server gets null value. And my response string in c# code receives nothing. So what is the procedure of sending the json string with this parameter – Vinod Kulkarni Apr 30 '15 at 11:03
  • well you did not give the parameter so yeah `null` or an empty string is expected here .... – Random Dev Apr 30 '15 at 11:04
  • Your post url should be : http://myipaddress/WindowsApp/Registration as well – Hussein Zawawi Apr 30 '15 at 11:04
  • @CarstenKönig my server responds correctly when the url is http://myipaddress/WindowsApp/Registration and parses the json string very well enough. But in case of parameter implementation of server code which is in java , it takes json string like "request.getParameter('data');" and it also decodes the json string – Vinod Kulkarni Apr 30 '15 at 11:07
  • so you sent your "json string" in the body? – Random Dev Apr 30 '15 at 11:08

3 Answers3

2

So, from what I see in the code you posted, from client side perspective(since we don't see the server side code)you are sending a request to the server in the body of the request.

There are two ways to POST: one way in the body of the request, the other one in the query string.

Seems to me that you are mixing the two.

When you do a POST request to your server to the address without the ?data= then you send the request in the body.

Solutions:

  • If you want to POST in the body of the request, POST to the address without the ?data= parameter in the query string

  • If you want to send it trough the query string, you need to add the value after the ?data=

something like:

http://myipaddress/WindowsApp/Registration?data=MyValue
TheBoyan
  • 6,802
  • 3
  • 45
  • 61
  • ok but will you please show me a demo code for this? Because I guess I am lacking enough in this concept. – Vinod Kulkarni Apr 30 '15 at 11:13
  • 1
    you said yourself, that it works if you don't add the `?data=` ... why do you want to add this anyway? – Random Dev Apr 30 '15 at 11:14
  • Because actually in future my server is gonna use this type of implementation where it will be having multiple parameters. – Vinod Kulkarni Apr 30 '15 at 11:16
  • Exactly @CarstenKönig is right, but it might also be just a workaround. It seems that you need to study the server side in a little more detail, and what it expects. You might get by to POST your Json to it, but it might expect some sort of ?data= parameter as well, that you might need to pass in as a query string. I don't know since I don't know the server side specs. – TheBoyan Apr 30 '15 at 11:17
  • 1
    if it is in "the future" it makes no sense to add an incomplete query right now (don't you agree) - most likely your web-framework will first look at the query-strings and therefore assume your `data` to be `null` (also since it's JSON you can always add your desired parameters there instead) – Random Dev Apr 30 '15 at 11:19
  • I agree. I was sending my request in body and not as a query string. Thanks a lot for help. – Vinod Kulkarni Apr 30 '15 at 11:23
  • But will anybody tell me how to send json string as a query string ? – Vinod Kulkarni Apr 30 '15 at 12:16
  • @VinodKulkarni http://stackoverflow.com/questions/15872658/standardized-way-to-serialize-json-to-query-string – TheBoyan Apr 30 '15 at 12:41
  • No. Not this one. I am expecting whether I can do some changes in my above posted code. – Vinod Kulkarni Apr 30 '15 at 12:48
0

If I have understood everything correctly, you are trying to send a POST request to the server and to get a response from the server?

The POST request methods documentation says, that POST request must be used to submit data, not to get a response.

Note that query strings (name/value pairs) is sent in the HTTP message body of a POST request like this:

POST /test/demo_form.asp HTTP/1.1
Host: w3schools.com
name1=value1&name2=value2
0

Your Url should be http://myipaddress/WindowsApp/Registration since you are posting your data in the payload.

Hussein Zawawi
  • 2,907
  • 2
  • 26
  • 44