NEWS

[C#] Hướng dẫn đăng nhập website sử dụng Http Request with Authenticate Username and password

[C#] Hướng dẫn đăng nhập website sử dụng Http Request with Authenticate Username and password
Đăng bởi: Thảo Meo - Lượt xem: 14423 10:54:33, 03/06/2017C#   In bài viết

Bài viết hôm nay, mình sẽ hướng dẫn các bạn cách đăng nhập vào website có sử dụng login username và password, bài viết này chúng ta sẽ sử dụng Http Request trong thư viện System.net của .net framework.

Ví dụ: Các bạn muốn login vào trang web https://laptrinhvb.net/soan-bai-viet.html. Để lấy nội dung html từ Url này, nhưng mặc định mặc muốn đăng nhập vào link này, bạn phải đăng nhập username và mật khẩu vào lúc đó, server sẽ tạo session cho chúng ta truy cập, còn nếu không mặc định website sẽ đưa chúng ta về trang web login.

login website httprequest c#

Đầu tiên các bạn tạo 1 class đặt tên sau CookieAwareWebClient.cs:

using System;
using System.Net;
/// 
/// A Cookie-aware WebClient that will store authentication cookie information and persist it through subsequent requests. ///

public class CookieAwareWebClient : WebClient {
 //Properties to handle implementing a timeout
 private int? _timeout = null;
 public int? Timeout { get { return _timeout; }
 set { _timeout = value; }
 }
 //A CookieContainer class to house the Cookie once it is contained within one of the Requests
public CookieContainer CookieContainer { get; private set; }
 public CookieCollection ResponseCookies { get; set; } 
//Constructor
 public CookieAwareWebClient() {
 CookieContainer = new CookieContainer();
 this.ResponseCookies = new CookieCollection();
 }
 public CookieAwareWebClient(CookieContainer cookies)
 {
 this.CookieContainer = cookies;
 }
 //Method to handle setting the optional timeout (in milliseconds)
 public void SetTimeout(int timeout) { 
_timeout = timeout;
 } 
//This handles using and storing the Cookie information as well as managing the Request timeout 
protected override WebRequest GetWebRequest(Uri address)
{
 //Handles the CookieContainer var request = (HttpWebRequest)base.GetWebRequest(address); request.CookieContainer = CookieContainer; //Sets the Timeout if it exists if (_timeout.HasValue) { request.Timeout = _timeout.Value; } return request; 
} 
}

Tiếp đến các bạn sử dụng hàm sau để đăng nhập vào website:

 

 private void button1_Click(object sender, EventArgs e)
        {
            using (var client = new CookieAwareWebClient())
            {
                client.Encoding = Encoding.UTF8;              
                var values = new NameValueCollection { { "username", textBox1.Text }, { "password", textBox2.Text } };
                client.UploadValues(new Uri("https://www.cloudflare.com/a/login"), "POST", values);
                var html = client.DownloadString(@"https://www.cloudflare.com");
                webBrowser1.DocumentText = html;
            }
        }

 

HAVE FUN :)

 

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn đăng nhập website sử dụng Http Request with Authenticate Username and password
Đăng bởi: Thảo Meo - Lượt xem: 14423 10:54:33, 03/06/2017C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.