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请求的更多相关文章

  1. Response.ContentType 详细列表-请求的内容类型详细记录

    Response.ContentType 详细列表-请求的内容类型详细记录 作者:王春天一.应用实例: Response.Clear(); Response.ContentType = "t ...

  2. C# ContentType: "application/json" 请求方式传json参数问题

    处理Http请求时遇到的ContentType为application/json方式,记录下这种Post请求方式下如何传json参数: var request = (HttpWebRequest)We ...

  3. http请求与响应(content-type)

    http请求信息由浏览器把地址栏URL信息和页面(html.jsp.asp)组装成http请求消息体(如下). <request-line>(请求消息行)<headers>(请 ...

  4. 关于content-type请求头的说明

    Content-Type请求头的作用,用于标记请求体数据的格式,如: 1. Content-Type:application/x-www-form-urlencoded 请求体:b'pwd=123&a ...

  5. 处理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 ...

  6. Ajax服务请求原理 简单总结

    刚开始以为Ajax是一种新的语言,接触之后才知道,ajax是用于服务器交换数据并更新部分网页的Web应用程序的技术. 第一次看到Ajax请求代码时,感觉一脸萌逼,这些代码竟然把后台数据请求过来了,神奇 ...

  7. AFNetworking请求设置请求头

    NSString *url = @"INPUT URL HERE"; AFHTTPRequestOperationManager *manager = [AFHTTPRequest ...

  8. Android请求服务器的两种方式--post, get的区别

    android中用get和post方式向服务器提交请求_疯狂之桥_新浪博客http://blog.sina.com.cn/s/blog_a46817ff01017yxt.html Android提交数 ...

  9. HTTP请求头详解

    http://blog.csdn.net/kfanning/article/details/6062118 HTTP由两部分组成:请求和响应.当你在Web浏览器中输入一个URL时,浏览 器将根据你的要 ...

随机推荐

  1. sass不识别中文字符的问题

    进入Koala安装目录,例如:C:\Program Files (x86)\Koala\rubygems\gems\sass-3.4.9\lib\sass 或者 C:\Ruby\lib\ruby\ge ...

  2. oracle(八)块清除

    (1)  快速块清除(fast block cleanout), 当事务修改的数据库全部保存在buffer cache并且修改数据块的数据量没有超过cache buffer 的10%,快速清除事务信息 ...

  3. [解决]WPF 在 win7 系统无法运行:FileNotFoundException

    开发环境:VS2015 + .NET 4.6.2 开发项目1:WPF + CefSharp 开发项目2:WPF 情况:两个项目编译的程序都无法在客户环境的 win7上运行,事件查看器中如下日志: Th ...

  4. 鼠标滑动到指定位置时div固定在头部

    $(function(){ $(window).scroll(function () {            if ($(window).scrollTop() > 253) {       ...

  5. mysql 数据类型 目录

    mysql 数据类型 mysql 整数类型 数值类型 tinyint mysql int 整数类型 解释显示宽度 和 存储宽度 mysql float 浮点型 mysql 日期类型 mysql 字符串 ...

  6. HTML5-CSS3-JavaScript(4)

    CSS3中 变形与动画相关属性 CSS3在原来的基础上新增了变形和动画相关属性,通过这些属性可以实现以前需要大段JavaScript才能实现的功能.CSS3的变形功能可以对HTML组件执行位移.旋转. ...

  7. Scala系统学习(三):Scala基础语法

    如果您熟悉Java语言语法和编程,那么学习Scala将会很容易.Scala和Java之间最大的句法差异在于行结束字符的分号(;) 是可选的. 当编写Scala程序时,它可以被定义为通过调用彼此的方法进 ...

  8. opencv3寻找最小包围矩形在图像中的应用-滚动条

    #include<opencv2/opencv.hpp> #include<iostream> #include<vector> using namespace c ...

  9. 9/24matplotlib使用入门

    ---恢复内容开始--- matplotlib的使用中有好几种输出风格,有matlab风格,和官方文档的as风格,各有所长,本文对比介绍官方文档中的使用风格. 我们画图的目的是要将函数以图像显示出来, ...

  10. sql distinct去除重复

    distinct select  distinct * from  table1 或者用 group  by