Why I am not able to send POST request using the following command and some other code similar to it. Where as when I send same request using Mozilla browser’s RESTClient it works fine. This is only for a server’s simulator deployed on LAN/even on my local machine. with Live server it is working fine.
ASCIIEncoding encoding = new ASCIIEncoding();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.220.12:5000"); // this is my system's local ip
byte[] byteArray = encoding.GetBytes("hello");
request.Method = "POST";
request.ContentType = "text/xml";// i tried it with "application/x-www-form-urlencoded" as well
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Flush();
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
Console.ReadKey();
reader.Close();
dataStream.Close();
response.Close();
By testing it on server, I saw there is a message for successful receiving of the request but it does not show the data received from the above code whereas it shows the data from RESTclient.
What could be the possible reason. Is there any firewall the is not allowing to send postdata from .net library to simulator?
This is the same response for un formatted xml, I don’t see anything wrong with the xml (same copy of what I am sending through RESTclient).
It is not clear what content type does the server expects the request to be. If it is
application/x-www-form-urlencoded(key=value pairs) you could simplify your code:If you want to POST XML then:
So as you can see, it will all depend on what does your server side script expects.