C# uses WebClient to log in to the website and capture the web page information after login.

黄舟
Release: 2017-05-14 10:37:31
Original
3070 people have browsed it

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;
  }
}
Copy after login

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");
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!