Hướng dẫn lấy data từ wensite bằng httprequest trong c năm 2024

CodeLearn is an online platform that helps users to learn, practice coding skills and join the online coding contests.

Links

Learning

Training

Fights

Information

About Us

Terms of Use

Help

Help

Discussion

Powered by CodeLearn © 2024. All Rights Reserved. rev 2/5/2024 5:31:56 PM

Lớp

// Khởi tạo http client using var httpClient = new HttpClient(); // Thiết lập các Header nếu cần httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json"); // Thực hiện truy vấn GET HttpResponseMessage response = await httpClient.GetAsync(url);

6 được sử dụng để gửi truy vấn HTTP (Http Request Message - Request) và nhận phản hồi Response (Http Response Message) từ các truy vấn đó. Lớp này thuộc namespace

// Khởi tạo http client using var httpClient = new HttpClient(); // Thiết lập các Header nếu cần httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json"); // Thực hiện truy vấn GET HttpResponseMessage response = await httpClient.GetAsync(url);

7, namespace này chứa các lớp giúp tạo ra sự liên lạc giữa client và server. Để làm việc với HttpClient dùng những namespace sau:

using System; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Threading; using System.Threading.Tasks; using System.IO; using System.Text;

Phần này thực hiện làm việc với giao thức HTTP nên trước tiên cần hiểu rõ về giao thức này, hãy đọc kỹ phần Giao thức HTTP và địa chỉ Uri, Url ở phần Cấu trúc Uri, Url,

Để tạo ra truy vấn GET tới một địa chỉ URL, thực hiện phương thức

// Khởi tạo http client using var httpClient = new HttpClient(); // Thiết lập các Header nếu cần httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json"); // Thực hiện truy vấn GET HttpResponseMessage response = await httpClient.GetAsync(url);

8, đây là phương thức

// Khởi tạo http client using var httpClient = new HttpClient(); // Thiết lập các Header nếu cần httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json"); // Thực hiện truy vấn GET HttpResponseMessage response = await httpClient.GetAsync(url);

9 khi kết thúc nó trả về đối tượng

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");  
    }  
 }  
Console.WriteLine();  
}

0. Từ đối tượng này ta sẽ biết kết quả truy vấn, và ta có thể đọc được dữ liệu tải về.

// Khởi tạo http client using var httpClient = new HttpClient(); // Thiết lập các Header nếu cần httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json"); // Thực hiện truy vấn GET HttpResponseMessage response = await httpClient.GetAsync(url);

Khi có đối tượng lớp

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");  
    }  
 }  
Console.WriteLine();  
}

0 có thể thực hiện các tác vụ đọc dữ liệu, tham khảo một số thuộc tính và phương thức:

Một số phương thức, thuộc tính lớp HttpResponseMessage HttpResponseHeaders

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");  
    }  
 }  
Console.WriteLine();  
}

2 Phát sinh Exception nếu truy vấn có mã trả về không thành công (khi thuộc tính

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");  
    }  
 }  
Console.WriteLine();  
}

3 là

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");  
    }  
 }  
Console.WriteLine();  
}

4)

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");  
    }  
 }  
Console.WriteLine();  
}

3 Nhận

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");  
    }  
 }  
Console.WriteLine();  
}

6 khi mã trả về thành công, ví dụ mã trạng thái

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");  
    }  
 }  
Console.WriteLine();  
}

7 nhận giá trị

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");  
    }  
 }  
Console.WriteLine();  
}

8

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");  
    }  
 }  
Console.WriteLine();  
}

7 Thuộc tính có kiểu enum

Content.Headers.ContentType.CharSet = @"utf-8"; //ISO-8859-1

0 cho biết mã trạng thái của kết quả (Như 301 - MovedPermanently, 200 - OK, 404 - NotFound ...), để chuyển sang số nguyên

Content.Headers.ContentType.CharSet = @"utf-8"; //ISO-8859-1

1

