Send a simple HTTP GET request and retrieve the response.

Source: Internet
Author: User

String uri= "http//www.baidu.com"; WebClient WC = new WebClient (); Console.WriteLine ("Sending an HTTP GET request to" +uri); byte[] Bresponse = WC. Downloaddata (URI); string strresponse = Encoding.ASCII.GetString (bresponse); Console.WriteLine ("HTTP response is:"); Console.WriteLine (Strresponse);
Annotations:

The WebClient class is part of the System.Net namespace and is accessible by default to the console program. Retrieving an HTTP response using the Webclient.downloaddata () method is extremely straightforward, but downloaddata () returns only a character array (byte array), It must be converted to a string by the System.Text.Encoding.ASCII.GetString () method. Another scenario is to use the Webclient.openread () method and correlate it with a stream:

String uri = "http://www.baidu.com"; WebClient WC = new WebClient (); Console.WriteLine ("Sending an HTTP GET request to" +url); Stream st = WC. OpenRead (URI); StreamReader sr=new StreamReader (ST); string res =SR. ReadToEnd (); Sr. Close (); St. Close (); Console.WriteLine ("HTTP Response is"); Console.WriteLine (RES);

The WebClient class is more suitable for testing static HTML Web pages than for testing ASP. This code can be used to detect the response returned by the ASP, but to extend this code into an automated test program, you need to check the HTTP response based on expectations. Determine the ViewState value of an ASP. NET Web application programmatically. How to check HTTP responses based on a given expectation.

Send a simple HTTP GET request and retrieve the response.

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.