C#工具:利用HttpClient调用WebApi
可以利用HttpClient来进行Web Api的调用。由于WebA Api的调用本质上就是一次普通的发送请求与接收响应的过程,
所有HttpClient其实可以作为一般意义上发送HTTP请求的工具。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks; namespace 自己的名称空间
{
public class ApiHelper
{
/// <summary>
/// api调用方法/注意一下API地址
/// </summary>
/// <param name="controllerName">控制器名称--自己所需调用的控制器名称</param>
/// <param name="overb">请求方式--get-post-delete-put</param>
/// <param name="action">方法名称--如需一个Id(方法名/ID)(方法名/?ID)根据你的API灵活运用</param>
/// <param name="obj">方法参数--如提交操作传整个对象</param>
/// <returns>json字符串--可以反序列化成你想要的</returns>
public static string GetApiMethod(string controllerName, string overb, string action, object obj = null)
{
Task<HttpResponseMessage> task = null;
string json = "";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:****/api/" + controllerName + "/");
switch (overb)
{
case "get":
task = client.GetAsync(action);
break;
case "post":
task = client.PostAsJsonAsync(action, obj);
break;
case "delete":
task = client.DeleteAsync(action);
break;
case "put":
task = client.PutAsJsonAsync(action, obj);
break;
default:
break;
}
task.Wait();
var response = task.Result;
if (response.IsSuccessStatusCode)
{
var read = response.Content.ReadAsStringAsync();
read.Wait();
json = read.Result;
}
return json;
}
}
}
可能需要以下引用集:
System.Net.Http.Formatting.dll
System.Web.Http.dll
C#工具:利用HttpClient调用WebApi的更多相关文章
- httpclient 调用WebAPI
1.创建webapi项目,提供接口方法如下: /// <summary> /// 获取租户.位置下的所有传感器 /// </summary> /// <returns&g ...
- 【ASP.NET Web API2】利用HttpClient调用Web API(TODO)
参照: 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用 纯属记录一下遇到的问题: 我们利用HttpClient来调用自宿主方式寄宿的Web API.HttpCl ...
- 使用HttpClient调用WebAPI接口,含WebAPI端示例
API端: using log4net; using System; using System.Collections.Generic; using System.IO; using System.L ...
- HttpClient调用webApi时注意的小问题
HttpClient client = new HttpClient(); client.BaseAddress = new Uri(thisUrl); client.GetAsync("a ...
- HttpClient 调用WebAPI时,传参的三种方式
public void Post() { //方法一,传json参数 var d = new { username = " ", password = " ", ...
- 使用httpclient异步调用WebAPI接口
最近的工作需要使用Bot Framework调用原有的WebAPI查询数据,查找了一些方法,大部分都是使用HttpClient调用的,现时贴出代码供参考 using System; using Sys ...
- Asp.Net MVC WebAPI的创建与前台Jquery ajax后台HttpClient调用详解
1.什么是WebApi,它有什么用途? Web API是一个比较宽泛的概念.这里我们提到Web API特指ASP.NET MVC Web API.在新出的MVC中,增加了WebAPI,用于提供REST ...
- java利用HttpClient进行https接口调用
1.为了避免需要证书,所以用一个类继承DefaultHttpClient类,忽略校验过程. import java.security.cert.CertificateException; import ...
- java 通过httpclient调用https 的webapi
java如何通过httpclient 调用采用https方式的webapi?如何验证证书.示例:https://devdata.osisoft.com/p...需要通过httpclient调用该接口, ...
随机推荐
- ztree设置节点checked,选中某节点等相关操作
ztree设置节点checked,选中某节点等相关操作 1.根据id获取树的某个节点: var zTree = $.fn.zTree.getZTreeObj("mytree"); ...
- [LeetCode] Positions of Large Groups 大群组的位置
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- swoole+websocket+redis实现一对一聊天
如同web端的QQ和微信一样,这是一个web端的聊天程序. 环境:ubuntu + php + swoole扩展 + redis + mysql Redis 实现每个连接websocket的服务都唯一 ...
- mybatis3源码阅读之SqlSessionFactoryBuilder
/** 构造器,根据配置或者代码生成SqlSessionFactory,采用分布构建的Builder模式 /* public class SqlSessionFactoryBuilder { /** ...
- 马哥k8s
https://pan.baidu.com/s/1BAX-j54bLcmWF-0ei-c-pw 31y6
- Eclipse格式化整个项目
Eclipse有一个非常好的功能,就是把源代码进行美化(或者是标准化),在打开的Java源代码中,Ctrl+Shift+F就可做到. 但是,如果你想把整个项目中的源代码都美化一下呢?这里有一个简单的办 ...
- [Swift]LeetCode287. 寻找重复数 | Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- [Swift]LeetCode342. 4的幂 | Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example 1: ...
- [Swift]LeetCode890. 查找和替换模式 | Find and Replace Pattern
You have a list of words and a pattern, and you want to know which words in words matches the patter ...
- Mysql的两种“排名第几且有可能为空的记录”写法(力扣176)
编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 ...