This article mainly introduces the implementation method of C# using WebClient to log in to the website and capture the web page information after login. It involves C# session-based operation to log in to the web page and page reading related operation skills. Friends who need it can Refer to the following
The example of this article describes the implementation method of C# using WebClient to log in to the website and capture the web page information after login. Share it with everyone for your reference, the details are as follows:
C# Logging in to the website actually simulates the browser to submit the form, and then records the session Cookie value returned by the browser response, and brings it when sending the request again By requesting this session cookie value, you can achieve the effect of simulated login.
The following CookieAwareWebClient implementations carry cookies when sending requests.
public class CookieAwareWebClient : WebClient { private CookieContainer cookie = new CookieContainer(); protected override WebRequest GetWebRequest(Uri address) { WebRequest request = base.GetWebRequest(address); if (request is HttpWebRequest) { (request as HttpWebRequest).CookieContainer = cookie; } return request; } }
The following is a simulated form submission loginUsage example:
var client = new CookieAwareWebClient(); client.BaseAddress = @"https://hovertree.net/any/base/url/"; var loginData = new NameValueCollection(); loginData.Add("login", "YourLogin"); loginData.Add("password", "YourPassword"); client.UploadValues("login.php", "POST", loginData); //Now you are logged in and can request pages string htmlSource = client.DownloadString("index.php");
The above is the detailed content of C# uses WebClient to log in to the website and capture the web page information after login.. For more information, please follow other related articles on the PHP Chinese website!