Send a simple http get request and respond

Source: Internet
Author: User

Problem

How to send a simple http get request and retrieve the corresponding HTTP response.

Design

Create a WebClient class instance and use its downloaddata () method.

Solution
 String Uri = "  Http: // server/path/webform. aspx  "  ; WebClient WC = New  WebClient (); console. writeline (  "  Sending an http get request  " + Uri );  Byte [] Bresponse = WC. downloaddata (URI );  String Strresponse = Encoding. ASCII. getstring (bresponse); console. writeline (  "  HTTP response is:  "  ); Console. writeline (strresponse ); 

 

Annotation

The WebClient class is part of the system. Net life namespace. By default, it is accessible to console programs. Through WebClient. downloaddata () method to retrieve an HTTP response is extremely simple, but downloaddata () returns only a byte array (byte array), which must be passed through system. text. encoding. ASCII. the getstring () method converts it into a string. Another solution is to use the WebClient. openread () method and associate it with a stream:

String Uri = "  Http: // server/path/webform. aspx  "  ; WebClient WC = New  WebClient (); console. writeline (  "  Sending an http get request  " + Uri); 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 ); 

 

Compared with ASP. NET web applications, The WebClient class is more suitable for testing static HTML web pages. This code can be used to detect the responses returned by the ASP. NET program, but to expand this code into an automatic testing program, you need to check the HTTP response as expected. Section 5.8 uses the technology in this section. In section 5.8, we use programming methods to determine the viewstate value of ASP. NET web applications. The technologies involved in section 5.11 demonstrate how to check an HTTP response based on the given expectations.

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.