Home >Backend Development >C++ >How to Programmatically Log In to a Website Using C#?

How to Programmatically Log In to a Website Using C#?

DDD
DDDOriginal
2025-01-18 09:36:10373browse

Write website login script using C#

Problem: My web scraper requires a logged in user to access specific source code on the website. How can I implement login functionality in my program to bypass this restriction?

Answer: To log in to the specified website (mmoinn.com/index.do?PageModule=UsersLogin), you can follow the steps below:

1. Send a POST request to the login form

Format your login credentials into form POST:

<code class="language-csharp">string formUrl = "http://www.mmoinn.com/index.do?PageModule=UsersAction&Action=UsersLogin";
string formParams = string.Format("email_address={0}&password={1}", "您的邮箱地址", "您的密码");

WebRequest req = WebRequest.Create(formUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";

byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;

using (Stream os = req.GetRequestStream())
{
    os.Write(bytes, 0, bytes.Length);
}

WebResponse resp = req.GetResponse();
string cookieHeader = resp.Headers["Set-cookie"];</code>

2. Get the login page

Create a GET request to the desired page containing the cookie header you retrieved from the login form:

<code class="language-csharp">string getUrl = "登录后页面的URL";
WebRequest getRequest = WebRequest.Create(getUrl);
getRequest.Headers.Add("Cookie", cookieHeader);
WebResponse getResponse = getRequest.GetResponse();

using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
    string pageSource = sr.ReadToEnd();
}</code>

Additional notes:

If you want to verify the response of the login POST, you can add the following lines of code:

<code class="language-csharp">using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
    string pageSource = sr.ReadToEnd();
}</code>

Check the pageSource string to check the server's response.

How to Programmatically Log In to a Website Using C#?

The above is the detailed content of How to Programmatically Log In to a Website Using C#?. 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