/// <summary>
/// HTTP通信帮助类
/// </summary>
public static class HttpHelper
{
/// <summary>
/// 头部信息集合
/// </summary>
private static KeyValuePair<string, string> _routeHeaders = new KeyValuePair<string, string>("AgencyType", "2");
#region 对外调用Http方法
/// <summary>
/// HTTPGet通讯
/// </summary>
/// <param name="url">地址</param>
/// <param name="data">数据</param>
/// <param name="headers">票据相关</param>
/// <returns>结果</returns>
public static string RouteHttpGet(string url, object data, params KeyValuePair<string, string>[] headers)
{
if (headers==null|| headers.Length==0)
{
return HttpGet(url, data, _routeHeaders);
}
else
{
var arr = headers.ToList();
arr.Add(_routeHeaders);
return HttpGet(url, data, arr.ToArray());
}
}
/// <summary>
/// HttpPost通讯
/// </summary>
/// <param name="url">地址</param>
/// <param name="data">数据</param>
/// <param name="headers">票据相关</param>
/// <returns>结果</returns>
public static string RouteHttpPost(string url, object data, params KeyValuePair<string, string>[] headers)
{
if (headers == null || headers.Length == 0)
{
return HttpPost(url, data, _routeHeaders);
}
else
{
var arr = headers.ToList();
arr.Add(_routeHeaders);
return HttpPost(url, data, arr.ToArray());
}
}
/// <summary>
/// HttpPatch通讯
/// </summary>
/// <param name="url">地址</param>
/// <param name="data">数据</param>
/// <param name="headers">票据相关</param>
/// <returns>结果</returns>
public static string RouteHttpPatch(string url, object data, params KeyValuePair<string, string>[] headers)
{
if (headers == null || headers.Length == 0)
{
return HttpPatch(url, data, _routeHeaders, _routeHeaders);
}
else
{
var arr = headers.ToList();
arr.Add(_routeHeaders);
return HttpPatch(url, data,arr.ToArray());
}
}
/// <summary>
/// HttpDelete通讯
/// </summary>
/// <param name="url">地址</param>
/// <param name="data">数据</param>
/// <param name="headers">票据相关</param>
/// <returns>结果</returns>
public static string RouteHttpDelete(string url, object data, params KeyValuePair<string, string>[] headers)
{
if (headers == null || headers.Length == 0)
{
return HttpDelete(url, data, _routeHeaders, _routeHeaders);
}
else
{
var arr = headers.ToList();
arr.Add(_routeHeaders);
return HttpDelete(url, data,arr.ToArray());
}
}
#endregion
#region 调用Http实现
/// <summary>
/// Get通讯方式进行通讯
/// </summary>
/// <param name="url">地址</param>
/// <param name="data">数据</param>
/// <param name="headers">头部信息</param>
/// <returns></returns>
private static string HttpGet(string url, object data, params KeyValuePair<string, string>[] headers)
{
return HttpAction(url, "GET", data, headers);
}
/// <summary>
/// Post通讯方式进行通讯
/// </summary>
/// <param name="url">地址</param>
/// <param name="data">数据</param>
/// <param name="headers">头部信息</param>
/// <returns></returns>
private static string HttpPost(string url, object data, params KeyValuePair<string, string>[] headers)
{
return HttpAction(url, "POST", data, headers);
}
/// <summary>
/// Patch通讯方式通讯
/// </summary>
/// <param name="url">地址</param>
/// <param name="data">数据</param>
/// <param name="headers">头部信息</param>
/// <returns></returns>
private static string HttpPatch(string url, object data, params KeyValuePair<string, string>[] headers)
{
return HttpAction(url, "PATCH", data, headers);
}
/// <summary>
/// Delete通讯方式进行通讯
/// </summary>
/// <param name="url">地址</param>
/// <param name="data">数据</param>
/// <param name="headers">头部信息</param>
/// <returns></returns>
private static string HttpDelete(string url, object data, params KeyValuePair<string, string>[] headers)
{
return HttpAction(url, "DELETE", data, headers);
}
/// <summary>
/// Http操作
/// </summary>
/// <param name="url">网址</param>
/// <param name="method">类型</param>
/// <param name="data">Post的数据</param>
/// <param name="headers">头文件</param>
/// <returns>返回的值</returns>
public static string HttpAction(string url, string method, object data, params KeyValuePair<string, string>[] headers)
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Headers.Set("Pragma", "no-cache");
request.Method = method;
request.ContentType = "application/json; charset=utf-8";
request.Accept = "*/*";
request.KeepAlive = false;
if (headers.Length > 0)
{
foreach (var item in headers)
{
request.Headers.Add(item.Key, item.Value);
}
}
//Post数据
if (data != null)
{
string postValue = JsonConvert.SerializeObject(data);
byte[] byteArray = Encoding.UTF8.GetBytes(postValue);
using (Stream newStream = request.GetRequestStream())
{
newStream.Write(byteArray, 0, byteArray.Length);
}
}
string result;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Stream stream = response.GetResponseStream();
if (!string.IsNullOrEmpty(response.ContentEncoding) && response.ContentEncoding.ToLower().Contains("gzip"))
{
//gzip格式
stream = new GZipStream(stream, CompressionMode.Decompress);
}
using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
{
result = sr.ReadToEnd();
}
stream.Dispose();
stream = null;
}
return result;
}
#endregion
}
c#_Http通讯帮助类
最新推荐文章于 2024-01-02 10:32:36 发布