发送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,并接受返回值的更多相关文章

  1. RestTemplate的exchange()方法,解决put和delete请求拿不到返回值的问题

    嗷嗷待哺的controller(被调用provider的controller方法) //测试get少量参数 @RequestMapping(value = "detailsGetD" ...

  2. 【转】AJAX发送 PUT和DELETE请求注意事项

    jax使用restful服务发送put 和 delete 请求时直接传参会出现问题 一,采用POST  + _method:delete/put  + filter 的方法ajax发送put 和 de ...

  3. AJAX发送 PUT和DELETE请求参数传递注意点,了解一下

    ajax发送put 和 delete 请求时,需要传递参数,如果参数在url地址栏上,则可以正常使用, 如果在 data:中需要传递参数,(浏览器会使用表单提交的方式进行提交) 则需要注意此时应作如下 ...

  4. springboot——发送put、delete请求

    在springmvc中我们要发送put和delete请求,需要先配置一个过滤器HiddenHttpMethodFilter,而springboot中,已经帮我们自动配置了,所以我们可以不用配置这个过滤 ...

  5. Spring框架下的 “接口调用、MVC请求” 调用参数、返回值、耗时信息输出

    主要拦截前端或后天的请求,打印请求方法参数.返回值.耗时.异常的日志.方便开发调试,能很快定位到问题出现在哪个方法中. 前端请求拦截,mvc的拦截器 import java.util.Date; im ...

  6. springmvc 发送PUT 和 DELETE 请求

    一: 发送 DELETE 或者 PUT 请求: 1.在表单中加入一个隐藏的参数: _method  , 值是 DELETE (或者PUT) <form action="springmv ...

  7. 如何同步发送put或者delete请求

    1.必须把前端发送方式改为post . 2.在web.xml中配置一个filter:HiddenHttpMethodFilter过滤器 3.必须携带一个键值对,key=_method,  value= ...

  8. 通过 Ajax 发送 PUT、DELETE 请求的两种实现方式

    一.普通请求方法发送 PUT 请求 1. 如果不用 ajax 发送 PUT,我们可以通过设置一个隐藏域设置 _method 的值,如下: <form action="/emps&quo ...

  9. FiddlerScript修改特定请求参数下的返回值

    使用场景: api/Live/GetLiveList接口: (1)Type为1,接口返回直播列表 (2)Type为2,接口返回回放列表 现在想修改直播列表的返回值 思路: 利用FiddlerScrip ...

随机推荐

  1. ImageButton的坑 ImageButton 有问题

    最近在用ImageButton,发现,我如果new ImageButton,并且 设置Warp_content,但是它并不会正真的warp,它会有一个边框. 不知道怎么回事. 后来,在代码里面使用Im ...

  2. 容器技术Docker

    什么是decker容器 简单理解就是将代码和部署环境一起打包的一个容器

  3. 树&二叉树&哈夫曼树

    1.树 需要注意的两点:n(n>=0)表示结点的个数,m表示子树的个数 (1)n>0时,树的根节点是唯一的. (2)m>0时,子树的个数没有限制. 结点的度和树的度 (1)结点的度是 ...

  4. ElasticSearch学习笔记(二)-- mapping

    1. mapping简介 2. 自定义 mapping 3. mapping演示 创建索引,设置mapping,可以新增字段 get一下mapping 设置索引的字段不可新增 为索引添加字段,发现报错 ...

  5. sqlsever存储过程学习笔记

    1,创建数据表 use test create table money( id ,) primary key, money int, monetary_unity char ); 2,考虑到货币单位的 ...

  6. 剑指Offer - 九度1388 - 跳台阶

    剑指Offer - 九度1388 - 跳台阶2013-11-24 03:43 题目描述: 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 输入: 输入可能包 ...

  7. XSS注入常用语句积累

    <script>alert('hello,gaga!');</script> //经典语句,哈哈! >"'><img src="javas ...

  8. Microsxxxxxxx-面试总结

    策略题 There are four kinds of cards, respectively, 1,2, 3,4 numbers. There are seven cards for each ty ...

  9. CentOS修改IP地址

    一.CentOS 修改IP地址修改对应网卡的IP地址的配置文件 # vi /etc/sysconfig/network-scripts/ifcfg-eth0   电信 # vi /etc/syscon ...

  10. java课后作业2017.10.20

    动手动脑1: public class Test{ public static void main(String args[]) { Foo obj1=new Foo(); }}class Foo{ ...