Export (0) Print
Expand All

WebException.Response Property

Gets the response that the remote host returned.

[Visual Basic]
Public ReadOnly Property Response As WebResponse
[C#]
public WebResponse Response {get;}
[C++]
public: __property WebResponse* get_Response();
[JScript]
public function get Response() : WebResponse;

Property Value

If a response is available from the Internet resource, a WebResponse instance containing the error response from an Internet resource; otherwise, a null reference (Nothing in Visual Basic).

Remarks

Some Internet protocols, such as HTTP, return otherwise valid responses indicating that an error has occurred at the protocol level. When the response to an Internet request indicates an error, WebRequest.GetResponse sets the Status property to WebExceptionStatus.ProtocolError and provides the WebResponse containing the error message in the Response property of the WebException that was thrown. The application can examine the WebResponse to determine the actual error.

Example

[Visual Basic, C#, C++] The following example checks the Status property and prints to the console the StatusCode and StatusDescription of the underlying HttpWebResponse instance.

[Visual Basic] 
Try
'Create a web request for an invalid site. Substitute the "invalid site" strong in the Create call with a invalid name.
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("invalid site"), HttpWebRequest)

'Get the associated response for the above request.
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
myHttpWebResponse.Close()
        Catch e As WebException
Console.WriteLine(e.Message)

 If e.Status = WebExceptionStatus.ProtocolError Then
    Console.WriteLine("Status Code : {0}", CType(e.Response, HttpWebResponse).StatusCode)
    Console.WriteLine("Status Description : {0}", CType(e.Response, HttpWebResponse).StatusDescription)
End If

[C#] 
try {
   // Create a web request for an invalid site. Substitute the "invalid site" strong in the Create call with a invalid name.
     HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("invalid site");

    // Get the associated response for the above request.
     HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
    myHttpWebResponse.Close();
}
catch(WebException e) {
    Console.WriteLine("This program is expected to throw WebException on successful run."+
                        "\n\nException Message :" + e.Message);
    if(e.Status == WebExceptionStatus.ProtocolError) {
        Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
        Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
    }

[C++] 
try {
   // Create a web request for an unknown server (this raises the WebException).
   HttpWebRequest* myHttpWebRequest =
      dynamic_cast<HttpWebRequest*> (WebRequest::Create(S"http://unknown.unknown.com"));

   // Get the associated response for the above request.
   HttpWebResponse* myHttpWebResponse =
      dynamic_cast<HttpWebResponse*> (myHttpWebRequest->GetResponse());
   myHttpWebResponse->Close();
} catch (WebException* e) {
   Console::WriteLine(S"This program is expected to throw WebException on successful run. \n\nException Message : {0}", e->Message);
   if (e->Status == WebExceptionStatus::ProtocolError) {
      Console::WriteLine(S"Status Code : {0}", __box((dynamic_cast<HttpWebResponse*>(e->Response))->StatusCode));
      Console::WriteLine(S"Status Description : {0}",
         (dynamic_cast<HttpWebResponse*>(e->Response))->StatusDescription);
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

WebException Class | WebException Members | System.Net Namespace

Show:
© 2015 Microsoft