Home  >  Article  >  Backend Development  >  C# uses WebClient to log in to the website and capture the web page information after login.

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

黄舟
黄舟Original
2017-05-14 10:37:313087browse

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!

Statement:
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