. NET provides three basic methods and two underlying methods to send HTTP requests and receive HTTP responses, which allow us to mimic the way in which a Web page is accessed by entering a URL address in the browser's address bar. We send an HTTP request to receive the response returned by the server (usually an HTML page). From this to the resulting web page analysis, such as doing automated testing, or crawl the page you are interested in, and then put into their own programs, in short, the application of a lot, I can think of, for the time being so much.
The five methods are:
1.WebClient
2.webrequest-webresponse
3.httpwebrequest-httpwebresponse
4.TcpClient
5.Socket
The first three are relatively simple, the latter two are relatively lower
This paper first WebClient, relatively simple, directly on the code
C # code
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Text;
- Using System.Net;
- Namespace Webclienttest
- {
- Class Program
- {
- static void Main (string[] args)
- {
- string uri="http://starnc.iteye.com/blog/404768";
- WebClient wc=New WebClient ();
- Console.WriteLine ("Sending an HTTP Get request to" +uri);
- byte[] BRESPONSE=WC. Downloaddata (URI);
- string strresponse=encoding.utf8.getstring (Bresponse);
- Console.WriteLine ("HTTP response is:");
- Console.WriteLine (Strresponse);
- }
- }
- }
Get results such as
This is what we get from the Web source file, and you directly in IE to access the URL of the page to get the same result, with this you can do what you like to do.
This article refers to the ". NET Software Automation testing Way", a good book, you should see.
5 Kinds of request-response methods provided by. Net