不同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时,浏览 器将根据你的要 ...
随机推荐
- 【python基础】python程序打包成.exe运行时会弹出黑框
用python调用.bat或者.exe文件时,一般调用 方式如下: os.system("C:\Windows\System32\osk.exe") 对吧,这样就会因为调用了系统s ...
- SQL实现交,并,差操作
有的数据库不支持intersect,except,所以交集,和差集使用嵌套查询来做比较靠谱. a表和b表具有完全一样的结构 mysql> desc a; +-------+----------- ...
- 关于获取多个id的用法
一.子页面中的写法 <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" ...
- mysql 数据类型 目录
mysql 数据类型 mysql 整数类型 数值类型 tinyint mysql int 整数类型 解释显示宽度 和 存储宽度 mysql float 浮点型 mysql 日期类型 mysql 字符串 ...
- SQLServer和MySQL job和 event定时器的差别
SQLServer和MySQL job和 event定时器的差别
- keras之save & reload model
import numpy as np np.random.seed(1337) # for reproducibility from keras.models import Sequential fr ...
- Fuzzy and fun on Air Jordan 12 Doernbecher design
Carissa Navarro keeps it warm, fuzzy and fun on her 2017 Air Jordan 12 Doernbecher design. Nike's 20 ...
- minicom的安装和tftp的安装
1.minicom 的安装 在弹出的窗口中选择“Serial port setup”进行配置.配置完之后选择“Save setup as dfl”保存.最后选择“Exit from Minicom”退 ...
- Functional Programming 资料收集
书籍: Functional Programming for Java Developers SICP(Structure and Interpretation of Computer Program ...
- nginx 总结
本文转自:http://freeloda.blog.51cto.com/2033581/1288553 ,感谢大神的辛勤付出! 大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之 ...