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