LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

C# HttpClient全攻略:GET、POST、文件傳輸與授權設置一網打盡

admin
2024年1月23日 16:53 本文熱度 759

概述:本文詳解了在C#中使用HttpClient發送HTTP請求的全面指南,包括GET、POST請求、文件上傳和下載,以及設置Authorization、cookie等操作,為實現網絡操作提供了清晰的方法和示例。

1. 說明

HttpClient是C#中用于發送HTTP請求的類,基于HttpClient的異步模型,可以實現GET、POST請求,處理響應數據,上傳和下載文件,以及設置Authorization、cookie等。

2. 方法說明

2.1 發送GET請求

using (HttpClient client = new HttpClient())
{
    HttpResponseMessage response = await client.GetAsync("https://example.com/api/resource");    // 處理響應
}

2.2 發送POST請求

using (HttpClient client = new HttpClient())
{
    var content = new StringContent("data to send", Encoding.UTF8, "application/json");
    HttpResponseMessage response = await client.PostAsync("https://example.com/api/resource", content);    // 處理響應
}

2.3 上傳文件

using (HttpClient client = new HttpClient())
{
    var content = new MultipartFormDataContent();
    content.Add(new ByteArrayContent(File.ReadAllBytes("file.txt")), "file", "file.txt");
    HttpResponseMessage response = await client.PostAsync("https://example.com/api/upload", content);    // 處理響應
}

2.4 下載文件

using (HttpClient client = new HttpClient())
{
    byte[] fileData = await client.GetByteArrayAsync("https://example.com/api/download"); 
    File.WriteAllBytes("downloaded_file.txt", fileData);
}

2.5 設置Authorization

using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "your_access_token");
    HttpResponseMessage response = await client.GetAsync("https://example.com/api/resource");    // 處理響應
}

2.6 設置Cookie

using (HttpClient client = new HttpClient())
{
    var cookieContainer = new CookieContainer();
    var handler = new HttpClientHandler { CookieContainer = cookieContainer };
    using (var httpClient = new HttpClient(handler))
    {
        HttpResponseMessage response = await client.GetAsync("https://example.com/api/resource");        // 處理響應
    }
}

3. 步驟說明

3.1 發送GET請求

  1. 創建HttpClient實例。

  2. 使用GetAsync方法發送GET請求。

  3. 處理HttpResponseMessage以獲取響應數據。

3.2 發送POST請求

  1. 創建HttpClient實例。

  2. 創建StringContentFormUrlEncodedContent作為請求內容。

  3. 使用PostAsync方法發送POST請求。

  4. 處理HttpResponseMessage以獲取響應數據。

3.3 上傳文件

  1. 創建HttpClient實例。

  2. 創建MultipartFormDataContent,添加ByteArrayContent作為文件內容。

  3. 使用PostAsync方法發送包含文件的請求。

  4. 處理HttpResponseMessage以獲取響應數據。

3.4 下載文件

  1. 創建HttpClient實例。

  2. 使用GetByteArrayAsync方法獲取文件的字節數組。

  3. 使用File.WriteAllBytes保存文件。

3.5 設置Authorization

  1. 創建HttpClient實例。

  2. 設置DefaultRequestHeaders.Authorization屬性為AuthenticationHeaderValue

3.6 設置Cookie

  1. 創建HttpClient實例。

  2. 創建CookieContainerHttpClientHandler實例。

  3. HttpClientHandler中設置CookieContainer

  4. 使用包含HttpClientHandlerHttpClient實例發送請求。

4. 實例源代碼

// 示例代碼請根據實際情況修改URL和文件路徑
// 發送GET請求
using (HttpClient client = new HttpClient())
{
    HttpResponseMessage response = await client.GetAsync("https://example.com/api/resource");    // 處理響應
}
// 發送POST請求
using (HttpClient client = new HttpClient())
{
    var content = new StringContent("data to send", Encoding.UTF8, "application/json");
    HttpResponseMessage response = await client.PostAsync("https://example.com/api/resource", content);    // 處理響應
}
// 上傳文件
using (HttpClient client = new HttpClient())
{
    var content = new MultipartFormDataContent();
    content.Add(new ByteArrayContent(File.ReadAllBytes("file.txt")), "file", "file.txt");
    HttpResponseMessage response = await client.PostAsync("https://example.com/api/upload", content);    // 處理響應
}
// 下載文件
using (HttpClient client = new HttpClient())
{
    byte[] fileData = await client.GetByteArrayAsync(" 
    File.WriteAllBytes("downloaded_file.txt", fileData);
}
// 設置
Authorizationusing (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "your_access_token");
    HttpResponseMessage response = await client.GetAsync("https://example.com/api/resource");    // 處理響應
}
// 設置
Cookieusing (HttpClient client = new HttpClient())
{
    var cookieContainer = new CookieContainer();
    var handler = new HttpClientHandler { CookieContainer = cookieContainer };
    using (var httpClient = new HttpClient(handler))
    {
        HttpResponseMessage response = await client.GetAsync("https://example.com/api/resource");        // 處理響應
    }
}

5. 注意事項及建議

  • 使用using語句確保及時釋放HttpClient資源。

  • 異步方法應避免在同步上下文中使用,以提高性能。

  • 注意處理HTTP響應狀態碼和異常,確保請求的正確執行。

  • 文件上傳和下載時,考慮使用流式傳輸以節省內存。

通過HttpClient,我們能夠在C#中方便地進行HTTP請求操作,包括GET、POST請求、文件上傳和下載,以及設置Authorization、cookie等。確保使用異步操作和適當的異常處理,能夠保證請求的穩定性和性能。


該文章在 2024/1/23 18:47:51 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
亚洲AV永久综合在线观看另类 | 亚洲日本一区二区 | 最新久久免费网址 | 日韩午夜精品一区二区三区 | 最新国产在线不卡AV | 亚洲精品免费在线观看 |