- [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 thêm cookie vào Http Request website
Xin chào các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn cách tích hợp cookie vào HttpRequest trong lập trình C#.
Khi các bạn gởi Request để lấy dữ liệu từ website, một số trang web có yêu cầu cookie để trả Response về.
Vậy Cookie là gì?
Cookie là một đoạn văn bản mà một Web server có thể lưu trên ổ cứng của người dùng. Cookie cho phép một website lưu các thông tin trên máy tính của người dùng và sau đó lấy lại nó.
Các mẩu thông tin sẽ được lưu dưới dạng cặp tên – giá trị (name-value).
Mình ví dụ ở trang web http://batdongsan.com.vn
Giống như hình ở trên, các bạn sẽ thấy khi truy cập lấy thông tin mới nhất từ trang web batdongsan.com.vn, thì nó sẽ gởi theo cookie với tham số
Key: psortfilter và Value: 1%24all%24%2BAhRasUDZiQ64tRxy5UrOQ%3D%3D
Ở đường dẫn request trên mình request từ địa chỉ: https://batdongsan.com.vn/ban-can-ho-chung-cu-tp-hcm
Nếu mặc định chúng ta không gởi kèm thông tin Cookie thì nó trả về cho chúng ta dữ liệu là tin thông thường.
Vì vậy, khi request chúng ta cần gởi bộ cookie để dữ liệu server trả về đúng yêu cầu của mình.
Dưới đây là hình ảnh demo Request khi sử dụng cookie và không sử dụng cookie để các bạn tham khảo:
Source code Cookie Http Request C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace bds
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Task MakeAsyncRequest(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
Cookie cookie = new Cookie("psortfilter", txt_cookie.Text);
request.CookieContainer = new CookieContainer();
Uri uri = new Uri(url);
request.CookieContainer.Add(uri, cookie);
request.Method = WebRequestMethods.Http.Get;
request.Timeout = 20000;
request.Proxy = null;
Task task = Task.Factory.FromAsync(
request.BeginGetResponse,
asyncResult => request.EndGetResponse(asyncResult),
(object)null);
return task.ContinueWith(t => ReadStreamFromResponse(t.Result));
}
private static string ReadStreamFromResponse(WebResponse response)
{
using (Stream responseStream = response.GetResponseStream())
using (StreamReader sr = new StreamReader(responseStream))
{
string strContent = sr.ReadToEnd();
return strContent;
}
}
private void button1_Click(object sender, EventArgs e)
{
string http = "https://batdongsan.com.vn/ban-can-ho-chung-cu-tp-hcm";
string data = MakeAsyncRequest(http).Result;
richTextBox1.Text = data;
webBrowser1.DocumentText = data;
}
}
}
HAVE FUN :)