HttpClient client = new HttpClient();
MultipartFormDataContent content = new MultipartFormDataContent();
//文件内容
StreamContent file1 = new StreamContent(File.Open("D:/1.txt", FileMode.Open));
//表示传的文件
file1.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
file1.Headers.ContentDisposition.FileName = "1.txt"; //文件名
file1.Headers.ContentDisposition.Name = "file1"; //key的名字
//将文件附加到content中 多个文件同理
content.Add(file1);
//上传文本信息
HttpContent strParam = new StringContent("asdasdadsa");
strParam.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
strParam.Headers.ContentDisposition.Name = "a";
content.Add(strParam);
var resu = client.PostAsync("http://localhost:2965/WebForm1.aspx", content).Result.Content.ReadAsStringAsync().Result;
HttpClient 上传文件
最新推荐文章于 2024-06-06 22:00:00 发布