Content.Headers.ContentType.CharSet = @"utf-8"; //ISO-8859-1

2 Đoạn text, mô tả thông tin cho mã trạng thái như

Content.Headers.ContentType.CharSet = @"utf-8"; //ISO-8859-1

3,

Content.Headers.ContentType.CharSet = @"utf-8"; //ISO-8859-1

4 ...

Content.Headers.ContentType.CharSet = @"utf-8"; //ISO-8859-1

5

Thuộc tính kiểu

Content.Headers.ContentType.CharSet = @"utf-8"; //ISO-8859-1

6 chứa tập hợp các header (xem thêm ) của Response.

Content.Headers.ContentType.CharSet = @"utf-8"; //ISO-8859-1

6 kế thừa từ

Content.Headers.ContentType.CharSet = @"utf-8"; //ISO-8859-1

8 là tập hợp mà mỗi phần tử có kiểu

Content.Headers.ContentType.CharSet = @"utf-8"; //ISO-8859-1

9 (biểu diễn một header httpd - key là tên header và value là tập hợp các chuỗi giá trị của header)

Ví dụ xây dựng phương thực để hiện thị tất cả các header

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");  
    }  
 }  
Console.WriteLine();  
}

Ngoài ra nó còn có một số thuộc tính là các header thông dụng HTTP Respone như Age, Etag, Server ... mà nó đã phân tích các chuỗi header trả về thành các đối tượng tương ứng như TimeSpan, EntityTagHeaderValue, HttpHeaderValueCollection ... xem chi tiết các thuộc tính này tại

string content = System.Text.Encoding.UTF8.GetString(byteArray);

0 Thuộc tính kiểu

string content = System.Text.Encoding.UTF8.GetString(byteArray);

1, trong đó chứa nội dung (content) và các header liên quan đến content nếu có (Headers như Content-Type, Content-Length ...), một số phương thức để lấy content:

  • string content = System.Text.Encoding.UTF8.GetString(byteArray); 2 đọc nội dung (content) HTTP trả về chuỗi (có encoding). Đối với phương thức này, một số server trả về mã string content = System.Text.Encoding.UTF8.GetString(byteArray); 3 mà .NET Core không nhận ra sẽ gây lỗi, để encoding được cho trường hợp này thì trước khi thi hành thiết lập cho nó: Content.Headers.ContentType.CharSet = @"utf-8"; //ISO-8859-1
  • string content = System.Text.Encoding.UTF8.GetString(byteArray); 4 trả về đối tượng string content = System.Text.Encoding.UTF8.GetString(byteArray); 5, từ stream dùng kỹ thuật đọc luồng để đọc nội dung: xem thêm Dữ liệu đọc được là các byte, bạn có thể lưu chúng ra file hoặc convert thành chuỗi
  • string content = System.Text.Encoding.UTF8.GetString(byteArray); 6 đọc trả về mảng byte, từ mảng byte này có thể lưu nó ra file. Hoặc encoding nó thành chuỗi, ví dụ encoding utf-8 string content = System.Text.Encoding.UTF8.GetString(byteArray);

Ví dụ, tạo truy vấn GET, tải về trang Web - hiện thị các header và trả về content kết quả

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");
    }  
 }  
Console.WriteLine();  
} // Tải về trang web và trả về chuỗi nội dung public static async Task<string> GetWebContent(string url) {
// Khởi tạo http client  
using var httpClient = new HttpClient();
// Thiết lập các Header nếu cần  
httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json");  
try {  
    // Thực hiện truy vấn GET  
    HttpResponseMessage response = await **httpClient.GetAsync(url)**;
    // Hiện thị thông tin header trả về  
    **ShowHeaders(response.Headers);**
    // Phát sinh Exception nếu mã trạng thái trả về là lỗi  
    response.EnsureSuccessStatusCode();
    Console.WriteLine($"Tải thành công - statusCode {(int)response.StatusCode} {response.ReasonPhrase}");
    Console.WriteLine("Starting read data");
    // Đọc nội dung content trả về - ĐỌC CHUỖI NỘI DUNG  
    string htmltext = **await response.Content.ReadAsStringAsync()**;  
    Console.WriteLine($"Nhận được {htmltext.Length} ký tự");  
    Console.WriteLine();  
    return htmltext;
}  
catch (Exception ex)  
{  
    Console.WriteLine(ex.Message);  
    return null;  
}  
} Khi sử dụng, như trong phương thức Main (chú ý chuyển sang bất đồng bộ)

