- [DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#
- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
[C#] Hướng dẫn đăng nhập website sử dụng Http Request with Authenticate Username and password
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.
Đầ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 :)