不同ContentType的post请求
public static T Invoke<T>(string url, object input, bool requireJSON = true)
{
using (var client = new HttpClient())
{
HttpContent httpContent = null; if (input != null)
{
string paramJson = string.Empty; if (requireJSON)
{
paramJson = JsonConvert.SerializeObject(input);
}
else
{
paramJson = Convert.ToString(input);
} httpContent = new StringContent(paramJson);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
} var response = client.PostAsync(url, httpContent).Result; var result = response.Content.ReadAsStringAsync().Result; var output = JsonConvert.DeserializeObject<T>(result); return output;
}
}
application/json
public static R Post3w<P, R>(string url, P input)
{
using (HttpClient client = new HttpClient())
{
HttpRequestMessage request = new HttpRequestMessage();
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(url);
string inputJson = JsonConvert.SerializeObject(input);
Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(inputJson); //convert to key/value pairs
request.Content = new FormUrlEncodedContent(values);
var response = client.SendAsync(request).Result;
var json = response.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<R>(json);
}
}
application/x-www-form-urlencoded
不同ContentType的post请求的更多相关文章
- Response.ContentType 详细列表-请求的内容类型详细记录
Response.ContentType 详细列表-请求的内容类型详细记录 作者:王春天一.应用实例: Response.Clear(); Response.ContentType = "t ...
- C# ContentType: "application/json" 请求方式传json参数问题
处理Http请求时遇到的ContentType为application/json方式,记录下这种Post请求方式下如何传json参数: var request = (HttpWebRequest)We ...
- http请求与响应(content-type)
http请求信息由浏览器把地址栏URL信息和页面(html.jsp.asp)组装成http请求消息体(如下). <request-line>(请求消息行)<headers>(请 ...
- 关于content-type请求头的说明
Content-Type请求头的作用,用于标记请求体数据的格式,如: 1. Content-Type:application/x-www-form-urlencoded 请求体:b'pwd=123&a ...
- 处理flutter http请求添加application/json报错Cannot set the body fields of a Request with content-type “application/json”
在flutter中在http请求发送时设置"content-type": "application/json"会出现报错Cannot set the body ...
- Ajax服务请求原理 简单总结
刚开始以为Ajax是一种新的语言,接触之后才知道,ajax是用于服务器交换数据并更新部分网页的Web应用程序的技术. 第一次看到Ajax请求代码时,感觉一脸萌逼,这些代码竟然把后台数据请求过来了,神奇 ...
- AFNetworking请求设置请求头
NSString *url = @"INPUT URL HERE"; AFHTTPRequestOperationManager *manager = [AFHTTPRequest ...
- Android请求服务器的两种方式--post, get的区别
android中用get和post方式向服务器提交请求_疯狂之桥_新浪博客http://blog.sina.com.cn/s/blog_a46817ff01017yxt.html Android提交数 ...
- HTTP请求头详解
http://blog.csdn.net/kfanning/article/details/6062118 HTTP由两部分组成:请求和响应.当你在Web浏览器中输入一个URL时,浏览 器将根据你的要 ...
随机推荐
- 网站优化(SEO)的10大误区
前段时间大前端也有关于SEO的文章贡献给广大读者,今日,再发一文,网站优化(SEO)的10大误区.很多新手站长初次接触SEO,感受到SEO 的无穷魅力,想要做一位优秀的SEOer,然而新手朋友在进行S ...
- 洛谷P3389 高斯消元 / 高斯消元+线性基学习笔记
高斯消元 其实开始只是想搞下线性基,,,后来发现线性基和高斯消元的关系挺密切就一块儿在这儿写了好了QwQ 先港高斯消元趴? 这个算法并不难理解啊?就会矩阵运算就过去了鸭,,, 算了都专门为此写个题解还 ...
- CentOS网卡配置大全
持续更新中... 1.配置机器名 2.配置网卡属性 3.问题解决 3.1解决网卡经常需要手动重连问题 vi /etc/sysconfig/network-scripts/ifcfg-ens33 把ON ...
- memcached-session-manager 教程实现session共享
1简单介绍 1.1决定用什么序列化策略. 1.2配置tomcat 1.2.1加入 memcached-session-manager jar 包到tomcat中. ...
- Domino代理运行问题
当Server出现“operation is disallowed in this session”此命令时为代理权限问题,修改后即可正常运行代理.
- vertx连接mysql数据库
1:创建一个verticle组件 package jdbcConnection; import io.vertx.core.AbstractVerticle; import io.vertx.core ...
- .Net Core 使用依赖注入
ASP.NET Core 源码阅读笔记(1) ---Microsoft.Extensions.DependencyInjection 在asp .net中使用依赖注入很简单,只需要在Startup类的 ...
- 批量导入数据到mssql数据库的
概述 批量导入数据到数据库中,我们有好几种方式. 从一个数据表里生成数据脚本,到另一个数据库里执行脚本 从EXCEL里导入数据 上面两种方式,导入的数据都会生成大量的日志.如果批量导入5W条数据到数据 ...
- win10下的iis的配置(服务于asp.net)
win10下的iis的配置和win7下的是类似的. 1.右键开始,打开控制面板,进入卸载程序中,勾上如下图所示的项目,即可装上iis. 这里写图片描述 2.重启后搜索iis,进入iis配置中.点击网站 ...
- #C++初学记录(算法考试1)
B - Maximal Continuous Rest Each day in Berland consists of n hours. Polycarp likes time management. ...