static async Task Main(string[] args) {

var c = await GetWebContent("https://www.google.com/search?q=xuanthulab");  
Console.WriteLine(c);  
}

Hướng dẫn lấy data từ wensite bằng httprequest trong c năm 2024

Mã nguồn tham khảo tại HttpClientExample hoặc tải về tại ex029-httpclient

ReadAsStreamAsync và ReadAsByteArrayAsync đọc nội dung

Ở ví dụ trên, đã dùng

string content = System.Text.Encoding.UTF8.GetString(byteArray);

7 để đọc content convert thành chuỗi (string). Ngoài ra, cũng có thể sử dụng

string content = System.Text.Encoding.UTF8.GetString(byteArray);

8 để tạo stream để đọc kết quả và sử dụng

string content = System.Text.Encoding.UTF8.GetString(byteArray);

6 để đọc hết các byte (mảng các byte) trong content.

Ví dụ sử dụng ReadAsByteArrayAsync

Xây dựng phương thức đọc tài nguyên từ một url và trả về mảng byte là content

var url = "https://raw.githubusercontent.com/xuanthulabnet/jekyll-example/master/images/jekyll-01.png"; byte[] bytes = await DownloadDataBytes(url); string filepath = "anh1.png"; using (var stream = new FileStream (filepath, FileMode.Create, FileAccess.Write, FileShare.None)) {

stream.Write (bytes, 0, bytes.Length);  
Console.WriteLine("save " + filepath);  
} Khi sủ dụng, có thể lưu toàn bộ dữ liệu mảng byte ra file. Ví dụ tải một file ảnh về:

static async Task Main(string[] args) {

var url = "https://raw.githubusercontent.com/xuanthulabnet/jekyll-example/master/images/jekyll-01.png";  
byte[] bytes = await DownloadDataBytes(url);
string filepath = "anh1.png";  
using (var stream = new FileStream (filepath, FileMode.Create, FileAccess.Write, FileShare.None))  
{  
    stream.Write (bytes, 0, bytes.Length);  
    Console.WriteLine("save " + filepath);  
}  
}

Ví dụ sử dụng DownloadDataStream

DownloadDataStream trả về một Stream để đọc Content, ví dụ xây dựng phương thức tải một file ảnh từ url cho trước.

// Tải từ url, trả về stream để đọc dữ liệu (xem bài về stream) public static async Task DownloadDataStream (string url, string filename) {

var httpClient = new HttpClient();  
Console.WriteLine ($"Starting connect {url}");  
try {  
    HttpResponseMessage response = await httpClient.GetAsync (url);  
    response.EnsureSuccessStatusCode ();
    // Lấy Stream để đọc content  
    using var stream =  await response.Content.ReadAsStreamAsync();
    // THỰC HIỆN ĐỌC Content  
    int SIZEBUFFER = 500;  
    using var streamwrite = File.OpenWrite (filename);  // Mở stream để lưu file  
    byte[] buffer = new byte[SIZEBUFFER];               // tạo bộ nhớ đệm lưu dữ liệu khi đọc stream
    bool endread = false;  
    do                                                  // thực hiện đọc các byte từ stream và lưu ra streamwrite  
    {  
        int numberRead = await stream.ReadAsync(buffer, 0, SIZEBUFFER);  
        Console.WriteLine(numberRead);  
        if (numberRead == 0)  
        {  
            endread = true;  
        }  
        else  
        {  
            await streamwrite.WriteAsync(buffer, 0, numberRead);  
        }
    } while (!endread);  
    Console.WriteLine ("Download success");
}  
catch (Exception e) {  
    Console.WriteLine (e.Message);  
    throw e;  
}  
} Khi áp dụng

