System.Net.HttpWebRequest.GetResponse Method

Returns a response from an Internet resource.

Syntax

public override WebResponse GetResponse ()

Returns

A System.Net.WebResponse that contains the response from the Internet resource.

Exceptions

TypeReason
System.Net.ProtocolViolationExceptionThe HttpWebRequest.ContentLength property of the current instance is not set.
System.Net.WebException

HttpWebRequest.Abort was previously called.

-or-

The timeout period for the request expired.

-or-

An error occurred while processing the request.

Remarks

The HttpWebRequest.GetResponse method returns a System.Net.WebResponse object that contains the response from the Internet resource. The actual instance returned is an System.Net.HttpWebResponse, and can be typecast to that class to access HTTP-specific properties.

A System.Net.ProtocolViolationException is thrown in several cases when the properties set on the System.Net.HttpWebRequest class are conflicting. This exception occurs if an application sets the HttpWebRequest.ContentLength property and the HttpWebRequest.SendChunked property to true, and then sends an HTTP GET request. This exception occurs if an application tries to send chunked to a server that only supports HTTP 1.0 protocol, where this is not supported. This exception occurs if an application tries to send data without setting the HttpWebRequest.ContentLength property or the HttpWebRequest.SendChunked is false when buffering is disabled and on a keepalive connection (the HttpWebRequest.KeepAlive property is true).

Note:

You must call the HttpWebResponse.Close method to close the stream and release the connection. Failure to do so may cause your application to run out of connections.

When using the POST method, you must get the request stream, write the data to be posted, and close the stream. This method blocks waiting for content to post; if there is no time-out set and you do not provide content, the calling thread blocks indefinitely.

Note:

Multiple calls to HttpWebRequest.GetResponse return the same response object; the request is not reissued.

Note:

Your application cannot mix synchronous and asynchronous methods for a particular request. If you call the HttpWebRequest.GetRequestStream method, you must use the HttpWebRequest.GetResponse method to retrieve the response.

Note:

If a System.Net.WebException is thrown, use the WebException.Response and WebException.Status properties of the exception to determine the response from the server.

Note:

This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].

Note:

For security reasons, cookies are disabled by default. If you wish to use cookies, use the HttpWebRequest.CookieContainer property to enable cookies.

Requirements

Namespace: System.Net
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0