using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace EmailHelp.Utility
{
public class HttpClientHelper
{
#region Get请求
/// <summary>
/// get请求
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static bool GetResponse(string url, out string statusCode, string ContentType = "application/json")
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(ContentType)
);
HttpResponseMessage response = httpClient.GetAsync(url).Result;
if (response.IsSuccessStatusCode)
{
statusCode = response.Content.ReadAsStringAsync().Result;
}
else
{
statusCode = "";
}
return response.IsSuccessStatusCode;
}
public static bool GetResponse<T>(string url, out T statusCode, string ContentType = "application/json")
where T : class, new()
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(ContentType)
);
HttpResponseMessage response = httpClient.GetAsync(url).Result;
if (response.IsSuccessStatusCode)
{
Task<string> t = response.Content.ReadAsStringAsync();
string s = t.Result;
statusCode = JsonConvert.DeserializeObject<T>(s);
}
else
{
statusCode = null;
}
return response.IsSuccessStatusCode;
}
#endregion
#region Post请求
/// <summary>
/// Post请求
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="postData">请求数据(已转为Json数据)</param>
/// <param name="statusCode">返回数据</param>
/// <param name="ContentType">请求类型</param>
/// <param name="CodingFormat">编码格式</param>
/// <returns>请求结果</returns>
public static bool PostResponseJson(string url, string postData, out string statusCode, string ContentType = "application/json", string CodingFormat = "utf-8")
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpContent httpContent = new StringContent(postData);
httpContent.Headers.ContentType = new MediaTypeHeaderValue(ContentType);
httpContent.Headers.ContentType.CharSet = CodingFormat;
HttpClientHandler handler = new HttpClientHandler();
handler.UseProxy = false;//不加这个会非常慢
HttpClient httpClient = new HttpClient(handler);
HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
statusCode = response.StatusCode.ToString();
if (response.IsSuccessStatusCode)
{
statusCode = response.Content.ReadAsStringAsync().Result;
}
else
{
statusCode = "";
}
return response.IsSuccessStatusCode;
}
/// <summary>
/// Post请求
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="postData">请求数据</param>
/// <param name="statusCode">返回数据</param>
/// <param name="ContentType">请求类型</param>
/// <param name="CodingFormat">编码格式</param>
/// <returns>请求结果</returns>
public static bool PostResponse(string url, string postData, out string statusCode, string ContentType = "application/json", string CodingFormat = "utf-8")
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
// 对象数据转为Json数据
JsonSerializerSettings jsetting = new JsonSerializerSettings();
jsetting.NullValueHandling = NullValueHandling.Ignore;
postData = JsonConvert.SerializeObject(postData, jsetting);
HttpContent httpContent = new StringContent(postData);
httpContent.Headers.ContentType = new MediaTypeHeaderValue(ContentType);
httpContent.Headers.ContentType.CharSet = CodingFormat;
HttpClientHandler handler = new HttpClientHandler();
handler.UseProxy = false;//不加这个会非常慢
HttpClient httpClient = new HttpClient(handler);
HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
statusCode = response.StatusCode.ToString();
if (response.IsSuccessStatusCode)
{
statusCode = response.Content.ReadAsStringAsync().Result;
}
else
{
statusCode = "";
}
return response.IsSuccessStatusCode;
}
/// <summary>
/// Post请求
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="postData">请求数据</param>
/// <param name="statusCode">返回数据</param>
/// <param name="ContentType">请求类型</param>
/// <param name="CodingFormat">编码格式</param>
/// <returns>请求结果</returns>
public static bool PostResponse(string url, string postData, out dynamic statusCode, string ContentType = "application/json", string CodingFormat = "utf-8")
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
// 对象数据转为Json数据
JsonSerializerSettings jsetting = new JsonSerializerSettings();
jsetting.NullValueHandling = NullValueHandling.Ignore;
string jsondata = JsonConvert.SerializeObject(postData, jsetting);
HttpContent httpContent = new StringContent(jsondata);
httpContent.Headers.ContentType = new MediaTypeHeaderValue(ContentType);
httpContent.Headers.ContentType.CharSet = CodingFormat;
HttpClientHandler handler = new HttpClientHandler();
handler.UseProxy = false;//不加这个会非常慢
HttpClient httpClient = new HttpClient(handler);
HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
if (response.IsSuccessStatusCode)
{
Task<string> t = response.Content.ReadAsStringAsync();
string s = t.Result;
statusCode = JsonConvert.DeserializeObject<dynamic>(s);
}
else
{
statusCode = null;
}
return response.IsSuccessStatusCode;
}
/// <summary>
/// Post请求
/// </summary>
/// <typeparam name="T">返回对象</typeparam>
/// <param name="url">请求地址</param>
/// <param name="postData">请求数据</param>
/// <param name="statusCode">返回数据</param>
/// <param name="ContentType">请求类型</param>
/// <param name="CodingFormat">编码格式</param>
/// <returns>请求结果</returns>
public static bool PostResponse<T>(string url, string postData, out T statusCode, string ContentType = "application/json", string CodingFormat = "utf-8")
where T : class, new()
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
// 对象数据转为Json数据
JsonSerializerSettings jsetting = new JsonSerializerSettings();
jsetting.NullValueHandling = NullValueHandling.Ignore;
string jsondata = JsonConvert.SerializeObject(postData, jsetting);
HttpContent httpContent = new StringContent(jsondata);
httpContent.Headers.ContentType = new MediaTypeHeaderValue(ContentType);
httpContent.Headers.ContentType.CharSet = CodingFormat;
HttpClientHandler handler = new HttpClientHandler();
handler.UseProxy = false;//不加这个会非常慢
HttpClient httpClient = new HttpClient(handler);
HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
if (response.IsSuccessStatusCode)
{
Task<string> t = response.Content.ReadAsStringAsync();
string s = t.Result;
statusCode = JsonConvert.DeserializeObject<T>(s);
}
else
{
statusCode = null;
}
return response.IsSuccessStatusCode;
}
/// <summary>
/// Post请求
/// </summary>
/// <typeparam name="T">返回对象</typeparam>
/// <param name="url">请求地址</param>
/// <param name="postData">请求数据</param>
/// <param name="statusCode">返回数据</param>
/// <param name="ContentType">请求类型</param>
/// <param name="CodingFormat">编码格式</param>
/// <returns>请求结果</returns>
public static bool PostResponse<T>(string url, T postData, out string statusCode, string ContentType = "application/json", string CodingFormat = "utf-8")
where T : class, new()
{
return PostResponse<T, string>(url, postData, out statusCode, ContentType, CodingFormat);
}
/// <summary>
/// 发送Post请求
/// </summary>
/// <typeparam name="T">请求对象</typeparam>
/// <typeparam name="T1">返回对象</typeparam>
/// <param name="url">请求地址</param>
/// <param name="postData">请求数据</param>
/// <param name="statusCode">返回数据</param>
/// <param name="ContentType">请求类型</param>
/// <param name="CodingFormat">编码格式</param>
/// <returns>请求结果</returns>
public static bool PostResponse<T, T1>(string url, T postData, out T1 statusCode, string ContentType = "application/json", string CodingFormat = "utf-8")
where T : class, new()
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
// 对象数据转为Json数据
JsonSerializerSettings jsetting = new JsonSerializerSettings();
jsetting.NullValueHandling = NullValueHandling.Ignore;
string jsondata = JsonConvert.SerializeObject(postData, jsetting);
HttpContent httpContent = new StringContent(jsondata);
httpContent.Headers.ContentType = new MediaTypeHeaderValue(ContentType);
httpContent.Headers.ContentType.CharSet = CodingFormat;
HttpClientHandler handler = new HttpClientHandler();
handler.UseProxy = false;//不加这个会非常慢
HttpClient httpClient = new HttpClient(handler);
HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
if (response.IsSuccessStatusCode)
{
Task<string> t = response.Content.ReadAsStringAsync();
string s = t.Result;
statusCode = JsonConvert.DeserializeObject<T1>(s);
}
else
{
statusCode = default(T1);
}
if (!response.IsSuccessStatusCode)
{
throw new Exception("请求失败。" + jsondata);
}
return response.IsSuccessStatusCode;
}
/// <summary>
/// 发送Post请求
/// </summary>
/// <typeparam name="T">请求对象</typeparam>
/// <typeparam name="T1">返回对象</typeparam>
/// <param name="url">请求地址</param>
/// <param name="postData">请求数据</param>
/// <param name="statusCode">返回数据</param>
/// <param name="ContentType">请求类型</param>
/// <param name="CodingFormat">编码格式</param>
/// <returns>请求结果</returns>
/// <returns></returns>
public static bool PostResponse<T, T1>(string url, T postData, out List<T1> statusCode, string ContentType = "application/json", string CodingFormat = "utf-8")
where T : class, new()
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
// 对象数据转为Json数据
JsonSerializerSettings jsetting = new JsonSerializerSettings();
jsetting.NullValueHandling = NullValueHandling.Ignore;
string jsondata = JsonConvert.SerializeObject(postData, jsetting);
HttpContent httpContent = new StringContent(jsondata);
httpContent.Headers.ContentType = new MediaTypeHeaderValue(ContentType);
httpContent.Headers.ContentType.CharSet = CodingFormat;
HttpClientHandler handler = new HttpClientHandler();
handler.UseProxy = false;//不加这个会非常慢
HttpClient httpClient = new HttpClient(handler);
HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
if (response.IsSuccessStatusCode)
{
Task<string> t = response.Content.ReadAsStringAsync();
string s = t.Result;
statusCode = JsonConvert.DeserializeObject<List<T1>>(s);
}
else
{
statusCode = default(List<T1>);
}
if (!response.IsSuccessStatusCode)
{
throw new Exception("请求失败。" + jsondata);
}
return response.IsSuccessStatusCode;
}
#endregion
/// <summary>
/// V3接口全部为Xml形式,故有此方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="url"></param>
/// <param name="xmlString"></param>
/// <returns></returns>
public static bool PostXmlResponse<T>(string url, string xmlString, out T statusCode, string ContentType = "application/json", string CodingFormat = "utf-8")
where T : class, new()
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpContent httpContent = new StringContent(xmlString);
httpContent.Headers.ContentType = new MediaTypeHeaderValue(ContentType);
httpContent.Headers.ContentType.CharSet = CodingFormat;
HttpClientHandler handler = new HttpClientHandler();
handler.UseProxy = false;//不加这个会非常慢
HttpClient httpClient = new HttpClient(handler);
HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
if (response.IsSuccessStatusCode)
{
Task<string> t = response.Content.ReadAsStringAsync();
string s = t.Result;
statusCode = XmlDeserialize<T>(s);
}
else
{
statusCode = null;
}
return response.IsSuccessStatusCode;
}
/// <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);
}
}
}
}
整个代码都在这了!有需要的直接拿走就行了!多个方法重载