ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

GetResponseWithoutException

Allow to get the HttpWebResponse event if the request wasn't successful, in order, for example, to know what went wrong

Source

public static HttpWebResponse GetResponseWithoutException(this HttpWebRequest request) 
{ 
    try 
    { 
        return (HttpWebResponse)request.GetResponse(); 
    } 
    catch (WebException ex) 
    { 
        HttpWebResponse response = ex.Response as HttpWebResponse; 
        if (response == null) 
        { 
            throw; 
        } 
        return response; 
    } 
}

Example

var response = request.GetResponseWithoutException();

if (response.StatusCode == HttpStatusCode.OK)
{
    // Request was successful
}
else if (response.StatusCode == HttpStatusCode.NotFound)
{

}

Author: Krimog

Submitted on: 1 mrt. 2013

Language: C#

Type: HttpWebRequest

Views: 4607