HttpWebRequest.GetResponse 方法

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   os   strong   io   

GetResponse 方法返回包含來自 Internet 資源的響應的 WebResponse 對象。 實際返回的執行個體是 HttpWebResponse,並且能夠轉換為訪問 HTTP 特定的屬性的類。

 

在一些情況下,當對 HttpWebRequest 類設定的屬性發生衝突時將引發 ProtocolViolationException。 如果應用程式將 ContentLength 屬性和 SendChunked 屬性設定為true,然後發送 HTTP GET 請求,則會引發該異常。 如果應用程式嘗試向僅支援 HTTP 1.0 協議而不支援分塊請求的伺服器發送分塊請求,則會引發該異常。 如果應用程式未設定 ContentLength 屬性就嘗試發送資料,或者在 keepalive 串連(KeepAlive 屬性為 true)上禁用緩衝時 SendChunked 為 false,則會引發該異常。

 

警告

必須調用 Close 方法關閉該流並釋放串連。 如果未能做到這一點,可能導致應用程式用完串連。

使用 POST 方法時,必須擷取請求流,寫入要發送的資料,然後關閉請求流。 此方法阻塞以等待發送的內容;如果沒有逾時設定並且您沒有提供內容,調用線程將無限期地阻塞。

 

說明

多次調用 GetResponse 會返回相同的響應對象;該請求不會重新發出。

說明

應用程式不能對特定請求混合使用同步和非同步方法呼叫。 如果調用 GetRequestStream 方法,則必須使用 GetResponse 方法檢索響應。

 

 

說明

如果引發 WebException,請使用該異常的 Response 和 Status 屬性確定伺服器的響應。

說明

當應用程式中啟用了網路跟蹤時,此成員將輸出跟蹤資訊。 有關詳細資料,請參閱 網路跟蹤。

說明

為安全起見,預設情況下禁用 Cookie。 如果您希望使用 Cookie,請使用 CookieContainer 屬性啟用 Cookie。

 

 1 using System; 2 using System.Net; 3 using System.Text; 4 using System.IO; 5  6  7     public class Test 8     { 9         // Specify the URL to receive the request.10         public static void Main (string[] args)11         {12             HttpWebRequest request = (HttpWebRequest)WebRequest.Create (args[0]);13 14             // Set some reasonable limits on resources used by this request15             request.MaximumAutomaticRedirections = 4;16             request.MaximumResponseHeadersLength = 4;17             // Set credentials to use for this request.18             request.Credentials = CredentialCache.DefaultCredentials;19             HttpWebResponse response = (HttpWebResponse)request.GetResponse ();20 21             Console.WriteLine ("Content length is {0}", response.ContentLength);22             Console.WriteLine ("Content type is {0}", response.ContentType);23 24             // Get the stream associated with the response.25             Stream receiveStream = response.GetResponseStream ();26 27             // Pipes the stream to a higher level stream reader with the required encoding format. 28             StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);29 30             Console.WriteLine ("Response stream received.");31             Console.WriteLine (readStream.ReadToEnd ());32             response.Close ();33             readStream.Close ();34         }35     }36 37 /*38 The output from this example will vary depending on the value passed into Main 39 but will be similar to the following:40 41 Content length is 154242 Content type is text/html; charset=utf-843 Response stream received.44 <html>45 ...46 </html>47 48 */

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.