不同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时,浏览 器将根据你的要 ...
随机推荐
- 学习计划 nginx 中 mime.types 的作用
nginx中http模块下默认配置加载了mime.type 于是想知道这是什么? -- 什么是mime.type? 首先,应该了解到,浏览器是如何处理内容的,在浏览器中显示的各种内容,MP4.png. ...
- 在本机搭建vue-cli3项目
vue-cli3官方网址: https://cli.vuejs.org/zh/ 由于公司开始一个新项目,用到的是vue-cli3搭建的项目,所以自己想搭建一个项目,今天搭建的项目就是一个很简单的项目, ...
- 第四课:通过配置文件获取对象(Spring框架中的IOC和DI的底层就是基于这样的机制)
首先在D盘创建一个文件hero.txt,内容为:com.hero.Hero(此处必须是Hero的完整路径) 接下来是Hero类 package com.hero; public class Hero ...
- 启动InnoDB引擎的方法
启动InnoDB引擎的方法 http://down.chinaz.com/server/201207/2090_1.htm 启动InnoDB引擎的方法 Mysql中默认的是MyISAM数据引擎,可惜此 ...
- Look for the Air Jordan 32 in full family sizing
Following the release of the 'Rosso Corsa' colorway, Jordan Brand is now set to officially launch th ...
- Summary: gcd最大公约数、lcm最小公倍数算法
欧几里德算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数.其计算原理依赖于下面的定理: 定理:gcd(a,b) = gcd(b,a mod b) 证明:a可以表示成a = kb + ...
- volatile的语义与实现
1.volatile关键字的两层语义 一旦一个共享变量(类的成员变量.类的静态成员变量)被volatile修饰之后,那么就具备了两层语义: 1)保证了不同线程对这个变量进行操作时的可见性,即一个线程修 ...
- YUV编码格式
YUV是被欧洲电视系统采用的一种颜色编码方法.在现代彩色电视系统中,通常采用三管彩色摄影机或彩色CCD摄像机取像,然后把取 得的彩色图像信号经过分色,分别放大校正后得到RGB,在经过矩阵变换电路,得到 ...
- Trove系列(五)—Trove的数据存储管理程序类型和版本管理功能介绍
功能描述数据存储管理程序(Datastore)类型管理允许Trove的用户从操作者列出的名单中选择数据库存储管理程序和版本.操作者将可以控制数据库存储管理程序的类型,添加一个新的版本并去活一个老版本. ...
- ASP.NET控件--DropDownList
设置默认值:DropDownList1.Items[i].Selected=true;绑定:DropDownList1.DataSource = dataSet.Tables["Tabl ...