Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
try
           {

               string url = "http://xyz.co.in:8080/register";

               string postdata = "uid" + uid + "userid" + userid + "password" + password + "emailid" + emailid + "isotemplate"+ isotemplate + "mobileno" + mobileno +
                                  "deviceserialid" + deviceserialid + "compmacaddress" + compmacaddress + "kioskid" + kioskid + "kioskhandler" + kioskhandler + "devicename"
                                  + devicename + "devicemodel" + devicemodel;
               byte[] data = Encoding.UTF8.GetBytes(postdata);

               HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

               request.KeepAlive = false;
               request.ProtocolVersion = HttpVersion.Version10;
               request.Method = "POST";
               // turn our request string into a byte stream
               byte[] postBytes = Encoding.UTF8.GetBytes(postdata);

               // this is important - make sure you specify type this way
               request.ContentType = "application/json; charset=UTF-8";
               request.Accept = "application/json";
               request.ContentLength = postBytes.Length;
               //request.CookieContainer = Cookies;
               //request.UserAgent = currentUserAgent;
               Stream requestStream = request.GetRequestStream();

               // now send it
               requestStream.Write(postBytes, 0, postBytes.Length);
               requestStream.Close();

               // grab te response and print it out to the console along with the status code
               HttpWebResponse response = (HttpWebResponse)request.GetResponse();
               string result;
               using (StreamReader rdr = new StreamReader(response.GetResponseStream()))
               {
                   result = rdr.ReadToEnd();
               }
C#



What I have tried:

remote server bad error 400 message
Posted
Updated 6-Mar-19 5:57am
Comments
Graeme_Grant 31-Oct-17 8:32am    
You're capturing the WebException and not just the default Exception class? What does the InnerException say?

1 solution

Try setting your postdata to be a string like "uid=" + uid + "&userid=" + userid + "&password=" + password etc
i.e. '&' between different fields and '=' between the field name and value.

Alternatively use FormUrlEncodedContent

Something like
Dim params = New Dictionary(Of String, String)
params.Add("uid", uid)
params.Add("userid", userid)
params.Add("password", password)
....

Dim postdata= New FormUrlEncodedContent(params)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900