400 Protocol Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta c# webclient 400 bad request Discuss the workings and policies of this site About Us Learn
Http 400 Bad Request Fix
more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us 400 bad request error Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like the request sent by the client was syntactically incorrect. you, helping each other. Join them; it only takes a minute: Sign up Bad Request 400 - protocol error on a valid url - Webclient up vote 1 down vote favorite I am trying to parse this page (http://www.coleparmer.co.uk/Product/Turb_Std_Hach_2100q_Kit/WZ-99900-47) using webclient and am having no luck. var client = new WebClient(); var html = client.DownloadString("http://www.coleparmer.co.uk/Product/Turb_Std_Hach_2100q_Kit/WZ-99900-47"); c# webclient
Http Status 400
share|improve this question asked Mar 25 '14 at 1:13 vishnu 60316 add a comment| 2 Answers 2 active oldest votes up vote 5 down vote accepted The appropriate headers needs to be set. try { string html; using (WebClient client = new WebClient()) { client.Headers.Add("Accept-Language", " en-US"); client.Headers.Add("Accept", " text/html, application/xhtml+xml, */*"); client.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"); html = client.DownloadString("http://www.coleparmer.co.uk/Product/Turb_Std_Hach_2100q_Kit/WZ-99900-47"); } } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null) { var resp = (HttpWebResponse)ex.Response; if (resp.StatusCode == HttpStatusCode.NotFound) // HTTP 404 { //Handle it } } //Handle it } share|improve this answer edited Jan 22 at 9:35 Ayush Shanker 1,446725 answered Mar 25 '14 at 1:34 Charls 1848 This solution works for me. I will mark this as the accepted answer. –vishnu Mar 25 '14 at 2:27 add a comment| up vote 4 down vote It requires a Header. WebClient client = new WebClient(); client.Headers["Content-Type"] = "application/json;charset=UTF-8"; share|improve this answer answered Mar 25 '14 at 1:19
here for a quick overview of the site Help Center Detailed answers to any questions you might 400 bad request ajax have Meta Discuss the workings and policies of this site About protocolerror Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads
The Remote Server Returned An Error: (400) Bad Request.
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million http://stackoverflow.com/questions/22623664/bad-request-400-protocol-error-on-a-valid-url-webclient 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 would be appreciated. Thanks. http://stackoverflow.com/questions/10131423/how-to-fix-400-bad-request-error // 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(); // Get the reponse WebResponse response = request.Get
Server Web App Gallery Microsoft Azure Tools Visual Studio Expression Studio Windows Internet Explorer WebMatrix Web Platform Installer Get Help: Ask a Question in our Forums More Help Resources Blogs Forums HomeLearnTroubleshootChapter 4. Diagnosing HTTP ErrorsTroubleshooting HTTP 400 Errors in IIS Troubleshooting HTTP 400 Errors in IIS http://www.iis.net/learn/troubleshoot/diagnosing-http-errors/troubleshooting-http-400-errors-in-iis By Mike LaingNovember 27, 2012Tools Used in this Troubleshooter: Network Monitor HTTP Error logging This material is provided for informational purposes only. Microsoft makes no warranties, express or implied. Overview After sending an HTTP request to an IIS server, an HTTP client (such as Internet Explorer) may display the following type of error message: The webpage cannot be found. HTTP 400 Most likely causes: There might be a typing error in the address. If you bad request clicked on a link, it may be out of date. What you can try: Retype the address. Go back to the previous page. Go to Bing and look for the information you want. If the HTTP client is Internet Explorer, and the Show Friendly HTTP Error Messages option is turned off, the error may resemble the following: Bad Request In these scenarios, IIS has rejected the client's HTTP request because the request did not meet the 400 bad request server's HTTP parsing rules, or it exceeded time limits, or failed some other rule that IIS or HTTP.sys require incoming requests to adhere to. IIS sends the HTTP 400 - Bad Request status back to the client, and then terminates the TCP connection. Troubleshooting Methods When troubleshooting an HTTP 400 condition, it is important to remember that the underlying problem is that the client has sent a request to IIS that breaks one or more rules that HTTP.sys is enforcing. With that in mind, you will want to see exactly what the client is sending to IIS; to do this, capture a network trace of the client sending the bad request. You can analyze the trace to see the raw data that the client sends to IIS, and to see the raw response data that IIS sends back to the client. You can also use an HTTP sniffer tool called Fiddler; this is a great tool as it allows you to see the HTTP headers even if the client and server are communicating over SSL. The next data item you will want to use is the C:\Windows\System32\LogFiles\HTTPERR\httperr.log file. Beginning in IIS 6.0, the HTTP.sys component handles incoming HTTP requests before they are passed along to IIS, and is the component responsible for blocking requests that don't meet the IIS requirements. When HTTP.sys blocks the request