共用方式為


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

例外狀況

先前對 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.
'...
'
'

備註

謹慎

WebRequestHttpWebRequestServicePointWebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient

GetResponse 方法會傳回 WebResponse 物件,其中包含來自因特網資源的回應。 傳回的實際實例是 HttpWebResponse,而且可以型別傳送至該類別以存取 HTTP 特定屬性。

HttpWebRequest 類別上設定的屬性發生衝突時,會在數種情況下擲回 ProtocolViolationException。 如果應用程式將 ContentLength 屬性和 SendChunked 屬性設定為 true,然後傳送 HTTP GET 要求,就會發生這個例外狀況。 如果應用程式嘗試將區塊傳送至只支援 HTTP 1.0 通訊協定的伺服器,但不支援此通訊協定,就會發生此例外狀況。 如果應用程式嘗試在不設定 ContentLength 屬性的情況下傳送數據,或在停用緩衝處理時 falseSendChunked,且在保留連線上發生此例外狀況(KeepAlive 屬性為 true.

謹慎

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

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

注意

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

注意

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

注意

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

注意

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

注意

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

適用於

另請參閱