// Khởi tạo http client using var httpClient = new HttpClient(); // Thiết lập các Header nếu cần httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json"); // Thực hiện truy vấn GET HttpResponseMessage response = await httpClient.GetAsync(url);

0

Tham khảo mã nguồn HttpClientExampleRead (git) hoặc tải về ex029-2

Tạo request với SendAsync

Ngoài phương thức

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");
    }  
 }  
Console.WriteLine();  
} // Tải về trang web và trả về chuỗi nội dung public static async Task<string> GetWebContent(string url) {
// Khởi tạo http client  
using var httpClient = new HttpClient();
// Thiết lập các Header nếu cần  
httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json");  
try {  
    // Thực hiện truy vấn GET  
    HttpResponseMessage response = await **httpClient.GetAsync(url)**;
    // Hiện thị thông tin header trả về  
    **ShowHeaders(response.Headers);**
    // Phát sinh Exception nếu mã trạng thái trả về là lỗi  
    response.EnsureSuccessStatusCode();
    Console.WriteLine($"Tải thành công - statusCode {(int)response.StatusCode} {response.ReasonPhrase}");
    Console.WriteLine("Starting read data");
    // Đọc nội dung content trả về - ĐỌC CHUỖI NỘI DUNG  
    string htmltext = **await response.Content.ReadAsStringAsync()**;  
    Console.WriteLine($"Nhận được {htmltext.Length} ký tự");  
    Console.WriteLine();  
    return htmltext;
}  
catch (Exception ex)  
{  
    Console.WriteLine(ex.Message);  
    return null;  
}  
} 0 gửi Request với phương thức GET ở trên ra, có thể dùng phương thức

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");
    }  
 }  
Console.WriteLine();  
} // Tải về trang web và trả về chuỗi nội dung public static async Task<string> GetWebContent(string url) {
// Khởi tạo http client  
using var httpClient = new HttpClient();
// Thiết lập các Header nếu cần  
httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json");  
try {  
    // Thực hiện truy vấn GET  
    HttpResponseMessage response = await **httpClient.GetAsync(url)**;
    // Hiện thị thông tin header trả về  
    **ShowHeaders(response.Headers);**
    // Phát sinh Exception nếu mã trạng thái trả về là lỗi  
    response.EnsureSuccessStatusCode();
    Console.WriteLine($"Tải thành công - statusCode {(int)response.StatusCode} {response.ReasonPhrase}");
    Console.WriteLine("Starting read data");
    // Đọc nội dung content trả về - ĐỌC CHUỖI NỘI DUNG  
    string htmltext = **await response.Content.ReadAsStringAsync()**;  
    Console.WriteLine($"Nhận được {htmltext.Length} ký tự");  
    Console.WriteLine();  
    return htmltext;
}  
catch (Exception ex)  
{  
    Console.WriteLine(ex.Message);  
    return null;  
}  
} 1 (hoặc

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");
    }  
 }  
Console.WriteLine();  
} // Tải về trang web và trả về chuỗi nội dung public static async Task<string> GetWebContent(string url) {
// Khởi tạo http client  
using var httpClient = new HttpClient();
// Thiết lập các Header nếu cần  
httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json");  
try {  
    // Thực hiện truy vấn GET  
    HttpResponseMessage response = await **httpClient.GetAsync(url)**;
    // Hiện thị thông tin header trả về  
    **ShowHeaders(response.Headers);**
    // Phát sinh Exception nếu mã trạng thái trả về là lỗi  
    response.EnsureSuccessStatusCode();
    Console.WriteLine($"Tải thành công - statusCode {(int)response.StatusCode} {response.ReasonPhrase}");
    Console.WriteLine("Starting read data");
    // Đọc nội dung content trả về - ĐỌC CHUỖI NỘI DUNG  
    string htmltext = **await response.Content.ReadAsStringAsync()**;  
    Console.WriteLine($"Nhận được {htmltext.Length} ký tự");  
    Console.WriteLine();  
    return htmltext;
}  
catch (Exception ex)  
{  
    Console.WriteLine(ex.Message);  
    return null;  
}  
} 2 nếu sử dụng code đồng bộ synchronous).

