public static string SendRequestUTF8(string url, string para, string method, int timeout, string ContentType, ref string Status, string Tag, string LogName)
{
Status = "OK";
string strResult = "";
if (url == null || url == "")
return null;
if (method == null || method == "")
method = "GET";
// GET方式
if (method.ToUpper() == "GET")
{
try
{
System.GC.Collect();
//设置最大连接数
ServicePointManager.DefaultConnectionLimit = 200;
//设置https验证方式
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(CheckValidationResult);
}
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
System.Net.WebRequest wrq = System.Net.WebRequest.Create(url + para);
wrq.Method = "GET";
if (timeout != 0)
{
wrq.Timeout = timeout;
}
using (System.Net.WebResponse wrp = wrq.GetResponse())
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(wrp.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8")))
{
strResult = sr.ReadToEnd();
sr.Close();
sr.Dispose();
}
wrp.Close();
}
wrq.Abort();
}
catch (WebException ex)
{
if (LogName != null)
{
T9.Util.LogUtil.WriteLog("SendRequestUTF8-发生未知错误" + (Tag != null ? "[" + Tag + "]" : "") + ":\r\n" + ex.Message.ToString() + "\r\n报错详情:" + ex.StackTrace.ToString()
, LogName);
}
Status = ex.Status.ToString();
return ex.Message;
}
catch (Exception ex)
{
if (LogName != null)
{
T9.Util.LogUtil.WriteLog("SendRequestUTF8-发生未知错误" + (Tag != null ? "[" + Tag + "]" : "") + ":\r\n" + ex.Message.ToString() + "\r\n报错详情:" + ex.StackTrace.ToString()
, LogName);
}
Status = "T9_9999";
return ex.Message;
}
}
// POST方式
else if (method.ToUpper() == "POST")
{
try
{
if (para.Length > 0 && para.IndexOf('?') == 0)
{
para = para.Substring(1);
}
WebRequest req = WebRequest.Create(url);
req.Method = "POST";
if (timeout != 0)
{
req.Timeout = timeout;
}//application/x-www-form-urlencoded
req.ContentType = string.IsNullOrWhiteSpace(ContentType) ? "application/x-www-form-urlencoded" : ContentType;
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(CheckValidationResult);
}
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = { '?', '=', '&' };
byte[] SomeBytes = null;
if (para != null)
{
int i = 0, j;
while (i < para.Length)
{
j = para.IndexOfAny(reserved, i);
if (j == -1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(para.Substring(i, para.Length - i), System.Text.Encoding.GetEncoding("utf-8")));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(para.Substring(i, j - i), System.Text.Encoding.GetEncoding("utf-8")));
UrlEncoded.Append(para.Substring(j, 1));
i = j + 1;
}
SomeBytes = Encoding.Default.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
else
{
req.ContentLength = 0;
}
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
WebResponse result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
Byte[] read = new Byte[512];
int bytes = ReceiveStream.Read(read, 0, 512);
while (bytes > 0)
{
// 注意:
// 下面假定响应使用 UTF-8 作为编码方式。
// 如果内容以 ANSI 代码页形式(例如,932)发送,则使用类似下面的语句:
// Encoding encode = System.Text.Encoding.GetEncoding("shift-jis");
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
strResult += encode.GetString(read, 0, bytes);
bytes = ReceiveStream.Read(read, 0, 512);
}
return strResult;
}
catch (WebException ex)
{
if (LogName != null)
{
T9.Util.LogUtil.WriteLog("SendRequestUTF8-发生未知错误" + (Tag != null ? "[" + Tag + "]" : "") + ":\r\n" + ex.Message.ToString() + "\r\n报错详情:" + ex.StackTrace.ToString()
, LogName);
}
Status = ex.Status.ToString();
if (ex.Response != null)
{
Stream ReceiveStream = ex.Response.GetResponseStream();
Byte[] read = new Byte[512];
int bytes = ReceiveStream.Read(read, 0, 512);
while (bytes > 0)
{
// 注意:
// 下面假定响应使用 UTF-8 作为编码方式。
// 如果内容以 ANSI 代码页形式(例如,932)发送,则使用类似下面的语句:
// Encoding encode = System.Text.Encoding.GetEncoding("shift-jis");
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
strResult += encode.GetString(read, 0, bytes);
bytes = ReceiveStream.Read(read, 0, 512);
}
}
return strResult;
}
catch (Exception ex)
{
if (LogName != null)
{
T9.Util.LogUtil.WriteLog("SendRequestUTF8-发生未知错误" + (Tag != null ? "[" + Tag + "]" : "") + ":\r\n" + ex.Message.ToString() + "\r\n报错详情:" + ex.StackTrace.ToString()
, LogName);
}
Status = "T9_9999";
return ex.Message;
}
}
return strResult;
}
C#-HTTP请求GET
最新推荐文章于 2024-03-18 09:13:37 发布