C# HttpClient全攻略:GET、POST、文件傳輸與授權設置一網(wǎng)打盡
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
概述:本文詳解了在C#中使用HttpClient發(fā)送HTTP請求的全面指南,包括GET、POST請求、文件上傳和下載,以及設置Authorization、cookie等操作,為實現(xiàn)網(wǎng)絡操作提供了清晰的方法和示例。 1. 說明
2. 方法說明2.1 發(fā)送GET請求using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync("https://example.com/api/resource"); // 處理響應 } 2.2 發(fā)送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 設置Authorizationusing (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 設置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"); // 處理響應 } } 3. 步驟說明3.1 發(fā)送GET請求
3.2 發(fā)送POST請求
3.3 上傳文件
3.4 下載文件
3.5 設置Authorization
3.6 設置Cookie
4. 實例源代碼// 示例代碼請根據(jù)實際情況修改URL和文件路徑 // 發(fā)送GET請求 using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync("https://example.com/api/resource"); // 處理響應 } // 發(fā)送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. 注意事項及建議
通過 該文章在 2024/1/23 18:47:51 編輯過 |
關鍵字查詢
相關文章
正在查詢... |