0

I am new in Programming. I am composing a straightforward application utilizing C# with android in which I need send information to server, information incorporates URL with two parameters. When I attempt to send information to server it just sends the URL however not parameters. Would you be able to help me with respect to this.

var postData = ("id1="+"123456");
postData += ("&id2="+"0123456789");
var request = (HttpWebRequest)WebRequest.Create("http://abc.xyz.com");
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST"
request.ContentType = "multipart/form-data";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
 {
     stream.Write(data, 0, data.Length);
     stream.Close();
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
 response.Close();
  • How are you verifying that information is not transmitting? – Anthony Liriano Apr 18 '18 at 19:24
  • i am using ping command with host and domain name it shows that Ping request could not find the host name. Please check the name and try again. But mine host name is right. – Kamaljit Kaur Apr 18 '18 at 19:28
  • 1
    What you are sending is not `multipart/form-data` but `application/x-www-form-urlencoded`. – Gusman Apr 18 '18 at 19:33
  • See https://stackoverflow.com/questions/14702902/post-form-data-using-httpwebrequest – asherber Apr 18 '18 at 19:38
  • I have tried this as well but it is giving me same response after that i did try multipart/form-data still it's not working for me. – Kamaljit Kaur Apr 18 '18 at 19:43
  • The request is correct if you change the content type, are you sure the server is working? have you tried with some external tool to do the exact same request? Also, what response are you receiving? – Gusman Apr 18 '18 at 19:54
  • yes, i am sure, Server is working because when i use the same address with parameters in the URL then it works fine. I am receiving Your Internet name abc.xyz.com has been activated and gives my ip address. Same does not work by Post in c#. – Kamaljit Kaur Apr 18 '18 at 20:05
  • If it works with query string parameters and a GET, but it doesn't work with a POST, then it sounds like the server is not set up to accept a POST at that URL. – asherber Apr 19 '18 at 00:16
  • Thank you Asherber . server may not be set up to accept a POST at that URL. presently it works fine with Get method. Thank you very much. Now I have one more issue I need to send this request after each 1 minute how might I do this. with the goal that application ought not crash. Thanks in advance. – Kamaljit Kaur Apr 19 '18 at 14:18
  • Hi, i am able to send request again after every two minutes, it works. Thanks every one in advance. But i have one last problem , now i want to stop this request to send to server. Would you please suggest me something about it. I tried with request.Abort() method but after that it takes a lot of time to send request again to server. Thanks in advance. – Kamaljit Kaur Apr 19 '18 at 15:33

1 Answers1

0

You have set the content type to multipart/form-data but the data you are posting is formed as application/x-www-form-urlencoded.

A multipart content should look something like this:

--12345
Content-Disposition: form-data; name="sometext"

some text sent via post...
--12345--

And the content type must contain the boundary:

request.ContentType = "multipart/form-data; boundary=12345";

But you have this type of content:

id1=123456&id2=0123456789

So using application/x-www-form-urlencoded for the content type will solve the problem:

request.ContentType = "application/x-www-form-urlencoded";
Gusman
  • 14,905
  • 2
  • 34
  • 50