- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
[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 :)