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.

TehBoyan
6,9063 gold badges48 silver badges65 bronze badges
asked Apr 30, 2015 at 10:57
8
  • do you expect a parameter to be in the query string? Is that why you have data= in there? Commented Apr 30, 2015 at 10:59
  • usually after ?data= you have to put your parameter/argument-value like ?data=42 Commented Apr 30, 2015 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? Commented Apr 30, 2015 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 Commented Apr 30, 2015 at 11:03
  • well you did not give the parameter so yeah null or an empty string is expected here .... Commented Apr 30, 2015 at 11:04

3 Answers 3

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
answered Apr 30, 2015 at 11:08

9 Comments

ok but will you please show me a demo code for this? Because I guess I am lacking enough in this concept.
you said yourself, that it works if you don't add the ?data= ... why do you want to add this anyway?
Because actually in future my server is gonna use this type of implementation where it will be having multiple parameters.
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.
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)
|
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
answered Apr 30, 2015 at 11:08

1 Comment

But the answer should be 200/OK, she server should not send back anything esle. There is a difference between you can and you should :D
0

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

answered Apr 30, 2015 at 11:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.