HttpWebRequest.GetResponse method

Source: Internet
Author: User

The GetResponse method returns a WebResponse object that contains a response from an Internet resource . The actual returned instance is HttpWebResponse, and can be converted to a class that accesses HTTP-specific properties.

In some cases, when theThe HttpWebRequest class sets a violation of the property that is thrown when theProtocolViolationException.If the application willContentLength Properties andThe SendChunked property is set toTrue, and then sends an HTTP GET request, the exception is thrown.This exception is thrown if an application attempts to send a chunked request to a server that supports only the HTTP 1.0 protocol and does not support chunked requests. contentlength Property or the sendchunked is false when buffering was disabled and on a keepalive C Onnection (The KeepAlive property is true ) . " If the application is not set  , the contentlength  property attempts to send data, or the KeepAlive connection ( keepalive  property is   true), sendchunked  is   false, the exception is thrown span class= "input".

Warning

You must call the Close method to close the stream and release the connection. failure to do so may cause the application to run out of connections.

When using the POST method, you must get the request stream, write the data to be sent, and then close the request stream. This method blocks to wait for the content to be sent, and the calling thread will block indefinitely if there is no timeout setting and you do not provide the content.

description

GetResponse Return the same response object; The request is not reissued. " The   is called multiple times, getresponse  Returns the same response object, and the request is not re-emitted.

Description

Applications cannot mix synchronous and asynchronous methods with specific requests. If you call the GetRequestStream method, you must use the GetResponse method to retrieve the response.

description

WebException is thrown, use the Response and Status Properties of the exception to determine the response from the server. " If &NBSP is thrown, WebException, use the   of the exception, response  and  , and status  property to determine the response of the server.

description

This member outputs trace information when a network trace is enabled in the application.   Network tracing . " > For more information, see   network tracing.

Description

For security reasons, cookies are disabled by default. If you wish to use cookies, use the Cookiecontainer property to enable cookies.

1 usingSystem;2 usingSystem.Net;3 usingSystem.Text;4 usingSystem.IO;5 6 7      Public classTest8     {9         //Specify the URL to receive the request.Ten          Public Static voidMain (string[] args) One         { AHttpWebRequest request = (HttpWebRequest) webrequest.create (args[0]); -  -             //Set some reasonable limits on resources used by this request theRequest. Maximumautomaticredirections =4; -Request. maximumResponseHeadersLength =4; -             //Set Credentials to use for the this request. -Request. Credentials =CredentialCache.DefaultCredentials; +HttpWebResponse response =(HttpWebResponse) request. GetResponse (); -  +Console.WriteLine ("Content length is {0}", Response. ContentLength); AConsole.WriteLine ("Content type is {0}", Response. ContentType); at  -             //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 =NewStreamReader (Receivestream, Encoding.UTF8); in  -Console.WriteLine ("Response Stream received."); to Console.WriteLine (Readstream.readtoend ()); + Response. Close (); - readstream.close (); the         } *     } $ Panax Notoginseng /* - The output from this example would vary depending on the value passed into Main the But would be is similar to the following: +  A Content length is 1542 the Content type is text/html; Charset=utf-8 + Response Stream received. -  $ ... $  -  - */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.