修复BUG:WebApiTools.Get方法支持https协议
修复BUG:WebApiTools.Get方法支持https协议
支持HTTPS协议的POST方法:
支持HTTPS协议的GET方法:
C# Code:
/// <summary>
/// 客户端统一提交数据
/// </summary>
/// <param name="url">WebAPI核心URL地址</param>
/// <param name="param">URL参数</param>
/// <returns>返回数据</returns>
public static string Get(string url, string param, string contentType)
{
try
{
System.Net.HttpWebRequest request;
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.CheckCertificateRevocationList = false;
ServicePointManager.DefaultConnectionLimit = 512;
ServicePointManager.Expect100Continue = false;
request = WebRequest.Create(url + (param == "" ? "" : "?") + param) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
request.KeepAlive = false;
}
else
{
request = (System.Net.HttpWebRequest)WebRequest.Create(url + (param == "" ? "" : "?") + param);
}
//request = (HttpWebRequest)WebRequest.Create(url + (param == "" ? "" : "?") + param);
request.Method = "GET";
request.ContentType = contentType;//text/html;charset=UTF-8;
#region 获取网页内容太大的话,就加下面这两句代码
request.Headers["Accept-Encoding"] = "gzip,deflate";
request.AutomaticDecompression = DecompressionMethods.GZip;
#endregion
request.Timeout = WebApiTools.TimeOut;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
catch (WebException ex)
{
string result = "GET:操作失败!\r\n" + ex.Message;
if (ex.Response != null)
result = result + "\r\n" + GetResponseText(ex.Response);
throw new Exception(result);
}
catch (Exception ex)
{
throw new Exception("GET:操作失败!\r\n" + ex.Message);
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
/// <summary>
/// 客户端统一提交数据
/// </summary>
/// <param name="url">WebAPI核心URL地址</param>
/// <param name="param">URL参数</param>
/// <returns>返回数据</returns>
public static string Get(string url, string param, string contentType)
{
try
{
System.Net.HttpWebRequest request;
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.CheckCertificateRevocationList = false;
ServicePointManager.DefaultConnectionLimit = 512;
ServicePointManager.Expect100Continue = false;
request = WebRequest.Create(url + (param == "" ? "" : "?") + param) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
request.KeepAlive = false;
}
else
{
request = (System.Net.HttpWebRequest)WebRequest.Create(url + (param == "" ? "" : "?") + param);
}
//request = (HttpWebRequest)WebRequest.Create(url + (param == "" ? "" : "?") + param);
request.Method = "GET";
request.ContentType = contentType;//text/html;charset=UTF-8;
#region 获取网页内容太大的话,就加下面这两句代码
request.Headers["Accept-Encoding"] = "gzip,deflate";
request.AutomaticDecompression = DecompressionMethods.GZip;
#endregion
request.Timeout = WebApiTools.TimeOut;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
catch (WebException ex)
{
string result = "GET:操作失败!\r\n" + ex.Message;
if (ex.Response != null)
result = result + "\r\n" + GetResponseText(ex.Response);
throw new Exception(result);
}
catch (Exception ex)
{
throw new Exception("GET:操作失败!\r\n" + ex.Message);
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
支持HTTPS协议的POST方法:
C# Code:
/// <summary>
/// 客户端统一提交数据
/// </summary>
/// <param name="url">WebAPI核心URL地址</param>
/// <param name="data">提交的数据</param>
/// <param name="contentType">指定request.ContentType</param>
/// <returns>返回数据</returns>
public static string Post(string url, string data, string contentType)
{
string returnData = null;
try
{
string strURL = url;
System.Net.HttpWebRequest request;
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.CheckCertificateRevocationList = false;
ServicePointManager.DefaultConnectionLimit = 512;
ServicePointManager.Expect100Continue = false;
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
request.KeepAlive = false;
}
else
{
request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
}
request.Method = "POST";
request.ContentType = contentType;// "application/json;charset=UTF-8";//POST必须使用JSON格式
string paraUrlCoded = data;
byte[] payload;
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
request.ContentLength = payload.Length;
request.Timeout = WebApiTools.TimeOut;
Stream writer = request.GetRequestStream();
writer.Write(payload, 0, payload.Length);
writer.Close();
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
string StrDate = "";
string strValue = "";
StreamReader Reader = new StreamReader(s, Encoding.GetEncoding("utf-8"));
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}
returnData = strValue;
return returnData.Trim() + "\n";
}
catch (WebException ex)
{
string result = "POST:操作失败!\r\n" + ex.Message;
if (ex.Response != null)
result = result + "\r\n" + GetResponseText(ex.Response);
throw new Exception(result);
}
catch (Exception ex)
{
throw new Exception("POST:操作失败!\r\n" + ex.Message);
}
}
private static string GetResponseText(WebResponse response)
{
string text;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}
if ((response is HttpWebResponse))
{
int status = (int)(response as HttpWebResponse).StatusCode;
if (status == 429) text = "流量访问限制!" + text;
}
return text;
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
/// <summary>
/// 客户端统一提交数据
/// </summary>
/// <param name="url">WebAPI核心URL地址</param>
/// <param name="data">提交的数据</param>
/// <param name="contentType">指定request.ContentType</param>
/// <returns>返回数据</returns>
public static string Post(string url, string data, string contentType)
{
string returnData = null;
try
{
string strURL = url;
System.Net.HttpWebRequest request;
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.CheckCertificateRevocationList = false;
ServicePointManager.DefaultConnectionLimit = 512;
ServicePointManager.Expect100Continue = false;
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
request.KeepAlive = false;
}
else
{
request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
}
request.Method = "POST";
request.ContentType = contentType;// "application/json;charset=UTF-8";//POST必须使用JSON格式
string paraUrlCoded = data;
byte[] payload;
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
request.ContentLength = payload.Length;
request.Timeout = WebApiTools.TimeOut;
Stream writer = request.GetRequestStream();
writer.Write(payload, 0, payload.Length);
writer.Close();
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
string StrDate = "";
string strValue = "";
StreamReader Reader = new StreamReader(s, Encoding.GetEncoding("utf-8"));
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}
returnData = strValue;
return returnData.Trim() + "\n";
}
catch (WebException ex)
{
string result = "POST:操作失败!\r\n" + ex.Message;
if (ex.Response != null)
result = result + "\r\n" + GetResponseText(ex.Response);
throw new Exception(result);
}
catch (Exception ex)
{
throw new Exception("POST:操作失败!\r\n" + ex.Message);
}
}
private static string GetResponseText(WebResponse response)
{
string text;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}
if ((response is HttpWebResponse))
{
int status = (int)(response as HttpWebResponse).StatusCode;
if (status == 429) text = "流量访问限制!" + text;
}
return text;
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网