public class HttpClientHelper
{
/// <summary>
/// get请求
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string GetResponse(string url)
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = httpClient.GetAsync(url).Result; if (response.IsSuccessStatusCode)
{
string result = response.Content.ReadAsStringAsync().Result;
return result;
}
return null;
} public static T GetResponse<T>(string url)
where T : class,new()
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = httpClient.GetAsync(url).Result; T result = default(T); if (response.IsSuccessStatusCode)
{
Task<string> t = response.Content.ReadAsStringAsync();
string s = t.Result; result = JsonConvert.DeserializeObject<T>(s);
}
return result;
} /// <summary>
/// post请求
/// </summary>
/// <param name="url"></param>
/// <param name="postData">post数据</param>
/// <returns></returns>
public static string PostResponse(string url, string postData)
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; HttpContent httpContent = new StringContent(postData);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpClient httpClient = new HttpClient(); HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; if (response.IsSuccessStatusCode)
{
string result = response.Content.ReadAsStringAsync().Result;
return result;
}
return null;
} /// <summary>
/// 发起post请求
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="url">url</param>
/// <param name="postData">post数据</param>
/// <returns></returns>
public static T PostResponse<T>(string url, string postData)
where T : class,new()
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; HttpContent httpContent = new StringContent(postData);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpClient httpClient = new HttpClient(); T result = default(T); HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; if (response.IsSuccessStatusCode)
{
Task<string> t = response.Content.ReadAsStringAsync();
string s = t.Result; result = JsonConvert.DeserializeObject<T>(s);
}
return result;
} /// <summary>
/// V3接口全部为Xml形式,故有此方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="url"></param>
/// <param name="xmlString"></param>
/// <returns></returns>
public static T PostXmlResponse<T>(string url, string xmlString)
where T : class,new()
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; HttpContent httpContent = new StringContent(xmlString);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpClient httpClient = new HttpClient(); T result = default(T); HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; if (response.IsSuccessStatusCode)
{
Task<string> t = response.Content.ReadAsStringAsync();
string s = t.Result; result = XmlDeserialize<T>(s);
}
return result;
} /// <summary>
/// 反序列化Xml
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="xmlString"></param>
/// <returns></returns>
public static T XmlDeserialize<T>(string xmlString)
where T : class,new ()
{
try
{
XmlSerializer ser = new XmlSerializer(typeof(T));
using (StringReader reader = new StringReader(xmlString))
{
return (T)ser.Deserialize(reader);
}
}
catch (Exception ex)
{
throw new Exception("XmlDeserialize发生异常:xmlString:" + xmlString + "异常信息:" + ex.Message);
} }
}

  

转载地址:http://www.2cto.com/weixin/201501/367405.html

C# HttpClientHelper请求的更多相关文章

  1. .Net之简单通知服务

    开篇语 这两天看见有大佬分享使用钉钉和企业微信的机器人来做通知报警,然后我想到了我使用的另一个第三方软件捷易快信(可能大家都不知道这个东西,我也忘了我最开始是咋知道的),该服务的优点是可以通过微信进行 ...

  2. C#微信开发之旅(二):基础类之HttpClientHelper(更新:SSL安全策略)

    public class HttpClientHelper   2     {   3         /// <summary>   4         /// get请求   5    ...

  3. Android实现异步处理 -- HTTP请求

    原帖:http://www.cnblogs.com/answer1991/archive/2012/04/22/2464524.html Android操作UI的方法不是线程安全的,也就是说开发者自己 ...

  4. commons-httpclient 实现get和post请求

    引入的jar包为: <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient --> &l ...

  5. java 常见几种发送http请求案例

    import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java ...

  6. Java Http 请求

    package zr.weixin.com.utils; import java.io.BufferedReader; import java.io.IOException; import java. ...

  7. c# Http请求之HttpClient

    利用HttpClient进行Http请求,基于此,简单地封装了下: using System; using System.Collections.Generic; using System.Colle ...

  8. java Http post请求发送json字符串

    最近差点被业务逻辑搞懵逼,果然要先花时间思考,确定好流程再执行.目前最好用的jar包还是org.apache.http. public class HttpClientHelper { private ...

  9. httpcomponents 发送get post请求

    引入的包为: <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <de ...

随机推荐

  1. Linux操作系统CentOS7.2发行版本的安装与配置(安装是选择服务器类型)

    原文地址:http://1130739.blog.51cto.com/1120739/1738791 由于CentOS 7出来不久,对于这个版本的系统安装与配置的教程较少,今天就索性介绍一下CentO ...

  2. shell case语法

    在阅读hadoop相关的脚本文件时,遇到case语句,好久不写shell,忘了不少,复习下shell的case语句:                             运行结果:         ...

  3. linux权限详解

    一个用户.一个组 我们来看一看 Linux 权限和所有权模型.我们已经看到每个文件属于一个用户和一个组.这正是 Linux 中权限模型的核心.您可以在 ls -l 清单中查看用户和组: $ ls -l ...

  4. regAsm的历史问题

    regAsm是用来注冊.卸载dll成为通用库的一个工具.关于regAsm的具体资料请參照http://msdn.microsoft.com/en-us/library/tzat5yw6(v=vs.11 ...

  5. Extjs DateTime 日期时间选择控件 (非点击日期强制选择) 支持4.0以上

    Extjs的日期控件,仅仅能支持到日期选择,对时间的选择并不完好.而网上下载的控件,都是基于Ext.form.dateField 开发.在选中日期后自己主动选择,并隐藏此选择窗体. 在经过一番改造后, ...

  6. 用js实现预览待上传的本地图片

    js实现预览待上传的本地图片,代码如下: <form name="form5" id="form5" method="post" ac ...

  7. Spring Boot 2.0 Intellij Idea 中图文详解打包成可执行Jar

    我们使用Spring Boot 2.0 创建好我们的项目后,我们一般需要打包,然后部署到服务器上. 打包步骤: 1. 选中项目,右键——> Open Module Settings. 2. 切换 ...

  8. 以太网基础知识0(UDP和TCP有什么区别)

    参考:http://zhidao.baidu.com/link?url=GSIg9_zFhWi6PHezalQveRwwUsU0as7k6MFd05r-cruLT1yDABARraHkuq8ohdIR ...

  9. java playframework

    刚学了java不久就让我们尝试架构,就选择了一个比較简单的架构 play framework直接上图 这里的执行环境是JDK1.7,我试了一下jdk1.8不行出问题了play使用的是play-1.2. ...

  10. C# 获取FormData数据

    通常的方法是你创建一个 FormData 对象.然后你使用append方法来加入任何额外的key和他们的值.就像这样: var form = new FormData(); form.append(& ...