Httpwebrequest.getresponse Protocol Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you webexception status protocolerror might have Meta Discuss the workings and policies of this webexception protocol error site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers system.net.webexceptionstatus.protocolerror 500 or 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 protocol error c# of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up HTTPWebRequest.GetResponse() failing with authenticated requests through a transparent proxy up vote 13 down vote favorite 1 We're using the HTTPWebRequest objects to make HTTP requests to our application and we're having a problem when the
Protocol Error 7
request requires authentication and there is a transparent proxy (Squid 3.1.10). string url = "http://www.icode.co.uk/test/auth.php"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Credentials = new NetworkCredential("username", "password"); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); MessageBox.Show(reader.ReadToEnd()); reader.Close(); stream.Close(); response.Close(); Our original code used the WebClient class which exhibited the same problem. The first time this code runs, it displays the result correctly. When the code runs a second time, it fails on the GetResponse() line with: System.Net.WebException was unhandled Message="The server committed a protocol violation. Section=ResponseStatusLine" Source="System" StackTrace: at System.Net.HttpWebRequest.GetResponse() at Dummy.DummyForm.button1_Click(Object sender, EventArgs e) in H:\Trial\Dummy\DummyForm.cs:line 42 at ... On Windows 7, restarting the process causes it to recover and work once, but Server 2003 requires a full reboot. Looking at the network capture, two requests are identical to start with, the initial unauthenticated request is sent and the server replies, but the failing requests sends the 2nd authenticated request in the middle of the initial
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and
Httpwebrequest Getresponse 400 Bad Request
policies of this site About Us Learn more about Stack Overflow the protocol error 400 bad request company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users httpwebrequest getresponse timeout Badges Ask 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 http://stackoverflow.com/questions/10984264/httpwebrequest-getresponse-failing-with-authenticated-requests-through-a-trans a minute: Sign up .Net HttpWebRequest.GetResponse() raises exception when http status code 400 (bad request) is returned up vote 150 down vote favorite 38 I am in a situation where when I get an HTTP 400 code from the server, it is a completely legal way of the server telling me what was wrong with my request (using a message in http://stackoverflow.com/questions/692342/net-httpwebrequest-getresponse-raises-exception-when-http-status-code-400-ba the HTTP response content) However, the .NET HttpWebRequest raises an exception when the status code is 400. How do I handle this? For me a 400 is completely legal, and rather helpful. The HTTP content has some important information but the exception throws me off my path. .net httpwebrequest share|improve this question asked Mar 28 '09 at 6:40 chefsmart 3,18762843 6 I was experiencing the same thing. I submitted a suggestion to the .NET Framework team. Feel free to vote for it: connect.microsoft.com/VisualStudio/feedback/details/575075/… –Jonas Stawski Jul 14 '10 at 17:49 add a comment| 6 Answers 6 active oldest votes up vote 252 down vote accepted It would be nice if there were some way of turning off "throw on non-success code" but if you catch WebException you can at least use the response: using System; using System.IO; using System.Web; using System.Net; public class Test { static void Main() { WebRequest request = WebRequest.Create("http://csharpindepth.com/asd"); try { using (WebResponse response = request.GetResponse()) { Console.WriteLine("Won't get here"); } } catch (WebException e) { using (WebResponse response = e.Response) { HttpWebResponse httpResponse = (HttpWebResponse) re
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups https://msdn.microsoft.com/en-us/library/es54hw8e(v=vs.110).aspx TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Samples Retired content We’re sorry. The content you requested has been https://bytes.com/topic/c-sharp/answers/237705-get-original-error-message-httpwebresponse removed. You’ll be auto redirected in 1 second. Development Guide Network Programming in the .NET Framework Requesting Data Requesting Data Handling Errors Handling Errors Handling protocol error Errors Creating Internet Requests How to: Request a Web Page and Retrieve the Results as a Stream How to: Request Data Using 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 400 bad request 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 .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.NameResolutionFail
help? Post your question and get tips & solutions from a community of 418,558 IT Pros & Developers. It's quick & easy. GET original error message with HttpWebResponse P: n/a Jon Maz Hi All, I am using a WebRequest & WebResponse to GET the contents of an aspx page as a string (code below). It's working fine, except that when the aspx page has an error on it, I want to know precisely what the error was (ie I want to get the error message that would be displayed if you were viewing the aspx page in a brower). Instead I always get this error: The remote server returned an error: (500) Internal Server Error pointing to the line: WebResponse myResponse = myRequest.GetResponse(); Can anyone tell me how I can GET the original error text returned to me? Thanks, JON ===== CODE ===== // Initialize the WebRequest. WebRequest myRequest = WebRequest.Create("http://localhost/PageWithError.aspx"); // Return the response. WebResponse myResponse = myRequest.GetResponse(); // Convert the GET to a string and display it StreamReader sr = new StreamReader(myResponse.GetResponseStream(),System .Text.Encoding.Default); string sBuffer; sBuffer = sr.ReadToEnd(); sr.Close(); txtDisplayHtml.Text = sBuffer; Nov 15 '05 #1 Post Reply Share this Question 2 Replies P: n/a Joerg Jooss Jon Maz wrote: Hi All, I am using a WebRequest & WebResponse to GET the contents of an aspx page as a string (code below). It's working fine, except that when the aspx page has an error on it, I want to know precisely what the error was (ie I want to get the error message that would be displayed if you were viewing the aspx page in a brower). Instead I always get this error: The remote server returned an error: (500) Internal Server Error pointing to the line: WebResponse myResponse = myRequest.GetResponse(); Can anyone tell me how I can GET the original error text returned to me? You mean the actual error page's content? If a WebException is thrown because of a protocol error, its Response property will give you access to actual response received from the web server. Just make sure you check the Status property accordingly: try { // Do WebRequest } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError) { HttpWebResponse response = ex.Response as HttpWebResponse; if (response != null) { // Process response } } } Cheers, -- Joerg Jooss jo*********@gmx.net Nov 15 '05 #2 P: n/a Jon Maz Thanks a lot, Joerg! Nov 15 '05 #3 This discussion thread is closed Start new discussion Replies have been disabled for this discussion. Similar topics Unable to Connect to Remote server Error Odd error in transport connection. Xml error: Namespace Manager or Xslt cont