網站首頁 編程語言 正文
HttpClient Post參數同時上傳文件
Demo 如下
using (var client = new HttpClient())
{
? ? using (var multipartFormDataContent = new MultipartFormDataContent())
? ? {
? ? ? ? var values = new[]
? ? ? ? {
? ? ? ? ? ? new KeyValuePair<string, string>("c", "3"),
? ? ? ? ? ? new KeyValuePair<string, string>("c", "2"),
? ? ? ? ? ? new KeyValuePair<string, string>("d", "2")
? ? ? ? ? ? ?//other values
? ? ? ? };
?
? ? ? ? foreach (var keyValuePair in values)
? ? ? ? {
? ? ? ? ? ? multipartFormDataContent.Add(new StringContent(keyValuePair.Value),
? ? ? ? ? ? ? ? String.Format("\"{0}\"", keyValuePair.Key));
? ? ? ? }
?
? ? ? ? multipartFormDataContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(@"D:\test.jpg")),
? ? ? ? ? ? "\"pic\"",
? ? ? ? ? ? "\"test.jpg\"");
?
? ? ? ? var requestUri = "http://localhost:8080";
? ? ? ? var html = client.PostAsync(requestUri, multipartFormDataContent).Result.Content.ReadAsStringAsync().Result;
? ? }
}
HttpClient上傳文件到服務器(multipart/form-data)
string reqUrl = JsonrpcHttpClient.MakeRpcUrl(typeof(Wfm_SimReport).Name, "save");
using (HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false }))//若想手動設置Cookie則必須設置UseCookies = false
{
string boundary = string.Format("----WebKitFormBoundary{0}",DateTime.Now.Ticks.ToString("x"));
MultipartFormDataContent content = new MultipartFormDataContent(boundary);
#region 設置請求參數
content.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
content.Headers.Add("Cookie", TokenManager.Token);
content.Headers.Add("client", "true");
#endregion
if (string.IsNullOrEmpty(fullPath) && !File.Exists(fullPath))
{
return false;
}
string fileName = Path.GetFileName(fullPath);
#region Stream請求
FileStream fStream = File.Open(fullPath, FileMode.Open, FileAccess.Read);
content.Add(new StreamContent(fStream, (int)fStream.Length), "file", fileName);
#endregion
content.Add(new StringContent(JsonHelper.Serialize(entity)), "dtoStr");
var result = client.PostAsync(reqUrl, content).Result;
try
{
if (result.IsSuccessStatusCode)
{
string rslt = result.Content.ReadAsStringAsync().Result;
Debug.WriteLine(rslt);
return true;
}
}
catch (Exception ex)
{
Debug.WriteLine(string.Format("獲取服務器返回結果錯誤:消息:{0},堆棧:{1}",ex.Message,ex.StackTrace));
}
finally
{
//關閉文件流
fStream.Close();
client.Dispose();
}
}
原文鏈接:https://blog.csdn.net/luofeng0710/article/details/84202008
相關推薦
- 2022-07-14 android實現多點觸摸效果_Android
- 2022-04-09 C#8.0中的索引與范圍功能介紹_C#教程
- 2022-05-24 redis中的bitmap你了解嗎_Redis
- 2022-01-17 計算屬性 computed與 watch 的區別
- 2022-07-13 淺談Redis中的自動過期機制_Redis
- 2022-12-01 Go語言中基本數據類型的相互轉換詳解_Golang
- 2023-02-05 Android?開發與代碼無關技巧詳解_Android
- 2022-06-12 C語言詳解熱門考點結構體內存對齊_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支