Phương thức này có tham số kiểu

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");
    }  
 }  
Console.WriteLine();  
} // Tải về trang web và trả về chuỗi nội dung public static async Task<string> GetWebContent(string url) {
// Khởi tạo http client  
using var httpClient = new HttpClient();
// Thiết lập các Header nếu cần  
httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json");  
try {  
    // Thực hiện truy vấn GET  
    HttpResponseMessage response = await **httpClient.GetAsync(url)**;
    // Hiện thị thông tin header trả về  
    **ShowHeaders(response.Headers);**
    // Phát sinh Exception nếu mã trạng thái trả về là lỗi  
    response.EnsureSuccessStatusCode();
    Console.WriteLine($"Tải thành công - statusCode {(int)response.StatusCode} {response.ReasonPhrase}");
    Console.WriteLine("Starting read data");
    // Đọc nội dung content trả về - ĐỌC CHUỖI NỘI DUNG  
    string htmltext = **await response.Content.ReadAsStringAsync()**;  
    Console.WriteLine($"Nhận được {htmltext.Length} ký tự");  
    Console.WriteLine();  
    return htmltext;
}  
catch (Exception ex)  
{  
    Console.WriteLine(ex.Message);  
    return null;  
}  
} 3 chứa các thông tin về một HTTP Request sẽ gửi đi (xem thêm ), giúp tùy biến, thêm được nhiều thông tin khi gửi request hơn. Ví dụ tạo Request:

// Khởi tạo http client using var httpClient = new HttpClient(); // Thiết lập các Header nếu cần httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json"); // Thực hiện truy vấn GET HttpResponseMessage response = await httpClient.GetAsync(url);

1

Sau khi tạo được HttpRequestMessage có thể thiết lập nhiều thông tin như, thiết lập method với thuộc tính

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");
    }  
 }  
Console.WriteLine();  
} // Tải về trang web và trả về chuỗi nội dung public static async Task<string> GetWebContent(string url) {
// Khởi tạo http client  
using var httpClient = new HttpClient();
// Thiết lập các Header nếu cần  
httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json");  
try {  
    // Thực hiện truy vấn GET  
    HttpResponseMessage response = await **httpClient.GetAsync(url)**;
    // Hiện thị thông tin header trả về  
    **ShowHeaders(response.Headers);**
    // Phát sinh Exception nếu mã trạng thái trả về là lỗi  
    response.EnsureSuccessStatusCode();
    Console.WriteLine($"Tải thành công - statusCode {(int)response.StatusCode} {response.ReasonPhrase}");
    Console.WriteLine("Starting read data");
    // Đọc nội dung content trả về - ĐỌC CHUỖI NỘI DUNG  
    string htmltext = **await response.Content.ReadAsStringAsync()**;  
    Console.WriteLine($"Nhận được {htmltext.Length} ký tự");  
    Console.WriteLine();  
    return htmltext;
}  
catch (Exception ex)  
{  
    Console.WriteLine(ex.Message);  
    return null;  
}  
} 4 (giá trị

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");
    }  
 }  
