兼容http与https请求的公共类新鲜出炉(小白上路,老司机绕道)…
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace OneCardEduWF.zbscxy.common
{
class HttpUtils
{
public static bool sendHttpGet(out string redata, string serviceAddress, string reqToken)
{
try
{
if (serviceAddress.Length <= 0)
{
redata = null;
return false;
}
HttpWebRequest request = null;
if (serviceAddress.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
request = WebRequest.Create(serviceAddress) as HttpWebRequest;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request.ProtocolVersion = HttpVersion.Version11;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
request.KeepAlive = false;
ServicePointManager.CheckCertificateRevocationList = true;
ServicePointManager.DefaultConnectionLimit = 100;
ServicePointManager.Expect100Continue = false;
}
else
{
request = (HttpWebRequest)WebRequest.Create(serviceAddress);
}
if (null!=reqToken && reqToken.Length>0)
{
request.Headers.Add("Authorization", reqToken);
}
request.Method = "GET";
request.ContentType = "application/json";
request.Timeout = 300000;
request.ServicePoint.ConnectionLimit = 50;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string strResponse = string.Empty;
if ("gzip".Equals(response.ContentEncoding))
{
using (Stream responseStream = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress))
{
if (responseStream != null)
{
StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding(response.CharacterSet));
strResponse = streamReader.ReadToEnd().Trim();
}
else
{
redata = null;
return false;
}
}
}
else if ("deflate".Equals(response.ContentEncoding))
{
using (Stream responseStream = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress))
{
if (responseStream != null)
{
StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding(response.CharacterSet));
strResponse = streamReader.ReadToEnd().Trim();
}
else
{
redata = null;
return false;
}
}
}
else
{
using (Stream responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding(response.CharacterSet));
strResponse = streamReader.ReadToEnd().Trim();
}
else
{
redata = null;
return false;
}
}
}
redata = strResponse;
return true;
}
catch (Exception ex)
{
redata = null;
LogUtils.Error(serviceAddress+"---HttpUtils---sendHttpGet---error:" + ex.Message);
return false;
}
}
public static bool sendHttpPost(out string redata, string serviceAddress, string postContent, string reqToken)
{
try
{
if (serviceAddress.Length <= 0)
{
redata = null;
return false;
}
HttpWebRequest request = null;
if (serviceAddress.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
request = WebRequest.Create(serviceAddress) as HttpWebRequest;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request.ProtocolVersion = HttpVersion.Version11;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
request.KeepAlive = false;
ServicePointManager.CheckCertificateRevocationList = true;
ServicePointManager.DefaultConnectionLimit = 100;
ServicePointManager.Expect100Continue = false;
}
else
{
request = (HttpWebRequest)WebRequest.Create(serviceAddress);
}
if (null != reqToken && reqToken.Length > 0)
{
request.Headers.Add("Authorization", reqToken);
}
request.Method = "POST";
request.ContentType = "application/json";
request.Timeout = 300000;
request.ServicePoint.ConnectionLimit = 50;
string strContent = postContent;
using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
{
dataStream.Write(strContent);
dataStream.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string strResponse = string.Empty;
if ("gzip".Equals(response.ContentEncoding))
{
using (Stream responseStream = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress))
{
if (responseStream != null)
{
StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding(response.CharacterSet));
strResponse = streamReader.ReadToEnd().Trim();
}
else
{
redata = null;
return false;
}
}
}
else if ("deflate".Equals(response.ContentEncoding))
{
using (Stream responseStream = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress))
{
if (responseStream != null)
{
StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding(response.CharacterSet));
strResponse = streamReader.ReadToEnd().Trim();
}
else
{
redata = null;
return false;
}
}
}
else
{
using (Stream responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding(response.CharacterSet));
strResponse = streamReader.ReadToEnd().Trim();
}
else
{
redata = null;
return false;
}
}
}
redata = strResponse;
return true;
}
catch (Exception ex)
{
redata = null;
LogUtils.Error(serviceAddress + "---HttpUtils---sendHttpPost---error:" + ex.Message);
return false;
}
}
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
}
}
亲测有效
|