- [C#] Di chuyển và thay đổi kích thước Control Winform khi ứng dụng đang chạy
- [VB.NET] Chia sẻ source tạo sắp xếp đội hình bóng đá Line-ups đội bóng
- [C#] Hướng dẫn chỉnh sửa Text của label trực tiếp trên winform
- [C#] Hướng dẫn custom TextBox giống Ultraviewer trên Winform
- [C#] Show Modal Winform like Bootstrap
- [DATABASE] Thứ tự thực hiện mệnh đề truy vấn SELECT trong Sqlserver
- [C#] Hướng dẫn viết addin Excel Lấy hình ảnh từ URL internet vào Excel
- [DATABASE] TSQL view max length all column data trên table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng MailMerge kèm Hình ảnh trên Winform
- [DATABASE] Hướng dẫn truy vấn xem kích thước lưu trữ của từng bảng ghi Table trên sqlserver
- [C#] Hướng dẫn Fake Date Time sử dụng thư viện Harmony
- [DATABASE] Phân biệt câu lệnh DDL và DML trong sqlserver
- [C#] Hướng dẫn convert file mã HTML sang file Pdf trên winform
- [DEVEXPRESS] Tạo các loại mã vạch Barcode trực tiếp trên Devexpress Barcode API
- [DEVEXPRESS] Hướng dẫn custom Simple button thành Progressbar
- [DATABASE] Tách số và chữ từ chuỗi - hàm tối ưu hóa tách số và chữ trong Sqlserver
- [C#] Tìm kiếm gần đúng Full Text Search sử dụng thư viện Lucene.NET
- [C#] Chia sẻ tài liệu, sdk và source code máy chấm công dòng máy ZKTeco
- [C#] Memory Cache là gì, và sử dụng trong ứng dụng Winform
- [DATABASE] Khóa chính Primary Key trong Sqlserver
[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 :)