I. Basic WebClient class
1. First use the OpenRead () method to return a stream stream object
2. Converting stream stream objects to StreamReader objects
3. Use the ReadLine () method to get the data from the data stream in the form of text
New WebClient (); = Web. OpenRead ("http://www.reuters.com"); New StreamReader (stream); while NULL ) { string line ; = Sr. ReadLine (); LISTBOX1.ITEMS.ADD (line); } Stream. Close ();
Two. WebRequest and WebResponse class
. WebRequest represents a request to send information to a particular URI, the URI is passed as a parameter to the Create () method, and the WebResponse class represents the data retrieved from the server
1. Create a new Thread request page
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.Net;usingSystem.IO;usingSystem.Threading;namespacewindowsformsapplication1{ Public Delegate voidMyDelegate (webheadercollection s);//Declaration of Entrustment Public Partial classForm1:form {Thread x; PublicForm1 () {InitializeComponent ();WebRequest WRQ= WebRequest.Create ("http://www.reuters.com"); //Define a threadx =NewThread (Doresponse); //Start ThreadX.start (WRQ); } Private voidDoresponse (Objectar) {WebRequest WRQ=(WebRequest) ar; WebResponse WRP=WRQ. GetResponse (); WebHeaderCollection WHC= WRP. Headers;//WRP. Headers: Gets the collection of header values associated with the requestMyDelegate my =NewMyDelegate (SetText);//declares that the delegate executes a method of SetText () This. Invoke (My,New Object[] {WHC});//executes the specified delegate on the thread that owns the underlying form handle of this control, and the second parameter of this method is used to pass in the method instead of the formal parameter WHC } Private voidsettext (webheadercollection WHC) { for(inti =0; I < WHC. Count; i++) {LISTBOX1.ITEMS.ADD (string. Format ("name {0}:{1}", WHC. GetKey (i), whc[i]));//GetKey (i) represents the header name, Whc[i], and WHC for the specified index. Get (i) represents a value that gets the name of a specific headerLISTBOX1.ITEMS.ADD (string. Format ("name name {0}:{1}", WHC. GetKey (i), WHC. Get (i))); } x.abort ();//close this thread } }}
2. Using asynchronous methods for page requests
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.Net;usingSystem.IO;usingSystem.Threading;namespacewindowsformsapplication1{ Public Delegate voidMyDelegate (webheadercollection s); Public Partial classForm1:form {Thread x; PublicForm1 () {InitializeComponent ();WebRequest WRQ= WebRequest.Create ("http://www.reuters.com"); //Attach the authentication certificate to the requestNetworkCredential mycredentials =NewNetworkCredential ("LZW","123"); HttpWebRequest HWRQ= (HttpWebRequest) WRQ;//you can use a derived class wherever you want the WebRequest class HttpWebRequestHwrq. Credentials = mycredentials;//Obtaining authentication information
Properties specific to the HttpWebRequest class: ListBox1.Items.Add ("time to wait for exception to be thrown:"+ HWRQ. Timeout);//HWRQ. Timeout default value 100000 millisecondsLISTBOX1.ITEMS.ADD ("whether to establish a persistent connection to Internet resources:"+ HWRQ. KeepAlive);//default is TrueLISTBOX1.ITEMS.ADD ("whether to automatically follow Web redirect responses:"+ HWRQ. AllowAutoRedirect);//default is True//Asynchronous page RequestIAsyncResult ar = Hwrq. BeginGetResponse (NULL,NULL); HWRQ=(HttpWebRequest) ar. asyncstate; WebResponse WRP=WRQ. EndGetResponse (AR);WebHeaderCollection WHC = WRP. Headers;//WRP. Headers: Gets the collection of header values associated with the request for(inti =0; I < WHC. Count; i++) {LISTBOX1.ITEMS.ADD (string. Format ("name {0}:{1}", WHC. GetKey (i), whc[i]));//GetKey (i) represents the header name, Whc[i], and WHC for the specified index. Get (i) represents a value that gets the name of a specific headerLISTBOX1.ITEMS.ADD (string. Format ("name name {0}:{1}", WHC. GetKey (i), WHC. Get (i))); } } }}