.net APIHelper client获取数据
using Newtonsoft.Json;
using System.Net.Http.Headers; public static class APIHepler
{
public static string Get(string url)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); return client.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
} public static string Post(string url, object data)
{
HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(data));
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpClient client = new HttpClient(); return client.PostAsync(url, httpContent).Result.Content.ReadAsStringAsync().Result;
} public static string Get(string httpUrl, ref List<string> msg, string source)
{
string strRet = string.Empty;
try
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync(httpUrl).Result;
if (response.IsSuccessStatusCode)
{
strRet = response.Content.ReadAsStringAsync().Result;
}
}
}
catch (Exception ex)
{
strRet = string.Empty;
//日志记录
}
return strRet;
}
public static string Post(string httpUrl, string requestJson, ref List<string> msg, string source)
{
string strRet = string.Empty;
try
{
using (var client = new HttpClient())
{
HttpContent httpContent = new StringContent(requestJson);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage response = client.PostAsync(httpUrl, httpContent).Result;
if (response.IsSuccessStatusCode)
{
strRet = response.Content.ReadAsStringAsync().Result;
}
}
}
catch (Exception ex)
{
strRet = string.Empty;
//日志记录
}
return strRet;
}
}
.net APIHelper client获取数据的更多相关文章
- zk client获取数据
获取数据 它返回znode的关联数据和指定znode的元数据.你将获得信息,例如上次修改数据的时间,修改的位置以及数据的相关信息.此CLI还用于分配监视器以显示数据相关的通知. 语法 get /pat ...
- HBase 高性能获取数据(多线程批量式解决办法) + MySQL和HBase性能测试比较
摘要: 在前篇博客里已经讲述了通过一个自定义 HBase Filter来获取数据的办法,在末尾指出此办法的性能是不能满足应用要求的,很显然对于如此成熟的HBase来说,高性能获取数据应该不是问题. ...
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- Hbase之遍历获取数据
import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import ...
- Hbase之尝试使用错误列族获取数据
import com.google.common.base.Strings; import org.apache.hadoop.conf.Configuration; import org.apach ...
- (一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
(一)----使用HttpClient发送HTTP请求(通过get方法获取数据) 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 “超文本传输协议”,是 ...
- nettyclient异步获取数据
源代码见,以下主要是做个重要代码记录 http://download.csdn.net/detail/json20080301/8180351 NETTYclient获取数据採用的方式是异步获取数据, ...
- Spring MVC 以.html为后缀名访问获取数据,报406 Not Acceptable错误。
如题,最近以spring mvc作为后台框架,前端异步获取数据时(.html为后缀名的访问方式),报406 Not Acceptable错误.当初都不知道啥原因,前后台都没报错就是返回不了数据,于是查 ...
- Spring MVC 以.html为后缀名访问获取数据,报406 Not Acceptable错误
转载,感谢这位博主,有自己的添加. 如题,最近以spring mvc作为后台框架,前端异步获取数据时(.html为后缀名的访问方式),报406 Not Acceptable错误.当初都不知道啥原因,前 ...
随机推荐
- jquery中on绑定click事件在苹果手机失效问题解决(巨坑啊)
描述:用一个div写一个按钮,并给这个按钮添加一个点击事件,在安卓机器上一切正常,但是在苹果机型上会出现点击事件失效. <!DOCTYPE html> <html lang=&quo ...
- linux自启动、定时启动脚本
linux开机自启动 想让一个程序.脚本开机自启动,可以在/etc/rc.d目录下面找到rc.local文件,编辑该文件,在尾部加上需要运行的命令即可. 如: #cd /etc/rc.d #sudo ...
- yarn add & yarn global add
yarn global add & add -D https://yarnpkg.com/zh-Hans/docs/cli/add#toc-commands $ yarn global add ...
- Vue.js 判断对象属性是否存,不存在添加
Vue.set是可以对对象添加属性的,这里item对象添加一个checked属性 //if(typeof item.checked=='undefined'){if(!this.item.checke ...
- 自动化测试断言Assent的使用
Assent 断言模板包含如下方法: assert.fail(actual, expected, message, operator) assert.ok(value, [message]) asse ...
- 【开发工具IDE】解决IntelliJ IDEA 创建Maven项目速度慢的问题
方法一(推荐) 在创建Maven项目时加上 archetypeCatalog=internal 参数,如下: 方法二 在maven的VM Options加上-DarchetypeCatalog=int ...
- vue中axios复用封装
ajax2: function() { let that = this; return that .$http({ method: "get", url: "/Home/ ...
- window与linux查看端口被占用
本文摘写自: 百度经验 https://www.cnblogs.com/ieayoio/p/5757198.html 一.windows:开始---->运行---->cmd,或者是wind ...
- Expect the Expected UVA - 11427(概率dp)
题意: 每天晚上你都玩纸牌,如果第一次就赢了,就高高兴兴的去睡觉,如果输了就继续玩.假如每盘游戏你获胜的概率都为p,每盘游戏输赢独立.如果当晚你获胜的局数的比例严格大于p时才停止,而且每天晚上最多只能 ...
- 跟我学Spring Cloud(Finchley版)-20-Spring Cloud Config-Git仓库配置详解 原
在跟我学Spring Cloud(Finchley版)-19-配置中心-Spring Cloud Config 一节中,已实现使用Git仓库作为Config Server的后端存储,本节详细探讨如何配 ...