WebException.Response 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得遠端主機所傳回的回應。
public:
property System::Net::WebResponse ^ Response { System::Net::WebResponse ^ get(); };
public System.Net.WebResponse Response { get; }
public System.Net.WebResponse? Response { get; }
member this.Response : System.Net.WebResponse
Public ReadOnly Property Response As WebResponse
屬性值
如果可以從網際網路資源使用回應,則為包含來自網際網路資源之錯誤回應的 WebResponse 執行個體,否則為 null
。
範例
下列範例會Status檢查 屬性,並列印至主控台和StatusCodeStatusDescription基礎HttpWebResponse實例的 。
try
{
// Create a web request for an unknown server (this raises the WebException).
HttpWebRequest^ myHttpWebRequest = (HttpWebRequest^)(WebRequest::Create( "http://unknown.unknown.com" ));
// 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 );
}
}
catch ( Exception^ e )
{
Console::WriteLine( e->Message );
}
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);
}
}
catch(Exception e) {
Console.WriteLine(e.Message);
}
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
Catch e As Exception
Console.WriteLine(e.Message)
End Try
備註
某些因特網通訊協定,例如 HTTP,會傳回有效的回應,指出通訊協定層級發生錯誤。 當對因特網要求的回應指出錯誤時,WebRequest.GetResponse請將 Status 屬性設定為 WebExceptionStatus.ProtocolError ,並提供 ,WebResponse其中包含擲回 之 WebException 屬性中的Response錯誤訊息。 應用程式可以檢查 WebResponse 以判斷實際錯誤。