Bad Request Protocol Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of c# webclient 400 bad request this site About Us Learn more about Stack Overflow the company Business the remote server returned an error 400 bad request httpwebrequest Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask
The Remote Server Returned An Error 400 Bad Request At System Net Httpwebrequest Getresponse
Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign
The Remote Server Returned An Error (400) Bad Request Gcm
up C# HttpWebRequest / Response gives 400 Bad Request. But not with Firefox HTTP Resource Test up vote 1 down vote favorite I need to make a POST request with an xml data. String xml = ""; byte[] data = System.Text.Encoding.UTF8.GetBytes(xml); HttpClient.post(url, data, "text/xml") Then I call the POST function: public static String post(String url, byte[] data, String contentType){ String body = ""; protocolerror body = getResponse("POST", url, data, contentType, 80); return body; } Now I call this function to make the request / get the response: public static String getResponse(String method, String url, byte[] data, String contentType, int serverPort) { String result = null; HttpWebRequest request = sendRequest(method, url, data, contentType, serverPort); HttpWebResponse response = null; try { response = (HttpWebResponse)request.GetResponse(); if (response != null){ // Get the stream associated with the response Stream receiveStream = response.GetResponseStream (); // Pipes the stream to a higher level stream reader StreamReader readStream = new StreamReader (receiveStream, System.Text.Encoding.UTF8); result = readStream.ReadToEnd (); } } catch(WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError){ throw new HttpClientException("HTTP response error. ", (int)(((HttpWebResponse)ex.Response).StatusCode), ((HttpWebResponse)ex.Response).StatusDescription); } else{ throw new HttpClientException("HTTP response error with status: " + ex.Status.ToString()); } } } and public static HttpWebRequest sendRequest(String method, String url, byte[] data, String contentType, int serverPort){ HttpWebRequest request = null; try { UriBuilder requestUri = new UriBuilder(url); requestUri.Port = serverPort; request = (HttpWebRequest)WebRequest.Create(requestUri.Uri); request.Method = method; // if ((method == "POST") && (data != null) && (data.Length > 0)){ request.ContentLength = data.Length; request.ContentType = ((String.IsNullOrEmpty(contentType))?"application/x-www-form-urlencoded":contentType); Stream dataStream = request.GetRequestStream ()
here for a quick overview of the site Help Center Detailed answers to any questions you
Webclient Post
might have Meta Discuss the workings and policies of this json validator site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or postman posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community http://stackoverflow.com/questions/7395852/c-sharp-httpwebrequest-response-gives-400-bad-request-but-not-with-firefox-ht of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to fix 400 Bad Request error? up vote 1 down vote favorite 2 I'm getting a The remote server returned an error: (400) Bad Request error when trying to run my code. Any help http://stackoverflow.com/questions/10131423/how-to-fix-400-bad-request-error would be appreciated. Thanks. // Open request and set post data HttpWebRequest request = (HttpWebRequest)WebRequest.Create("myurl.com/restservice/Login"); request.Method = "POST"; request.ContentType = "application/json; charset:utf-8"; string postData = "{ \"username\": \"testname\" },{ \"password\": \"testpass\" }"; // Write postData to request url using (Stream s = request.GetRequestStream()) { using (StreamWriter sw = new StreamWriter(s)) sw.Write(postData); } // Get response and read it using (Stream s = request.GetResponse().GetResponseStream()) // error happens here { using (StreamReader sr = new StreamReader(s)) { var jsonData = sr.ReadToEnd(); } } JSON EDIT Changed to: { \"username\": \"jeff\", \"password\": \"welcome\" } But still not working. EDIT This is what I found that works: // Open request and set post data HttpWebRequest request = (HttpWebRequest)WebRequest.Create("myurl.com/restservice/Login"); request.Method = "POST"; request.ContentType = "application/json"; string postData = "{ \"username\": \"testname\", \"password\": \"testpass\" }"; // Set postData to byte type and set content length byte[] postBytes = System.Text.UTF8Encoding.UTF8.GetBytes(postData); request.ContentLength = postBytes.Length; // Write postBytes to request stream Stream s = request.GetRequestStream(); s.Write(postBytes, 0, postBytes.Length); s.Close()
protocol completely. So the Web server was unable to understand the request and process it. It almost always means bad programming of the client http://www.checkupdown.com/status/E400.html system and/or the Web server. Fixing 400 errors - general There is a low-level problem in the client or the Web server or both. 95% of the time this is because of a problem on the client system e.g. there is something unstable on your PC running the Web browser. Is your PC secure ?. If your PC is not well-protected, then all kinds bad request of problems may occur - including HTTP 400 errors. If you run Windows, stay uptodate with automatic security updates from Microsoft and possibly consider getting a registry cleaner. Always have good anti-virus and spyware protection. Invest in a hardware firewall if you can afford one. Be sensible surfing the Web - block pop-up windows and avoid bad sites. If your PC security is compromised, 400 bad request then Web traffic out from your PC to the Internet may be secretly corrupted by malware (spyware, viruses, etc.) running on your PC. This can be difficult for you to detect. Have you installed web-based software ?. Some social networking and games sites ask you to download and run software on your PC so you can interact with other people on the Internet directly (without using your Web browser). This software, if badly written or even criminal, can corrupt all HTTP traffic from your PC. Getting rid of that defective software can be difficult. At worst you may have to reinstall your operating system again (possibly losing all your personal data on your PC if you do not have backup). How stable is your Internet connection ?. If you have recently changed ISPs or your ISP is very slow or unreliable, then Web traffic from your PC out to any site on the Internet may be corrupt. Your ISP may have reconfigured some of their setup (e.g. introduced new proxy servers or cacheing) that is causing some instability. A possible sign of problems here is if you can not easily bro