Returns a response from an Internet resource.
Namespace:
System.Net
Assembly:
System (in System.dll)
'Usage
Dim instance As HttpWebRequest
Dim returnValue As WebResponse
returnValue = instance.GetResponse()
'Declaration
Public Overrides Function GetResponse As WebResponse
The GetResponse method returns a WebResponse object that contains the response from the Internet resource. The actual instance returned is an HttpWebResponse, and can be typecast to that class to access HTTP-specific properties.
A ProtocolViolationException is thrown in several cases when the properties set on the HttpWebRequest class are conflicting. This exception occurs if an application sets the ContentLength property and the 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 ContentLength property or the SendChunked is false when buffering is disabled and on a keepalive connection (the KeepAlive property is true).
Caution: |
|---|
You must call the 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 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 GetRequestStream method, you must use the GetResponse method to retrieve the response. |
Note: |
|---|
If a WebException is thrown, use the Response and 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 Network Tracing. |
Note: |
|---|
For security reasons, cookies are disabled by default. If you wish to use cookies, use the CookieContainer property to enable cookies. |
The following code example gets the response for a request.
Imports System
Imports System.Net
Imports System.Text
Imports System.IO
Public Class Test
' Specify the URL to receive the request.
Public Shared Sub Main(ByVal args() As String)
Dim request As HttpWebRequest = CType(WebRequest.Create(args(0)), HttpWebRequest)
' Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4
request.MaximumResponseHeadersLength = 4
' Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
Console.WriteLine("Content length is {0}", response.ContentLength)
Console.WriteLine("Content type is {0}", response.ContentType)
' Get the stream associated with the response.
Dim receiveStream As Stream = response.GetResponseStream()
' Pipes the stream to a higher level stream reader with the required encoding format.
Dim readStream As New StreamReader(receiveStream, Encoding.UTF8)
Console.WriteLine("Response stream received.")
Console.WriteLine(readStream.ReadToEnd())
response.Close()
readStream.Close()
End Sub 'Main
End Class 'Test
'
'The output from this example will vary depending on the value passed into Main
'but will be similar to the following:
'
'Content length is 1542
'Content type is text/html; charset=utf-8
'Response stream received.
'<html>
'...
'</html>
'
'
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::Net;
using namespace System::Text;
using namespace System::IO;
// Specify the URL to receive the request.
int main ()
{
String* args[] = Environment::GetCommandLineArgs();
HttpWebRequest* request = dynamic_cast<HttpWebRequest*>(WebRequest::Create (args[1]));
// Set some reasonable limits on resources used by this request
request->MaximumAutomaticRedirections = 4;
request->MaximumResponseHeadersLength = 4;
// Set credentials to use for this request.
request->Credentials = CredentialCache::DefaultCredentials;
HttpWebResponse* response = dynamic_cast<HttpWebResponse*>(request->GetResponse ());
Console::WriteLine (S"Content length is {0}", __box(response->ContentLength));
Console::WriteLine (S"Content type is {0}", response->ContentType);
// Get the stream associated with the response.
Stream* receiveStream = response->GetResponseStream ();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader* readStream = new StreamReader (receiveStream, Encoding::UTF8);
Console::WriteLine (S"Response stream received.");
Console::WriteLine (readStream->ReadToEnd ());
response->Close ();
readStream->Close ();
}
/*
The output from this example will vary depending on the value passed into Main
but will be similar to the following:
Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.
<html>
...
</html>
*/
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 3.5, 2.0, 1.0
Reference
Other Resources