博客
关于我
C#httpclient
阅读量:271 次
发布时间:2019-03-01

本文共 2120 字,大约阅读时间需要 7 分钟。

 后台数据接口:

[HttpPost]        public Response
> LoadSchedule([FromForm]QueryScheduleOneListReq query) { var result = new Response
>(); try { result.Result = _scheduleApp.Load(query); } catch (Exception ex) { result.Code = 500; result.Message = ex.InnerException?.Message ?? ex.Message; } return result; }

前台请求格式:

public static async Task
PostAsync(string url,string data,Dictionary
header=null,bool Gzip = false) { try { HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false }); HttpContent content = new StringContent(data); content.Headers.ContentType=new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data"); if (header != null) { client.DefaultRequestHeaders.Clear(); foreach (var item in header) { client.DefaultRequestHeaders.Add(item.Key, item.Value); } } HttpResponseMessage response = await client.PostAsync(url, content); response.EnsureSuccessStatusCode(); string responseBody = ""; if (Gzip) { GZipInputStream inputStream = new GZipInputStream(await response.Content.ReadAsStreamAsync()); responseBody = new StreamReader(inputStream).ReadToEnd(); } else { responseBody = await response.Content.ReadAsStringAsync(); } return responseBody; } catch (Exception ex) { throw; } return null; }

 第一个问题请求的时候会抛出异常,把 content.Headers.ContentType=new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data");这行代码去掉,异常就没有了。

第二个问题就是前台的数据提交不上去

 

转载地址:http://ipua.baihongyu.com/

你可能感兴趣的文章
mysql中实现rownum,对结果进行排序
查看>>
mysql中对于数据库的基本操作
查看>>
Mysql中常用函数的使用示例
查看>>
MySql中怎样使用case-when实现判断查询结果返回
查看>>
Mysql中怎样使用update更新某列的数据减去指定值
查看>>
Mysql中怎样设置指定ip远程访问连接
查看>>
mysql中数据表的基本操作很难嘛,由这个实验来带你从头走一遍
查看>>
Mysql中文乱码问题完美解决方案
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
Mysql中的 IFNULL 函数的详解
查看>>
mysql中的collate关键字是什么意思?
查看>>
MySql中的concat()相关函数
查看>>
mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
查看>>
MySQL中的count函数
查看>>
MySQL中的DB、DBMS、SQL
查看>>
MySQL中的DECIMAL类型:MYSQL_TYPE_DECIMAL与MYSQL_TYPE_NEWDECIMAL详解
查看>>
MySQL中的GROUP_CONCAT()函数详解与实战应用
查看>>
MySQL中的IO问题分析与优化
查看>>
MySQL中的ON DUPLICATE KEY UPDATE详解与应用
查看>>
mysql中的rbs,SharePoint RBS:即使启用了RBS,内容数据库也在不断增长
查看>>