C#发送POST,GET,DELETE请求API,并接受返回值
发送POST请求
/// <summary>
/// API发送POST请求
/// </summary>
/// <param name="url">请求的API地址</param>
/// <param name="parametersJson">POST过去的参数(JSON格式)字符串</param>
/// <returns></returns>
public static string ApiPost(string url, string parametersJson)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(System.Configuration.ConfigurationManager.AppSettings["ApiHttp"]);
// 为JSON格式添加一个Accept报头
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//需要传递的参数(参数封装成JSON)
HttpContent content = new StringContent(parametersJson)
{
Headers = { ContentType = new MediaTypeHeaderValue("application/json") }
}; HttpResponseMessage response = client.PostAsync(url, content).Result;
response.EnsureSuccessStatusCode();
return response.Content.ReadAsStringAsync().Result;
}
发送GET请求
/// <summary>
/// API发送GET请求,返回Json
/// </summary>
/// <param name="url"></param>
/// <returns>如果未成功返回空</returns>
public static string ApiGet(string url)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(System.Configuration.ConfigurationManager.AppSettings["ApiHttp"]);
// 为JSON格式添加一个Accept报头
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync(url).Result;
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsStringAsync().Result;
}
return "";
}
发送DELETE请求
/// <summary>
/// API发送DELETE请求,返回状态:200成功,201失败
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string ApiDelete(string url)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(System.Configuration.ConfigurationManager.AppSettings["ApiHttp"]);
// 为JSON格式添加一个Accept报头
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.DeleteAsync(url).Result;
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsStringAsync().Result;
}
return "";
}
C#发送POST,GET,DELETE请求API,并接受返回值的更多相关文章
- RestTemplate的exchange()方法,解决put和delete请求拿不到返回值的问题
嗷嗷待哺的controller(被调用provider的controller方法) //测试get少量参数 @RequestMapping(value = "detailsGetD" ...
- 【转】AJAX发送 PUT和DELETE请求注意事项
jax使用restful服务发送put 和 delete 请求时直接传参会出现问题 一,采用POST + _method:delete/put + filter 的方法ajax发送put 和 de ...
- AJAX发送 PUT和DELETE请求参数传递注意点,了解一下
ajax发送put 和 delete 请求时,需要传递参数,如果参数在url地址栏上,则可以正常使用, 如果在 data:中需要传递参数,(浏览器会使用表单提交的方式进行提交) 则需要注意此时应作如下 ...
- springboot——发送put、delete请求
在springmvc中我们要发送put和delete请求,需要先配置一个过滤器HiddenHttpMethodFilter,而springboot中,已经帮我们自动配置了,所以我们可以不用配置这个过滤 ...
- Spring框架下的 “接口调用、MVC请求” 调用参数、返回值、耗时信息输出
主要拦截前端或后天的请求,打印请求方法参数.返回值.耗时.异常的日志.方便开发调试,能很快定位到问题出现在哪个方法中. 前端请求拦截,mvc的拦截器 import java.util.Date; im ...
- springmvc 发送PUT 和 DELETE 请求
一: 发送 DELETE 或者 PUT 请求: 1.在表单中加入一个隐藏的参数: _method , 值是 DELETE (或者PUT) <form action="springmv ...
- 如何同步发送put或者delete请求
1.必须把前端发送方式改为post . 2.在web.xml中配置一个filter:HiddenHttpMethodFilter过滤器 3.必须携带一个键值对,key=_method, value= ...
- 通过 Ajax 发送 PUT、DELETE 请求的两种实现方式
一.普通请求方法发送 PUT 请求 1. 如果不用 ajax 发送 PUT,我们可以通过设置一个隐藏域设置 _method 的值,如下: <form action="/emps&quo ...
- FiddlerScript修改特定请求参数下的返回值
使用场景: api/Live/GetLiveList接口: (1)Type为1,接口返回直播列表 (2)Type为2,接口返回回放列表 现在想修改直播列表的返回值 思路: 利用FiddlerScrip ...
随机推荐
- Ubuntu设置root密码[repost]
From: http://hi.baidu.com/busybox/item/283e7d31433db7179cc65ef3 安装完Ubuntu后在终端使用命令:su -然后输入密码,总是不正确.原 ...
- CSS 一些基础知识(优先级、行内元素的一些属性、font-size单位) 怎样不加载图片
CSS大小写不敏感 选择器优先级如下所示: 在属性后面使用 !important 会覆盖页面内任何位置定义的元素样式. 作为style属性写在元素内的样式 id选择器 类选择器 标签选择器 通配符选择 ...
- hasOne
public boolean hasOne(int n) { int lastdigit=0; while( n >0 ){ lastdigit=(n % 10); if(lastdigit== ...
- Kinect关于PlayerIndex和SkeletonId之间的关系。
项目中要锁定玩家骨骼后抠图, 一时没有灵感.google 关键词: the ralationship about skeletonid and playerindex. 结论: Player Segm ...
- js限制文本框只能输入特定字符
限制只能输入数字 // ---------------------------------------------------------------------- // <summary> ...
- linux_shell基础-变量、数组、运算符
#!/bin/bash# echo 'hello world'# name='http://www.baidu.com'# for file in $(ls ./); do# echo "t ...
- Django 运行 端口被占用 Error: That port is already in use
本来运行项目:python manage.py runserver 8000 发现运行到结果报错: Error: That port is already in use 首先查看已存在端口号列表: $ ...
- Python全栈(一)编程语言介绍
一.编程语言介绍 程序是计算机能读懂的语言,是人和计算机沟通的方式. 计算机无法理解符号,只能理解0,1的二进制. 计算机内的运行状态就像灯泡的开关一样来表示各庄状态,两个灯泡能表示4种状态,无数的灯 ...
- python学习总结----时间模块 and 虚拟环境(了解)
time - sleep:休眠指定的秒数(可以是小数) - time:获取时间戳 # 获取时间戳(从1970-01-01 00:00:00到此刻的秒数) t = time.time() print(t ...
- 孤荷凌寒自学python第六天 列表的嵌套与列表的主要方法
孤荷凌寒自学python第六天 列表的嵌套与列表的主要方法 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) (同步的语音笔记朗读:https://www.ximalaya.com/keji/1 ...