• c# HttpClient 上传文件并带参


    public class Class1
         {
             private readonly string url = "https://*****";
             private readonly string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "lepUrla.jpg");//文件
             public void Test()
             {
                 string result = PostFileAsync(url, file).Result;
                 Console.WriteLine(result);
             }

            private async Task<string> PostFileAsync(string url, string file, int timeout = 180000, Encoding encoding = null, Dictionary<string, string> header = null)
             {
                 Dictionary<string, string> bodys = new Dictionary<string, string>
                 {
                     { "img_md5", "c12e2b751d238953d91feb0a9811479e" }, //参数1
                     { "merchant_no", "0000" },     //参数2
                 };

                HttpClientHandler handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate };
                 handler.AllowAutoRedirect = true;
                 handler.UseCookies = true;
                 handler.ClientCertificateOptions = ClientCertificateOption.Automatic;
                 ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };//解决https终止问题
                 HttpClient httpClient = new HttpClient(handler);
                 httpClient.DefaultRequestHeaders.Connection.Add("keep-alive");
                 httpClient.Timeout = TimeSpan.FromMilliseconds(timeout);
                 if (header != null)
                 {
                     foreach (KeyValuePair<string, string> item in header)
                     {
                         httpClient.DefaultRequestHeaders.Add(item.Key, item.Value);
                     }
                 }


                 using (MultipartFormDataContent content = new MultipartFormDataContent())
                 {
                     content.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(file)), "media", new FileInfo(file).Name);
                     if (bodys != null)
                     {
                         foreach (KeyValuePair<string, string> item in bodys)
                         {
                             //添加字符串参数,参数名为Key
                             content.Add(new StringContent(item.Value), item.Key);
                         }
                     }

                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url)
                     {
                         Content = content
                     };

                    HttpCompletionOption option = HttpCompletionOption.ResponseContentRead | HttpCompletionOption.ResponseHeadersRead;//解决:将内容复制到流时出错
                     HttpResponseMessage response = httpClient.SendAsync(request, option).Result;
                     return await response.Content.ReadAsStringAsync();

                }


             }

        }

  • 相关阅读:
    java之大文件断点续传
    Html5大文件断点续传
    前端js怎么实现大文件G级的断点续传(分块上传)和分段下载
    HTML5解决大文件断点续传
    完整版断点续传、秒传,支持超大大大文件_支持重定义文件名和路径
    起来吧!不要做奴隶的ITproject师们!
    Oracle PL/SQL 编程基础 实例
    C语言高速入门系列(八)
    POJ 3253-Fence Repair(堆)
    抽屉式导航可能减少产品一半的用户參与度
  • 原文地址:https://www.cnblogs.com/94cool/p/15060319.html
Copyright © 2020-2023  润新知