HttpWebRequest.GetResponse 方法

定義

傳回來自網際網路資源的回應。

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

包含來自網際網路資源回應的 WebResponse

例外狀況

此資料流已由先前對 BeginGetResponse(AsyncCallback, Object) 的呼叫所使用。

-或- TransferEncoding 設定為值,且 SendChunkedfalse

Method 是 GET 或 HEAD,且 ContentLength 是大於或等於零,或 SendChunkedtrue

-或- KeepAlivetrueAllowWriteStreamBufferingfalseContentLength 為 -1,SendChunkedfalse,且 Method 為 POST 或 PUT。

-或- HttpWebRequest 具有實體主體,但 GetResponse() 方法在沒有呼叫 GetRequestStream() 方法的情況下呼叫。

-或- ContentLength 大於零,但應用程式不會寫入所有承諾的資料

要求快取驗證程式指出,此要求的回應可以由快取服務;不過,此要求包含要傳送到此伺服器的資料。 傳送資料的要求不得使用此快取。 如果您使用不正確實作的自訂快取驗證程式,便會發生這個例外狀況。

之前已呼叫過 Abort()

-或- 要求的逾時時限已過期。

-或- 處理這個要求時發生錯誤。

範例

下列程式碼範例會取得要求的回應。

#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 屬性設定 ContentLengthtrue ,然後傳送 HTTP GET 要求,就會發生這個例外狀況。 如果應用程式嘗試將區塊化傳送至僅支援 HTTP 1.0 通訊協定的伺服器,但不支援此通訊協定,就會發生此例外狀況。 如果應用程式嘗試在未設定 ContentLength 屬性的情況下傳送資料,或 SendChunked 是在 false 停用緩衝時,以及在屬性 (保持連線時 KeepAlive true ,就會發生此例外狀況) .

警告

您必須呼叫 Close 方法來關閉資料流程並釋放連線。 無法這麼做可能會導致您的應用程式用盡連線。

使用 POST 方法時,您必須取得要求資料流程、寫入要張貼的資料,以及關閉資料流程。 這個方法會封鎖等候內容張貼;如果沒有逾時設定,而且您未提供內容,則呼叫執行緒會無限期地封鎖。

注意

傳回相同回應物件的多個呼叫 GetResponse ;要求不會重新發出。

注意

您的應用程式無法混合特定要求的同步和非同步方法。 如果您呼叫 GetRequestStream 方法,則必須使用 GetResponse 方法來擷取回應。

注意

WebException如果擲回 ,請使用 Response 例外狀況的 和 Status 屬性來判斷伺服器的回應。

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱.NET Framework中的網路追蹤

注意

基於安全性考慮,預設會停用 Cookie。 如果您想要使用 Cookie,請使用 CookieContainer 屬性來啟用 Cookie。

適用於

另請參閱