- [DEVEXPRESS] Hỗ trợ tìm kiếm highlight không dấu và không khoảng cách trên Gridview Filter
- [C#] Chia sẻ source code phần mềm Image Downloader tải hàng loạt hình ảnh từ danh sách link url
- [C#] Chụp hình và quay video từ camera trên winform
- [C#] Chia sẽ full source code tách file Pdf thành nhiều file với các tùy chọn
- Giới thiệu về Stock Tracker Widget - Công cụ theo dõi cổ phiếu và cảnh báo giá tăng giảm bằng C# và WPF
- [VB.NET] Chia sẻ công cụ nhập số tiền tự động định dạng tiền tệ Việt Nam
- [VB.NET] Hướng dẫn fill dữ liệu từ winform vào Microsoft word
- [VB.NET] Hướng dẫn chọn nhiều dòng trên Datagridview
- Hướng Dẫn Đăng Nhập Nhiều Tài Khoản Zalo Trên Máy Tính Cực Kỳ Đơn Giản
- [C#] Chia sẻ source code phần mềm đếm số trang tập tin file PDF
- [C#] Cách Sử Dụng DeviceId trong C# Để Tạo Khóa Cho Ứng Dụng
- [SQLSERVER] Loại bỏ Restricted User trên database MSSQL
- [C#] Hướng dẫn tạo mã QRcode Style trên winform
- [C#] Hướng dẫn sử dụng temp mail service api trên winform
- [C#] Hướng dẫn tạo mã thanh toán VietQR Pay không sử dụng API trên winform
- [C#] Hướng Dẫn Tạo Windows Service Đơn Giản Bằng Topshelf
- [C#] Chia sẻ source code đọc dữ liệu từ Google Sheet trên winform
- [C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform
- [C#] Chia sẻ source code phần mềm lên lịch tự động chạy ứng dụng Scheduler Task Winform
- [C#] Hướng dẫn download file từ Minio Server Winform
[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 :)

![[C#] Hướng dẫn thêm cookie vào Http Request website](https://laptrinhvb.net/uploads/users/9a8cb514e4428e85fb4ca07588e9103f.png)


![⏰ [C#] Chia sẽ source code ứng dụng Chronometer trong lập trình C#](https://laptrinhvb.net/uploads/source/image_baiviet/9b002f2b21b8a98d0c6b880366303f7b.gif)
![[C#] Chia sẻ code lock và unlock user trong domain Window](https://laptrinhvb.net/uploads/source/new_image_baiviet/lock_unlock_user_domain.png)
![[C#] Hướng dẫn sử dụng định dạng chuỗi với String.Format](https://laptrinhvb.net/uploads/source/csharp/string_format_thumb.jpg)
![[C#] Hướng dẫn hiển thị Message Box with Button Details](https://laptrinhvb.net/uploads/source/csharp/detail_messagebox.png)
![[C#] Chia sẽ code tạo Text Effect trong lập trình Winform](https://laptrinhvb.net/uploads/source/vbnet/text_effect_thumb.jpg)
![[C#] Hướng dẫn thay đổi độ phân giải màn hình trong winform](https://laptrinhvb.net/uploads/source/csharp/change_resolution_screen_thumb.jpg)
![[C#] Hiển thị thông báo Toast Message trong lập trình Winform](https://laptrinhvb.net/uploads/source/csharp/toast_message_csharp.png)
![[C#] Empty Recycle Bin trong lập trình csharp](https://laptrinhvb.net/uploads/source/image_baiviet/5ffdaade6ca87e295ce10f4bcb715c04.jpg)
![[C#] Hướng dẫn khóa chuột và bàn phím trong lập trình Winform](https://laptrinhvb.net/uploads/source/csharp/lock_keyboard_thumb.png)
![[C#] Kiểm tra kết nối internet Realtime sử dụng Network List Manager của Windows](https://laptrinhvb.net/uploads/source/csharp/detect_internet_realtime_thumb.jpg)
![[C#] Hướng dẫn đọc tất cả email qua giao thức IMAP Gmail](https://laptrinhvb.net/uploads/source/new_image_baiviet/gmail_thumb.jpg)
![[C#] Hướng dẫn cấu hình địa chỉ IPAddress, SubnetMask, DefaultGetway, DNS Winform](https://laptrinhvb.net/uploads/source/vbnet/set_ipaddress_csharp.png)
![[C#] Hướng dẫn tạo mã tự động trên lập trình Winform](https://laptrinhvb.net/uploads/source/vbnet/AUTO_CODE.png)
![[C#] Hướng dẫn mở nhiều ứng dụng zalo trên Desktop](https://laptrinhvb.net/uploads/source/new_image_baiviet/zalo_instance_csharp.png)
![[C#] Hướng dẫn cách khởi động ứng dụng cùng Windows](https://laptrinhvb.net/uploads/source/csharp/starup_app_csharp_thumb.jpg)
![[C#] Sự kiện Reactive Observable Event Winform](https://laptrinhvb.net/uploads/source/csharp/observableEvent_thumb.png)

![[C#] Hướng dẫn lấy thông tin tỷ giá vàng SJC sử dụng API](https://laptrinhvb.net/uploads/source/vbnet/ty_gia_vang.png)
![[C#] Hướng dẫn sử dụng Fluent Interface Design Pattern](https://laptrinhvb.net/uploads/source/csharp/fluent_interface_thumb.png)
![[C#] Giới thiệu  thư viện Xander UI Framework dùng thiết kế Winform](https://laptrinhvb.net/uploads/source/csharp/XanderUIFramework_thumb.png)
![[C#] Chia sẽ class INIHelper dùng để ghi và đọc file config INI](https://laptrinhvb.net/uploads/source/web/config_thumb_ini.png)
![[C#] Hướng dẫn chạy ứng dụng EXE từ bộ nhớ Embedded Resource](https://laptrinhvb.net/uploads/source/vbnet/embbed_resource_csharp_thumb.jpg)
![[C#] Tìm giá trị trùng lắp và tô màu trên datagridview winform](https://laptrinhvb.net/uploads/source/devexpress/find_duplicate.png)
![[C#] Hướng dẫn sử dụng thư viện Simple Broker (Observer pattern)](https://laptrinhvb.net/uploads/source/csharp/Observer_pattern_thumb.png)