Console.WriteLine();  
} // Tải về trang web và trả về chuỗi nội dung public static async Task<string> GetWebContent(string url) {
// Khởi tạo http client  
using var httpClient = new HttpClient();
// Thiết lập các Header nếu cần  
httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json");  
try {  
    // Thực hiện truy vấn GET  
    HttpResponseMessage response = await **httpClient.GetAsync(url)**;
    // Hiện thị thông tin header trả về  
    **ShowHeaders(response.Headers);**
    // Phát sinh Exception nếu mã trạng thái trả về là lỗi  
    response.EnsureSuccessStatusCode();
    Console.WriteLine($"Tải thành công - statusCode {(int)response.StatusCode} {response.ReasonPhrase}");
    Console.WriteLine("Starting read data");
    // Đọc nội dung content trả về - ĐỌC CHUỖI NỘI DUNG  
    string htmltext = **await response.Content.ReadAsStringAsync()**;  
    Console.WriteLine($"Nhận được {htmltext.Length} ký tự");  
    Console.WriteLine();  
    return htmltext;
}  
catch (Exception ex)  
{  
    Console.WriteLine(ex.Message);  
    return null;  
}  
} 5,

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");
    }  
 }  
Console.WriteLine();  
} // Tải về trang web và trả về chuỗi nội dung public static async Task<string> GetWebContent(string url) {
// Khởi tạo http client  
using var httpClient = new HttpClient();
// Thiết lập các Header nếu cần  
httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json");  
try {  
    // Thực hiện truy vấn GET  
    HttpResponseMessage response = await **httpClient.GetAsync(url)**;
    // Hiện thị thông tin header trả về  
    **ShowHeaders(response.Headers);**
    // Phát sinh Exception nếu mã trạng thái trả về là lỗi  
    response.EnsureSuccessStatusCode();
    Console.WriteLine($"Tải thành công - statusCode {(int)response.StatusCode} {response.ReasonPhrase}");
    Console.WriteLine("Starting read data");
    // Đọc nội dung content trả về - ĐỌC CHUỖI NỘI DUNG  
    string htmltext = **await response.Content.ReadAsStringAsync()**;  
    Console.WriteLine($"Nhận được {htmltext.Length} ký tự");  
    Console.WriteLine();  
    return htmltext;
}  
catch (Exception ex)  
{  
    Console.WriteLine(ex.Message);  
    return null;  
}  
} 6,

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");
    }  
 }  
Console.WriteLine();  
} // Tải về trang web và trả về chuỗi nội dung public static async Task<string> GetWebContent(string url) {
// Khởi tạo http client  
using var httpClient = new HttpClient();
// Thiết lập các Header nếu cần  
httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json");  
try {  
    // Thực hiện truy vấn GET  
    HttpResponseMessage response = await **httpClient.GetAsync(url)**;
    // Hiện thị thông tin header trả về  
    **ShowHeaders(response.Headers);**
    // Phát sinh Exception nếu mã trạng thái trả về là lỗi  
    response.EnsureSuccessStatusCode();
    Console.WriteLine($"Tải thành công - statusCode {(int)response.StatusCode} {response.ReasonPhrase}");
    Console.WriteLine("Starting read data");
    // Đọc nội dung content trả về - ĐỌC CHUỖI NỘI DUNG  
    string htmltext = **await response.Content.ReadAsStringAsync()**;  
    Console.WriteLine($"Nhận được {htmltext.Length} ký tự");  
    Console.WriteLine();  
    return htmltext;
}  
catch (Exception ex)  
{  
    Console.WriteLine(ex.Message);  
    return null;  
}  
} 7 ...), thiết lập địa chỉ truy vấn với thuộc tính

/// In ra thông tin các Header của HTTP Response public static void ShowHeaders(HttpHeaders headers) {

Console.WriteLine("CÁC HEADER:");  
foreach (var header in headers)  
{  
    foreach (var value in header.Value)  
    {  
        Console.WriteLine($"{header.Key,25} : {value}");
    }  
 }  
