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 ...
随机推荐
- SetConsoleCtrlHandler
Excerpt: Registering a Control Handler Function This is an example of the SetConsoleCtrlHandler fu ...
- 为什么i=i++后,i的值不变(深入解析)
在Java中,运行以下代码: int i=10; i=i++; System.out.println(i); 得到的结果仍然为10,为什么呢?理论上,运算的过程不应该是i首先把10取出来,赋值给i,然 ...
- PJMEID学习之视频的捕捉与播放
pjmedia是pjsip的视频部分,官网明确提示,要想使用pjmedia离不开directshow/sdl/ffmpeg这三个库. 软件版本的限制: ffmpeg不能高于1.25.(建议下载1.01 ...
- leetcode 【 Merge Two Sorted Lists 】 python 实现
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- Swift 与众不同的地方
Swift 与众不同的地方 switch(元组) 特点 其他语言中的switch语句只能比较离散的整形数据(字符可以转换成整数) 但是swift中可以比较整数.浮点数.字符.字符串.和元组数据类型,而 ...
- js 请求异常重连或断线后联网重连机制(ajax)
转到到 https://blog.csdn.net/mengtoumingren/article/details/80296788
- pytest 运行指定用例
pytest运行指定用例 随着软件功能的增加,模块越来越多,也意味用例越来越多,为了节约执行时间,快速得到测试报告与结果,在工作中可以通过运行指定用例,达到快速执行用例 例子目录 spec_sub1_ ...
- LightGBM的并行优化--机器学习-周振洋
LightGBM的并行优化 上一篇文章介绍了LightGBM算法的特点,总结起来LightGBM采用Histogram算法进行特征选择以及采用Leaf-wise的决策树生长策略,使其在一批以树模型为基 ...
- C++ 虚函数的内存分配
1.无继承的普通类: 在有虚函数的情况下类会为其增加一个隐藏的成员,虚函数表指针,指向一个虚函数表,虚函数表里面就是类的各个虚函数的地址了.那么,虚函数表指针是以什么模型加入到类里面的,虚函数表里 ...
- thinkphp3.2 学习笔记 基础篇
环境要求:PHP5.3以上版本注意:PHP5.3DEV和php6不支持 目录结构 www WEB部署目录(或者子目录)├─index.php 入口文件├─README.md README文件├─App ...