網站首頁 編程語言 正文
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-04-03 C++中的運算符重載詳解_C 語言
- 2022-08-17 C++詳解鏈棧的實現_C 語言
- 2022-03-15 el-form-item prop屬性動態綁定不生效
- 2022-11-22 Python網絡請求模塊urllib與requests使用介紹_python
- 2022-07-20 虛擬機下的/dev/sda1占用率為100%
- 2022-10-15 python-yml文件讀寫與xml文件讀寫_python
- 2022-11-08 goalng?結構體?方法集?接口實例詳解_Golang
- 2022-10-31 ?Go?語言實現?HTTP?文件上傳和下載_Golang
- 最近更新
-
- 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同步修改后的遠程分支