HttpWebRequest.GetResponse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回來自網際網路資源的回應。
public:
override System::Net::WebResponse ^ GetResponse();
public override System.Net.WebResponse GetResponse ();
override this.GetResponse : unit -> System.Net.WebResponse
Public Overrides Function GetResponse () As WebResponse
傳回
包含來自網際網路資源回應的 WebResponse。
例外狀況
此資料流已由先前對 BeginGetResponse(AsyncCallback, Object) 的呼叫所使用。
-或-
TransferEncoding 設定為值,且 SendChunked 是 false
。
Method 是 GET 或 HEAD,且 ContentLength 是大於或等於零,或 SendChunked 是 true
。
-或-
KeepAlive 是 true
,AllowWriteStreamBuffering 是 false
,ContentLength 為 -1,SendChunked 是 false
,且 Method 為 POST 或 PUT。
-或-
HttpWebRequest 具有實體主體,但 GetResponse() 方法在沒有呼叫 GetRequestStream() 方法的情況下呼叫。
-或-
ContentLength 大於零,但應用程式不會寫入所有承諾的資料
要求快取驗證程式指出,此要求的回應可以由快取服務;不過,此要求包含要傳送到此伺服器的資料。 傳送資料的要求不得使用此快取。 如果您使用不正確實作的自訂快取驗證程式,便會發生這個例外狀況。
範例
下列程式代碼範例會取得要求的回應。
#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()
{
array<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("Content length is {0}", response->ContentLength);
Console::WriteLine("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 = gcnew StreamReader(receiveStream, Encoding::UTF8);
Console::WriteLine("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>
*/
using System;
using System.Net;
using System.Text;
using System.IO;
public class Test
{
// Specify the URL to receive the request.
public static void Main (string[] args)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]);
// 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 = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Content length is {0}", response.ContentLength);
Console.WriteLine("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("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>
*/
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
End Class
'
'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.
'...
'
'
備註
方法 GetResponse 會傳 WebResponse 回 物件,其中包含來自因特網資源的回應。 傳回的實際實例是 HttpWebResponse,而且可以型別傳送至該類別,以存取 HTTP 特定屬性。
ProtocolViolationException當類別上HttpWebRequest設定的屬性衝突時,會在數種情況下擲回 。 如果應用程式將 屬性和 SendChunked 屬性設定ContentLength為 true
,然後傳送 HTTP GET 要求,就會發生這個例外狀況。 如果應用程式嘗試將區塊化傳送至僅支援 HTTP 1.0 通訊協議的伺服器,但不支援此通訊協定,就會發生此例外狀況。 如果應用程式嘗試在未設定 ContentLength 屬性的情況下傳送數據,或 SendChunked 是在 false
停用緩衝時,以及在屬性 (保持連線時 KeepAlivetrue
,就會發生此例外狀況) .
警告
您必須呼叫 Close 方法來關閉數據流並釋放連線。 無法這麼做可能會導致您的應用程式用盡連線。
使用 POST 方法時,您必須取得要求數據流、寫入要張貼的數據,以及關閉數據流。 這個方法會封鎖等候內容張貼;如果沒有逾時設定,而且您未提供內容,則呼叫線程會無限期封鎖。
注意
傳回相同回應物件的多個呼叫 GetResponse ;要求不會重新發出。
注意
您的應用程式無法混合特定要求的同步和異步方法。 如果您呼叫 GetRequestStream 方法,則必須使用 GetResponse 方法來擷取回應。
注意
WebException如果擲回 ,請使用Response例外狀況的 和 Status 屬性來判斷伺服器的回應。
注意
在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤。
注意
基於安全性考慮,預設會停用 Cookie。 如果您想要使用 Cookie,請使用 CookieContainer 屬性來啟用 Cookie。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應