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.