Net Webresponse 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 this site About Us Learn more about Stack Overflow the company webresponse status code Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs
Httpwebrequest Getresponse 400 Bad Request
Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just
Httpwebresponse C#
like you, helping each other. Join them; it only takes a minute: Sign up How to process WebResponse when .NET throws WebException ((400) Bad Request)? up vote 27 down vote favorite 10 I'm using Facebook Graph Api
The Remote Server Returned An Error 400 Bad Request Httpwebrequest
and trying to get user data. I'm sending user access token and in case this token is expired or invalid Facebook returns status code 400 and this response: { "error": { "message": "Error validating access token: The session is invalid because the user logged out.", "type": "OAuthException" } } The problem is that when I use this C# code: try { webResponse = webRequest.GetResponse(); // in case of status code 400 .NET throws WebException c# httpwebrequest 400 bad request here } catch (WebException ex) { } If status code is 400 .NET throws WebException and my webResponse is null after exception is caught so I have no chance to process it. I want to do it to make sure that the problem is in expired token and not somewhere else. Is there a way to do it? Thanks. c# asp.net facebook exception httpwebresponse share|improve this question asked Sep 30 '11 at 10:38 Burjua 3,692135995 add a comment| 2 Answers 2 active oldest votes up vote 55 down vote accepted Using a try/catch block like this and processing the error message appropriately should work fine: var request = (HttpWebRequest)WebRequest.Create(address); try { using (var response = request.GetResponse() as HttpWebResponse) { if (request.HaveResponse && response != null) { using (var reader = new StreamReader(response.GetResponseStream())) { string result = reader.ReadToEnd(); } } } } catch (WebException wex) { if (wex.Response != null) { using (var errorResponse = (HttpWebResponse)wex.Response) { using (var reader = new StreamReader(errorResponse.GetResponseStream())) { string error = reader.ReadToEnd(); //TODO: use JSON.net to parse this string and look at the error message } } } } } However, using the Facebook C# SDK makes this all really easy so that you don't have to process this yourself. share|improve this answer answered Oct 1 '11 at 5:30 bkaid 37.4k1388109 Thanks for detailed answer
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators bad request exception c# Students Microsoft Imagine Microsoft Student Partners ISV Startups c# webexception status code TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference c# webexception response null Dev centers Samples Retired content We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. http://stackoverflow.com/questions/7609194/how-to-process-webresponse-when-net-throws-webexception-400-bad-request System.Net HttpWebResponse Class HttpWebResponse Properties HttpWebResponse Properties StatusCode Property StatusCode Property StatusCode Property CharacterSet Property ContentEncoding Property ContentLength Property ContentType Property Cookies Property Headers Property IsMutuallyAuthenticated Property LastModified Property Method Property ProtocolVersion Property ResponseUri Property Server Property StatusCode Property StatusDescription Property SupportsHeaders https://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.statuscode(v=vs.110).aspx Property 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. HttpWebResponse.StatusCode Property .NET Framework (current version) Other Versions Visual Studio 2010 .NET Framework 4 Silverlight Visual Studio 2008 .NET Framework 3.5 .NET Framework 3.0 .NET Framework 2.0 .NET Framework 1.1 Gets the status of the response.Namespace: System.NetAssembly: System (in System.dll)Syntax C#C++F#VB Copy public virtual HttpStatusCode StatusCode { get; } Property Value Type: System.Net.HttpStatusCodeOne of the HttpStatusCode values.Exceptions Exception Condition ObjectDisposedExceptionThe current instance has been disposed. RemarksThe StatusCode parameter is a number that indicates the status of the HTTP response. The expected values for status are defined in the HttpStatusCode class.ExamplesThe following exam
resources Windows Server 2012 resources https://msdn.microsoft.com/en-us/library/system.net.webresponse(v=vs.110).aspx Programs MSDN subscriptions Overview Benefits Administrators Students https://msdn.microsoft.com/en-us/library/es54hw8e(v=vs.110).aspx Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Samples Retired content We’re sorry. The content you bad request requested has been removed. You’ll be auto redirected in 1 second. .NET Framework Class Library System.Net Namespaces System.Net System.Net WebResponse Class WebResponse Class WebResponse Class AuthenticationManager Class AuthenticationSchemes Enumeration AuthenticationSchemeSelector Delegate Authorization Class BindIPEndPoint Delegate Cookie 400 bad request Class CookieCollection Class CookieContainer Class CookieException Class CredentialCache Class DecompressionMethods Enumeration Dns Class DnsEndPoint Class DnsPermission Class DnsPermissionAttribute Class DownloadDataCompletedEventArgs Class DownloadDataCompletedEventHandler Delegate DownloadProgressChangedEventArgs Class DownloadProgressChangedEventHandler Delegate DownloadStringCompletedEventArgs Class DownloadStringCompletedEventHandler Delegate EndPoint Class EndpointPermission Class FileWebRequest Class FileWebResponse Class FtpStatusCode Enumeration FtpWebRequest Class FtpWebResponse Class GlobalProxySelection Class HttpContinueDelegate Delegate HttpListener Class HttpListener.ExtendedProtectionSelector Delegate HttpListenerBasicIdentity Class HttpListenerContext Class HttpListenerException Class HttpListenerPrefixCollection Class HttpListenerRequest Class HttpListenerResponse Class HttpListenerTimeoutManager Class HttpRequestHeader Enumeration HttpResponseHeader Enumeration HttpStatusCode Enumeration HttpVersion Class HttpWebRequest Class HttpWebResponse Class IAuthenticationModule Interface ICertificatePolicy Interface ICredentialPolicy Interface ICredentials Interface ICredentialsByHost Interface INetworkProgress Interface IPAddress Class IPEndPoint Class IPEndPointCollection Class IPHostEntry Class IUnsafeWebRequestCreate Interface IWebProxy Interface IWebProxyScript Interface IWebRequestCreate Int
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 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 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 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 .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 within the time-out set for the request.TrustFailureA server certificate could not be validated.MessageLengthLimitExceededA message was received that exceeded the specif