不同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时,浏览 器将根据你的要 ...
随机推荐
- extjs错误,看到红色才能让自己记住知识点
1.Cannot call method 'getColumnCount' of undefined Hi, maybe you use colModel before rendering.Notic ...
- requests爬取百度贴吧:python 美女 3
import requests import sys class Tieba(object): def __init__(self, tieba_name, pn): self.tieba_name ...
- sqlserver 索引的结构及其存储,索引内容
sqlserver 索引的结构及其存储,sql server索引内容 文章转载,原文地址: http://www.cnblogs.com/panchunting/p/SQLServer_IndexSt ...
- word使用
1:插入图片,显示不完整,需要>点击上方的段落,选择单倍行距 2:wps 可以直接右键选择保存文件中的图片 3:word中换行符的标识符为^p ,可以用来替换换行符. 4:使word中某一段背 ...
- Selenium之Css Selector使用方法
什么是Css Selector? Css Selector定位实际就是HTML的Css选择器的标签定位 工具 Css Selector的练习建议使用火狐浏览器,下载插件,FireFinder.Fire ...
- Selenium+Java元素定位之一
通过id进行定位 driver.findElement(By.id("kw")).sendKeys("博客园"); 通过name进行定位 driver.find ...
- zabbix3.0 centos7 yum 安装与简单配置
参考文档https://www.zabbix.com/documentation/3.0/start zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.zab ...
- Vue.js学习笔记——表单控件实践
最近项目中使用了vue替代繁琐的jquery处理dom的数据更新,个人非常喜欢,所以就上官网小小地实践了一把. 以下为表单控件的实践,代码敬上,直接新建html文件,粘贴复制即可看到效果~ <! ...
- 【页面加速】配置Nginx加载ngx_pagespeed模块,加快网站打开的速度
ngx_pagespeed 是一个 Nginx 的扩展模块,可以加速你的网站,减少页面加载时间,它会自动将一些提升web性能的实践应用到网页和相关的资源(CSS.JS和图片)上,无需你修改内容和流程. ...
- 新浪微博 SAE
一.云平台中云是指互联网,网络的一种比喻说法,顾名思义,这种平台允许开发者们或是将写好的程序放在“云”里运行,或是使用“云”里提供的服务,或二者皆是. 二.开发类云平台比较: 1.鼻祖 GOOGLE ...