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.
3 Answers 3
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
9 Comments
?data=
... why do you want to add this anyway?data
to be null
(also since it's JSON you can always add your desired parameters there instead)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
1 Comment
Your Url should be http://myipaddress/WindowsApp/Registration since you are posting your data in the payload.
?data=
you have to put your parameter/argument-value like?data=42
null
or an empty string is expected here ....