Console.WriteLine();  
} // Tải về trang web và trả về chuỗi nội dung public static async Task<string> GetWebContent(string url) {
// Khởi tạo http client  
using var httpClient = new HttpClient();
// Thiết lập các Header nếu cần  
httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json");  
try {  
    // Thực hiện truy vấn GET  
    HttpResponseMessage response = await **httpClient.GetAsync(url)**;
    // Hiện thị thông tin header trả về  
    **ShowHeaders(response.Headers);**
    // Phát sinh Exception nếu mã trạng thái trả về là lỗi  
    response.EnsureSuccessStatusCode();
    Console.WriteLine($"Tải thành công - statusCode {(int)response.StatusCode} {response.ReasonPhrase}");
    Console.WriteLine("Starting read data");
    // Đọc nội dung content trả về - ĐỌC CHUỖI NỘI DUNG  
    string htmltext = **await response.Content.ReadAsStringAsync()**;  
    Console.WriteLine($"Nhận được {htmltext.Length} ký tự");  
    Console.WriteLine();  
    return htmltext;
}  
catch (Exception ex)  
{  
    Console.WriteLine(ex.Message);  
    return null;  
}  
} 8, thiết lập Content của truy vấn với

string content = System.Text.Encoding.UTF8.GetString(byteArray);

0 ...

Sau đó có thể thực hiện truy vấn để trả về HttpResponseMessage

// Khởi tạo http client using var httpClient = new HttpClient(); // Thiết lập các Header nếu cần httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json"); // Thực hiện truy vấn GET HttpResponseMessage response = await httpClient.GetAsync(url);

2

Sử dụng FormUrlEncodedContent

Để thiết lập Content trong HttpRequestMessage để gửi đến server thì thiết lập thuộc tính Content bằng các đối tượng thuộc các lớp như:

static async Task Main(string[] args) {

var c = await GetWebContent("https://www.google.com/search?q=xuanthulab");  
Console.WriteLine(c);  
}

0,

static async Task Main(string[] args) {

var c = await GetWebContent("https://www.google.com/search?q=xuanthulab");  
Console.WriteLine(c);  
}

1,

static async Task Main(string[] args) {

var c = await GetWebContent("https://www.google.com/search?q=xuanthulab");  
Console.WriteLine(c);  
}

2

Với

static async Task Main(string[] args) {

var c = await GetWebContent("https://www.google.com/search?q=xuanthulab");  
Console.WriteLine(c);  
}

0 bạn có thể tạo Content tương ứng như một Form HTML, nó chứa các giá trị (key/value) sẽ Post đến Server. Ví dụ sau, nó post đến server hai giá trị tương ứng key và value là key1/value1 (có thể hiểu tương ứng với phần tử HTML Input có name là key1 và value là value1) và key2/value2 (trường hợp này chứa nhiều giá trị, tương ứng với HTML Multi Select)

// Khởi tạo http client using var httpClient = new HttpClient(); // Thiết lập các Header nếu cần httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json"); // Thực hiện truy vấn GET HttpResponseMessage response = await httpClient.GetAsync(url);

3

Sử dụng StringContent

Có thể gán Content bằng đối tượng kiểu StringContent chứa chuỗi nội dung (sử dụng cách này để tạo truy vấn Webservice API với Content là JSON)

// Khởi tạo http client using var httpClient = new HttpClient(); // Thiết lập các Header nếu cần httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json"); // Thực hiện truy vấn GET HttpResponseMessage response = await httpClient.GetAsync(url);

4

Sử dụng MultipartFormDataContent

Bạn có thể gán Content bằng đối tượng kiểu MultipartFormDataContent, để post dữ liệu nhiều thành phần (multipart/form-data), vừa có dữ liệu như các phần tử của HTML Form vừa có thể kèm file.

Ví dụ, tạo ra truy vấn tương tự form HTML post các dữ liệu: có file đính kèm, có hai phần tử

// Khởi tạo http client using var httpClient = new HttpClient(); // Thiết lập các Header nếu cần httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json"); // Thực hiện truy vấn GET HttpResponseMessage response = await httpClient.GetAsync(url);

5

Tham khảo mã nguồn HttpClientExampleSendAsync (git) hoặc tải về ex031