Httpwebrequest Protocol Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might webexception status protocolerror have Meta Discuss the workings and policies of this site About
Webexception Protocol Error
Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting
System.net.webexceptionstatus.protocolerror 500
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 of 4.7 million
Protocol Error 400 Bad Request
programmers, just like you, helping each other. Join them; it only takes a minute: Sign up ProtocolError while calling HttpWebRequest.GetResponse() up vote 4 down vote favorite 1 I have a page containing links to some files. I basically need to access the source of the page for parsing it then and obtaining all the hyperlinks c# protocolerror to the files. My code is something like this (some piece of code I've found in many places on the net ..): "private static byte[] ReadImageFromUrl(string url) { var myReq = (HttpWebRequest)WebRequest.Create(url); myReq.Timeout = 10000; WebResponse myResp = myReq.GetResponse(); Stream stream = myResp.GetResponseStream(); List
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 this site About Us Learn more about Stack Overflow protocol error 7 the company Business Learn more about hiring developers or posting ads with us Stack Overflow "a webexception with status protocolerror was thrown" Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 webexception status code c# million programmers, just like 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 1 http://stackoverflow.com/questions/7474077/protocolerror-while-calling-httpwebrequest-getresponse 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 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", " http://stackoverflow.com/questions/22623664/bad-request-400-protocol-error-on-a-valid-url-webclient 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,456725 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 maxspan 1,8691527 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged c# webclient or ask your own question. asked 2 years ago viewed 4900 times active 8 months ago Blog Stack Overflow Podcast #91 - Can You S
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and https://msdn.microsoft.com/en-us/library/es54hw8e(v=vs.110).aspx reference Dev centers Samples Retired content We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. Development Guide Network Programming in the .NET Framework Requesting http://blog.strictly-software.com/2009/11/possible-fixes-for-protocol-violation.html Data Requesting Data Handling Errors Handling Errors Handling Errors Creating Internet Requests How to: Request a Web Page and Retrieve the Results as a Stream How to: Request Data Using protocol error the WebRequest Class How to: Send Data Using the WebRequest Class How to: Retrieve a Protocol-Specific WebResponse that Matches a WebRequest Using Streams on the Network Making Asynchronous Requests Handling Errors TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. Handling Errors httpwebrequest protocol error .NET Framework (current version) Other Versions Visual Studio 2010 .NET Framework 4 .NET Framework 3.5 .NET Framework 3.0 .NET Framework 2.0 .NET Framework 1.1 The WebRequest and WebResponse classes throw both system exceptions (such as ArgumentException) and Web-specific exceptions (which are WebExceptions thrown by the GetResponse method).Each WebException includes a Status property that contains a value from the WebExceptionStatus enumeration. You can examine the Status property to determine the error that occurred and take the proper steps to resolve the error.The following table describes the possible values for the Status property.StatusDescriptionConnectFailureThe remote service could not be contacted at the transport level.ConnectionClosedThe connection was closed prematurely.KeepAliveFailureThe server closed a connection made with the Keep-alive header set.NameResolutionFailureThe name service could not resolve the host name.ProtocolErrorThe response received from the server was complete but indicated an error at the protocol level.ReceiveFailureA complete response was not received from the remote server.RequestCanceledThe request was canceled.SecureChannelFailureAn error occurred in a secure channel link.SendFailureA complete request could not be sent to the remote server.ServerProtocolViolationThe server response was not a valid HTTP response.SuccessNo error was encountered.TimeoutNo response was received with
returning a protocol violation errorI have just been working on my robot which is written in C# and I was trying to resolve some issues with redirects on certain pages which the bot wasn't following. The default setting for the HttpWebRequest object is to follow up to 50 redirects however you can override the default settings with the following properties:HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
request.AllowAutoRedirect = true;
request.MaximumAutomaticRedirections = 5;However I was having some issue with a link to an article that was going to an advert page first and then redirecting afterwards. I was trying to come up with a solution to bypass this page but running a test console app from my laptop on the URL with my HttpRequest wrapper class was only returning the following error:The server committed a protocol violation. Section=ResponseStatusLineHowever when I tried to access the same URL from a webpage the error wasn't being raised and the HTTP response was being returned with a 200 status code.A quick look on the web and I found that this problem can be caused by invalid headers in the response such as extra carriage returns or incomplete or invalid headers. To get round the problem of invalid headers the following code can be added to the app.config file.
// set user-agent to the same agent as the current browser
string useragent = Request.ServerVariables["HTTP_USER_AGENT"].ToString();
// use my HTTP request object to make requests
HTTPRequest webReq = new HTTPRequest(url, proxy, useragent);However when I was running my test console application I was using a default user-agent which I have made up myself e.g:Mozilla/4.0 (compatible; RobsRobot 1.3; www.strictly-software.com;)I changed this default agent to IE 6.0 e.g:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)and then I was able to retrieve a valid response from the remote server. However on the second response I got the error again. Changing the agent to IE 7.0 once again allowed me to retrieve a response but only for one attempt. So I am wondering whe