博客
关于我
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/

你可能感兴趣的文章
Navicat报错:1045-Access denied for user root@localhost(using passwordYES)
查看>>
Navicat控制mysql用户权限
查看>>
navicat操作mysql中某一张表后, 读表时一直显示正在载入,卡死不动,无法操作
查看>>
Navicat连接mysql 2003 - Can't connect to MySQL server on ' '(10038)
查看>>
Navicat连接mysql数据库中出现的所有问题解决方案(全)
查看>>
Navicat连接Oracle出现Oracle library is not loaded的解决方法
查看>>
Navicat连接Oracle数据库以及Oracle library is not loaded的解决方法
查看>>
Navicat连接sqlserver提示:未发现数据源名并且未指定默认驱动程序
查看>>
navicat连接远程mysql数据库
查看>>
Navicat通过存储过程批量插入mysql数据
查看>>
Navicat(数据库可视化操作软件)安装、配置、测试
查看>>
navigationController
查看>>
NB-IOT使用LWM2M移动onenet基础通信套件对接之APN设置
查看>>
NBear简介与使用图解
查看>>
Vue过滤器_使用过滤器进行数据格式化操作---vue工作笔记0015
查看>>
Ncast盈可视 高清智能录播系统 IPSetup.php信息泄露+RCE漏洞复现(CVE-2024-0305)
查看>>
NCNN中的模型量化解决方案:源码阅读和原理解析
查看>>
NCNN源码学习(1):Mat详解
查看>>
nc命令详解
查看>>
NC综合漏洞利用工具
查看>>