WebRequest.EndGetResponse-Methode
Gibt beim Überschreiben in einer abgeleiteten Klasse eine WebResponse zurück.
Namespace: System.Net
Assembly: System (in system.dll)
Syntax
'Declaration
Public Overridable Function EndGetResponse ( _
asyncResult As IAsyncResult _
) As WebResponse
'Usage
Dim instance As WebRequest
Dim asyncResult As IAsyncResult
Dim returnValue As WebResponse
returnValue = instance.EndGetResponse(asyncResult)
public virtual WebResponse EndGetResponse (
IAsyncResult asyncResult
)
public:
virtual WebResponse^ EndGetResponse (
IAsyncResult^ asyncResult
)
public WebResponse EndGetResponse (
IAsyncResult asyncResult
)
public function EndGetResponse (
asyncResult : IAsyncResult
) : WebResponse
Parameter
- asyncResult
Ein IAsyncResult, das auf eine ausstehende Anforderung einer Antwort verweist.
Rückgabewert
Eine WebResponse mit einer Antwort auf die Internetanforderung.
Ausnahmen
Ausnahmetyp | Bedingung |
---|---|
Es wurde versucht, auf die Methode zuzugreifen, obwohl die Methode in einer abgeleiteten Klasse nicht überschrieben wurde. |
Hinweise
Die EndGetResponse-Methode schließt eine asynchrone Anforderung einer Internetressource ab, die mit der BeginGetResponse-Methode gestartet wurde.
Hinweis
Die WebRequest-Klasse ist eine abstract Klasse. Das tatsächliche Verhalten von WebRequest-Instanzen zur Laufzeit wird von der abgeleiteten Klasse bestimmt, die von der System.Net.WebRequest.Create-Methode zurückgegeben wird. Weitere Informationen über Standardwerte und Ausnahmen finden Sie in der Dokumentation zu den abgeleiteten Klassen, z. B. HttpWebRequest und FileWebRequest.
Beispiel
Im folgenden Beispiel wird EndGetResponse aufgerufen, um die Zielressource abzurufen.
Imports System
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Threading
Imports Microsoft.VisualBasic
Public Class RequestState
' This class stores the state of the request
Private Shared BUFFER_SIZE As Integer = 1024
Public requestData As StringBuilder
Public bufferRead() As Byte
Public request As WebRequest
Public response As WebResponse
Public responseStream As Stream
Public Sub New()
bufferRead = New Byte(BUFFER_SIZE) {}
requestData = New StringBuilder("")
request = Nothing
responseStream = Nothing
End Sub ' New
End Class ' RequestState
Class WebRequest_BeginGetResponse
Public Shared allDone As New ManualResetEvent(False)
Private Shared BUFFER_SIZE As Integer = 1024
Shared Sub Main()
Try
' Create a new webrequest to the mentioned URL.
Dim myWebRequest As WebRequest = WebRequest.Create("https://www.contoso.com")
'Please, set the proxy to a correct value.
Dim proxy As New WebProxy("itgproxy:80")
proxy.Credentials = New NetworkCredential("srikun", "simrin123")
myWebRequest.Proxy = proxy
' Create a new instance of the RequestState.
Dim myRequestState As New RequestState()
' The 'WebRequest' object is associated to the 'RequestState' object.
myRequestState.request = myWebRequest
' Start the Asynchronous call for response.
Dim asyncResult As IAsyncResult = CType(myWebRequest.BeginGetResponse(AddressOf RespCallback, myRequestState), IAsyncResult)
allDone.WaitOne()
' Release the WebResponse resource.
myRequestState.response.Close()
Console.Read()
Catch e As WebException
Console.WriteLine("WebException raised!")
Console.WriteLine(ControlChars.Cr + "{0}", e.Message)
Console.WriteLine(ControlChars.Cr + "{0}", e.Status)
Catch e As Exception
Console.WriteLine("Exception raised!")
Console.WriteLine(("Source : " + e.Source))
Console.WriteLine(("Message : " + e.Message))
End Try
End Sub ' Main
Private Shared Sub RespCallback(asynchronousResult As IAsyncResult)
Try
' Set the State of request to asynchronous.
Dim myRequestState As RequestState = CType(asynchronousResult.AsyncState, RequestState)
Dim myWebRequest1 As WebRequest = myRequestState.request
' End the Asynchronous response.
myRequestState.response = myWebRequest1.EndGetResponse(asynchronousResult)
' Read the response into a 'Stream' object.
Dim responseStream As Stream = myRequestState.response.GetResponseStream()
myRequestState.responseStream = responseStream
' Begin the reading of the contents of the HTML page and print it to the console.
Dim asynchronousResultRead As IAsyncResult = responseStream.BeginRead(myRequestState.bufferRead, 0, BUFFER_SIZE, AddressOf ReadCallBack, myRequestState)
Catch e As WebException
Console.WriteLine("WebException raised!")
Console.WriteLine(ControlChars.Cr + "{0}", e.Message)
Console.WriteLine(ControlChars.Cr + "{0}", e.Status)
Catch e As Exception
Console.WriteLine("Exception raised!")
Console.WriteLine(("Source : " + e.Source))
Console.WriteLine(("Message : " + e.Message))
End Try
End Sub ' RespCallback
Private Shared Sub ReadCallBack(asyncResult As IAsyncResult)
Try
' Result state is set to AsyncState.
Dim myRequestState As RequestState = CType(asyncResult.AsyncState, RequestState)
Dim responseStream As Stream = myRequestState.responseStream
Dim read As Integer = responseStream.EndRead(asyncResult)
' Read the contents of the HTML page and then print to the console.
If read > 0 Then
myRequestState.requestData.Append(Encoding.ASCII.GetString(myRequestState.bufferRead, 0, read))
Dim asynchronousResult As IAsyncResult = responseStream.BeginRead(myRequestState.bufferRead, 0, BUFFER_SIZE, AddressOf ReadCallBack, myRequestState)
Else
Console.WriteLine(ControlChars.Cr + "The HTML page Contents are: ")
If myRequestState.requestData.Length > 1 Then
Dim sringContent As String
sringContent = myRequestState.requestData.ToString()
Console.WriteLine(sringContent)
End If
Console.WriteLine(ControlChars.Cr + "Press 'Enter' key to continue........")
responseStream.Close()
allDone.Set()
End If
Catch e As WebException
Console.WriteLine("WebException raised!")
Console.WriteLine(ControlChars.Cr + "{0}", e.Message)
Console.WriteLine(ControlChars.Cr + "{0}", e.Status)
Catch e As Exception
Console.WriteLine("Exception raised!")
Console.WriteLine("Source :{0} ", e.Source)
Console.WriteLine("Message :{0} ", e.Message)
End Try
End Sub ' ReadCallBack
End Class ' WebRequest_BeginGetResponse
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Threading;
public class RequestState
{
// This class stores the state of the request.
const int BUFFER_SIZE = 1024;
public StringBuilder requestData;
public byte[] bufferRead;
public WebRequest request;
public WebResponse response;
public Stream responseStream;
public RequestState()
{
bufferRead = new byte[BUFFER_SIZE];
requestData = new StringBuilder("");
request = null;
responseStream = null;
}
}
class WebRequest_BeginGetResponse
{
public static ManualResetEvent allDone= new ManualResetEvent(false);
const int BUFFER_SIZE = 1024;
static void Main()
{
try
{
// Create a new webrequest to the mentioned URL.
WebRequest myWebRequest= WebRequest.Create("https://www.contoso.com");
// Please, set the proxy to a correct value.
WebProxy proxy=new WebProxy("myproxy:80");
proxy.Credentials=new NetworkCredential("srikun","simrin123");
myWebRequest.Proxy=proxy;
// Create a new instance of the RequestState.
RequestState myRequestState = new RequestState();
// The 'WebRequest' object is associated to the 'RequestState' object.
myRequestState.request = myWebRequest;
// Start the Asynchronous call for response.
IAsyncResult asyncResult=(IAsyncResult) myWebRequest.BeginGetResponse(new AsyncCallback(RespCallback),myRequestState);
allDone.WaitOne();
// Release the WebResponse resource.
myRequestState.response.Close();
Console.Read();
}
catch(WebException e)
{
Console.WriteLine("WebException raised!");
Console.WriteLine("\n{0}",e.Message);
Console.WriteLine("\n{0}",e.Status);
}
catch(Exception e)
{
Console.WriteLine("Exception raised!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
}
private static void RespCallback(IAsyncResult asynchronousResult)
{
try
{
// Set the State of request to asynchronous.
RequestState myRequestState=(RequestState) asynchronousResult.AsyncState;
WebRequest myWebRequest1=myRequestState.request;
// End the Asynchronous response.
myRequestState.response = myWebRequest1.EndGetResponse(asynchronousResult);
// Read the response into a 'Stream' object.
Stream responseStream = myRequestState.response.GetResponseStream();
myRequestState.responseStream=responseStream;
// Begin the reading of the contents of the HTML page and print it to the console.
IAsyncResult asynchronousResultRead = responseStream.BeginRead(myRequestState.bufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);
}
catch(WebException e)
{
Console.WriteLine("WebException raised!");
Console.WriteLine("\n{0}",e.Message);
Console.WriteLine("\n{0}",e.Status);
}
catch(Exception e)
{
Console.WriteLine("Exception raised!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
}
private static void ReadCallBack(IAsyncResult asyncResult)
{
try
{
// Result state is set to AsyncState.
RequestState myRequestState = (RequestState)asyncResult.AsyncState;
Stream responseStream = myRequestState.responseStream;
int read = responseStream.EndRead( asyncResult );
// Read the contents of the HTML page and then print to the console.
if (read > 0)
{
myRequestState.requestData.Append(Encoding.ASCII.GetString(myRequestState.bufferRead, 0, read));
IAsyncResult asynchronousResult = responseStream.BeginRead( myRequestState.bufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);
}
else
{
Console.WriteLine("\nThe HTML page Contents are: ");
if(myRequestState.requestData.Length>1)
{
string sringContent;
sringContent = myRequestState.requestData.ToString();
Console.WriteLine(sringContent);
}
Console.WriteLine("\nPress 'Enter' key to continue........");
responseStream.Close();
allDone.Set();
}
}
catch(WebException e)
{
Console.WriteLine("WebException raised!");
Console.WriteLine("\n{0}",e.Message);
Console.WriteLine("\n{0}",e.Status);
}
catch(Exception e)
{
Console.WriteLine("Exception raised!");
Console.WriteLine("Source : {0}" , e.Source);
Console.WriteLine("Message : {0}" , e.Message);
}
}
}
#using <System.dll>
using namespace System;
using namespace System::Net;
using namespace System::IO;
using namespace System::Text;
using namespace System::Threading;
public ref class RequestState
{
private:
// This class stores the state of the request.
literal int BUFFER_SIZE = 1024;
public:
StringBuilder^ requestData;
array<Byte>^bufferRead;
WebRequest^ request;
WebResponse^ response;
Stream^ responseStream;
RequestState()
{
bufferRead = gcnew array<Byte>(BUFFER_SIZE);
requestData = gcnew StringBuilder( "" );
request = nullptr;
responseStream = nullptr;
}
};
ref class WebRequest_BeginGetResponse
{
public:
static ManualResetEvent^ allDone = gcnew ManualResetEvent( false );
literal int BUFFER_SIZE = 1024;
static void RespCallback( IAsyncResult^ asynchronousResult )
{
try
{
// Set the State of request to asynchronous.
RequestState^ myRequestState = dynamic_cast<RequestState^>(asynchronousResult->AsyncState);
WebRequest^ myWebRequest1 = myRequestState->request;
// End the Asynchronous response.
myRequestState->response = myWebRequest1->EndGetResponse( asynchronousResult );
// Read the response into a 'Stream' object.
Stream^ responseStream = myRequestState->response->GetResponseStream();
myRequestState->responseStream = responseStream;
// Begin the reading of the contents of the HTML page and print it to the console.
IAsyncResult^ asynchronousResultRead = responseStream->BeginRead( myRequestState->bufferRead, 0, BUFFER_SIZE, gcnew AsyncCallback( ReadCallBack ), myRequestState );
}
catch ( WebException^ e )
{
Console::WriteLine( "WebException raised!" );
Console::WriteLine( "\n {0}", e->Message );
Console::WriteLine( "\n {0}", e->Status );
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception raised!" );
Console::WriteLine( "Source : {0}", e->Source );
Console::WriteLine( "Message : {0}", e->Message );
}
}
static void ReadCallBack( IAsyncResult^ asyncResult )
{
try
{
// Result state is set to AsyncState.
RequestState^ myRequestState = dynamic_cast<RequestState^>(asyncResult->AsyncState);
Stream^ responseStream = myRequestState->responseStream;
int read = responseStream->EndRead( asyncResult );
// Read the contents of the HTML page and then print to the console.
if ( read > 0 )
{
myRequestState->requestData->Append( Encoding::ASCII->GetString( myRequestState->bufferRead, 0, read ) );
IAsyncResult^ asynchronousResult = responseStream->BeginRead( myRequestState->bufferRead, 0, BUFFER_SIZE, gcnew AsyncCallback( ReadCallBack ), myRequestState );
}
else
{
Console::WriteLine( "\nThe HTML page Contents are: " );
if ( myRequestState->requestData->Length > 1 )
{
String^ sringContent;
sringContent = myRequestState->requestData->ToString();
Console::WriteLine( sringContent );
}
Console::WriteLine( "\nPress 'Enter' key to continue........" );
responseStream->Close();
allDone->Set();
}
}
catch ( WebException^ e )
{
Console::WriteLine( "WebException raised!" );
Console::WriteLine( "\n {0}", e->Message );
Console::WriteLine( "\n {0}", e->Status );
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception raised!" );
Console::WriteLine( "Source : {0}", e->Source );
Console::WriteLine( "Message : {0}", e->Message );
}
}
};
int main()
{
try
{
// Create a new webrequest to the mentioned URL.
WebRequest^ myWebRequest = WebRequest::Create( "https://www.contoso.com" );
// Please, set the proxy to a correct value.
WebProxy^ proxy = gcnew WebProxy( "myproxy:80" );
proxy->Credentials = gcnew NetworkCredential( "srikun","simrin123" );
myWebRequest->Proxy = proxy;
// Create a new instance of the RequestState.
RequestState^ myRequestState = gcnew RequestState;
// The 'WebRequest' object is associated to the 'RequestState' object.
myRequestState->request = myWebRequest;
// Start the Asynchronous call for response.
IAsyncResult^ asyncResult = dynamic_cast<IAsyncResult^>(myWebRequest->BeginGetResponse( gcnew AsyncCallback( WebRequest_BeginGetResponse::RespCallback ), myRequestState ));
WebRequest_BeginGetResponse::allDone->WaitOne();
// Release the WebResponse resource.
myRequestState->response->Close();
Console::Read();
}
catch ( WebException^ e )
{
Console::WriteLine( "WebException raised!" );
Console::WriteLine( "\n {0}", e->Message );
Console::WriteLine( "\n {0}", e->Status );
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception raised!" );
Console::WriteLine( "Source : {0}", e->Source );
Console::WriteLine( "Message : {0}", e->Message );
}
}
import System.*;
import System.Net.*;
import System.IO.*;
import System.Text.*;
import System.Threading.*;
public class RequestState
{
// This class stores the state of the request.
private static final int BUFFER_SIZE = 1024;
public StringBuilder requestData;
public ubyte bufferRead[];
public WebRequest request;
public WebResponse response;
public Stream responseStream;
public RequestState()
{
bufferRead = new ubyte[BUFFER_SIZE];
requestData = new StringBuilder("");
request = null;
responseStream = null;
} //RequestState
} //RequestState
class WebRequest_BeginGetResponse
{
public static ManualResetEvent allDone = new ManualResetEvent(false);
private static final int BUFFER_SIZE = 1024;
public static void main(String[] args)
{
try {
// Create a new webrequest to the mentioned URL.
WebRequest myWebRequest =
WebRequest.Create("https://www.contoso.com");
// Please, set the proxy to a correct value.
WebProxy proxy = new WebProxy("myproxy:80");
proxy.set_Credentials(new NetworkCredential("srikun", "simrin123"));
myWebRequest.set_Proxy(proxy);
// Create a new instance of the RequestState.
RequestState myRequestState = new RequestState();
// The 'WebRequest' object is associated to the 'RequestState' object.
myRequestState.request = myWebRequest;
// Start the Asynchronous call for response.
IAsyncResult asyncResult = (IAsyncResult)(myWebRequest.
BeginGetResponse(new AsyncCallback(RespCallback),
myRequestState));
allDone.WaitOne();
// Release the WebResponse resource.
myRequestState.response.Close();
Console.Read();
}
catch (WebException e) {
Console.WriteLine("WebException raised!");
Console.WriteLine("\n{0}", e.get_Message());
Console.WriteLine("\n{0}", e.get_Status());
}
catch (System.Exception e) {
Console.WriteLine("Exception raised!");
Console.WriteLine("Source : " + e.get_Source());
Console.WriteLine("Message : " + e.get_Message());
}
} //main
private static void RespCallback(IAsyncResult asynchronousResult)
{
try {
// Set the State of request to asynchronous.
RequestState myRequestState = (RequestState)(
asynchronousResult.get_AsyncState());
WebRequest myWebRequest1 = myRequestState.request;
// End the Asynchronous response.
myRequestState.response = myWebRequest1.EndGetResponse(
asynchronousResult);
// Read the response into a 'Stream' object.
Stream responseStream = myRequestState.response.GetResponseStream();
myRequestState.responseStream = responseStream;
// Begin the reading of the contents of the HTML page and print it
// to the console.
IAsyncResult asynchronousResultRead = responseStream.BeginRead(
myRequestState.bufferRead, 0, BUFFER_SIZE, new AsyncCallback(
ReadCallBack), myRequestState);
}
catch (WebException e) {
Console.WriteLine("WebException raised!");
Console.WriteLine("\n{0}", e.get_Message());
Console.WriteLine("\n{0}", e.get_Status());
}
catch (System.Exception e) {
Console.WriteLine("Exception raised!");
Console.WriteLine("Source : " + e.get_Source());
Console.WriteLine("Message : " + e.get_Message());
}
} //RespCallback
private static void ReadCallBack(IAsyncResult asyncResult)
{
try {
// Result state is set to AsyncState.
RequestState myRequestState = (RequestState)(
asyncResult.get_AsyncState());
Stream responseStream = myRequestState.responseStream;
int read = responseStream.EndRead(asyncResult);
// Read the contents of the HTML page and then print to the console.
if (read > 0) {
myRequestState.requestData.Append(Encoding.get_ASCII().
GetString(myRequestState.bufferRead, 0, read));
IAsyncResult asynchronousResult = responseStream.BeginRead(
myRequestState.bufferRead, 0, BUFFER_SIZE,
new AsyncCallback(ReadCallBack), myRequestState);
}
else {
Console.WriteLine("\nThe HTML page Contents are: ");
if (myRequestState.requestData.get_Length() > 1) {
String sringContent;
sringContent = myRequestState.requestData.ToString();
Console.WriteLine(sringContent);
}
Console.WriteLine("\nPress 'Enter' key to continue........");
responseStream.Close();
allDone.Set();
}
}
catch (WebException e) {
Console.WriteLine("WebException raised!");
Console.WriteLine("\n{0}", e.get_Message());
Console.WriteLine("\n{0}", e.get_Status());
}
catch (System.Exception e) {
Console.WriteLine("Exception raised!");
Console.WriteLine("Source : {0}", e.get_Source());
Console.WriteLine("Message : {0}", e.get_Message());
}
} //ReadCallBack
} //WebRequest_BeginGetResponse
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0, 1.0
Siehe auch
Referenz
WebRequest-Klasse
WebRequest-Member
System.Net-Namespace
BeginGetResponse