最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# HttpClient上傳文件并附帶其它參數(shù)方式

 更新時間:2023年11月07日 14:48:14   作者:csdmwinter  
這篇文章主要介紹了C# HttpClient上傳文件并附帶其它參數(shù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

1、Fiddler抓包參數(shù)詳情

Content-Type: multipart/form-data; boundary="8d9ade1fd906a6a"
Content-Length: 39356
 
--8d9ade1fd906a6a
Content-Type: application/x-jpg
Content-Disposition: form-data; name=file; filename=test.jpg
 
...文件二進制流
--8d9ade1fd906a6a
Content-Disposition: form-data; name=toes
 
123@qq.com
--8d9ade1fd906a6a
Content-Disposition: form-data; name=subject
 
測試發(fā)送附件
--8d9ade1fd906a6a
Content-Disposition: form-data; name=Body
 
測試HttpClient發(fā)送文件+額外參數(shù)請求
--8d9ade1fd906a6a

2、使用MultipartFormDataContent封裝文件和其它參數(shù)

上傳文件時Content-Type是multipart/form-data,并且其它額外參數(shù)都要標識form-data

  • 2.1、把文件轉(zhuǎn)成ByteArrayContent
        private static ByteArrayContent CreateByteArrayContent(string key, string fileName, string fileContent,
            byte[] fileBytes)
        {
            var fileByteArrayContent = new ByteArrayContent(fileBytes);
            fileByteArrayContent.Headers.ContentType = new MediaTypeHeaderValue(fileContent);
            fileByteArrayContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                Name = key, //接口匹配name
                FileName = fileName //附件文件名
            };
            return fileByteArrayContent;
        }
  • 2.2、把其它附件參數(shù)轉(zhuǎn)成ByteArrayContent
        private static List<ByteArrayContent> CreateParamsByteArrayContent(IDictionary<string, string> dic)
        {
            var list = new List<ByteArrayContent>();
            if (dic == null || dic.Count == 0) return list;
            foreach (var (key, value) in dic)
            {
                var valueBytes = Encoding.UTF8.GetBytes(value);
                var byteArray = new ByteArrayContent(valueBytes);
                byteArray.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                {
                    Name = key
                };
                list.Add(byteArray);
            }
 
            return list;
        }
  • 2.3、構(gòu)建MultipartFormDataContent
        private static MultipartFormDataContent CreateContent(byte[] bytes,IDictionary<string,string> addParams)
        {
            var strBoundary = DateTime.Now.Ticks.ToString("x"); //分隔符
 
            var resultContent = new MultipartFormDataContent(strBoundary);
            //文件
            var fileByteContent = CreateByteArrayContent("file", "test.jpg", "application/x-jpg", bytes);
            resultContent.Add(fileByteContent);
            //其它附加參數(shù)
            var paramsByteContent = CreateParamsByteArrayContent(addParams);
            paramsByteContent.ForEach(el =>
            {
                resultContent.Add(el);
            });
            return resultContent;
        }

3、發(fā)送請求

        static async Task Main(string[] args)
        {
 
            var dic = new Dictionary<string, string>
            {
                {"toes", "111@qq.com"},
                {"subject", "測試發(fā)送附件"},
                {"Body", "測試HttpClient發(fā)送文件+額外參數(shù)請求"},
            };
 
            var filePath = @"d:\image\shui.jpg";
            using (var fileStream = File.Open(filePath, FileMode.Open))
            {
                var bytes = new byte[fileStream.Length];
                fileStream.Read(bytes, 0, bytes.Length);
                using (var client = new HttpClient())
                {
                    var message = new HttpRequestMessage(HttpMethod.Post, "https://xxx/email/SendEmailIncludeAttach");
                    message.Content = CreateContent(bytes, dic);
                    var responseMessage = await client.SendAsync(message);
                    if (responseMessage.IsSuccessStatusCode)
                    {
                        var resStr = await responseMessage.Content.ReadAsStringAsync();
                        Console.WriteLine(resStr);
                    }
                }
            }
 
            Console.ReadKey();
 
        }

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

墨竹工卡县| 理塘县| 天门市| 开平市| 卫辉市| 邯郸市| 界首市| 清新县| 江陵县| 利辛县| 明光市| 荥经县| 南岸区| 资溪县| 昌黎县| 凉城县| 抚松县| 河西区| 盐山县| 巴彦县| 若羌县| 桃园市| 谷城县| 保定市| 沧源| 深水埗区| 蒙山县| 武冈市| 庐江县| 洛隆县| 西乌珠穆沁旗| 临海市| 萝北县| 都江堰市| 同心县| 巫山县| 鹿邑县| 宁陕县| 朔州市| 石城县| 平乡县|