SVN r1234
SVN-Revision: r1234
This commit is contained in:
+103
-6
@@ -12,6 +12,9 @@ namespace Lskj.Util
|
||||
{
|
||||
public static class HttpHelper
|
||||
{
|
||||
private const int RequestTimeout = 120000;
|
||||
private const int Tls12 = 3072;
|
||||
|
||||
public enum Encode
|
||||
{
|
||||
Default = 0,
|
||||
@@ -28,6 +31,76 @@ namespace Lskj.Util
|
||||
UserAgent = userAgent;
|
||||
EncodeMode = encode;
|
||||
}
|
||||
|
||||
private static void ConfigureTransport()
|
||||
{
|
||||
try
|
||||
{
|
||||
ServicePointManager.SecurityProtocol |= (SecurityProtocolType)Tls12;
|
||||
ServicePointManager.Expect100Continue = false;
|
||||
if (ServicePointManager.DefaultConnectionLimit < 20)
|
||||
{
|
||||
ServicePointManager.DefaultConnectionLimit = 20;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private static void ConfigureRequest(HttpWebRequest request)
|
||||
{
|
||||
request.Timeout = RequestTimeout;
|
||||
request.ReadWriteTimeout = RequestTimeout;
|
||||
request.KeepAlive = false;
|
||||
request.ServicePoint.Expect100Continue = false;
|
||||
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
|
||||
}
|
||||
|
||||
private static string BuildExceptionMessage(Exception ex)
|
||||
{
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.Append(ex.Message);
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
builder.AppendFormat("; Inner={0}", ex.InnerException.Message);
|
||||
}
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
private static string BuildWebExceptionMessage(WebException ex)
|
||||
{
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.Append(BuildExceptionMessage(ex));
|
||||
builder.AppendFormat("; Status={0}", ex.Status);
|
||||
|
||||
HttpWebResponse response = ex.Response as HttpWebResponse;
|
||||
if (response != null)
|
||||
{
|
||||
builder.AppendFormat("; HttpStatus={0} {1}", (int)response.StatusCode, response.StatusDescription);
|
||||
try
|
||||
{
|
||||
Stream responseStream = response.GetResponseStream();
|
||||
if (responseStream != null)
|
||||
{
|
||||
using (StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8))
|
||||
{
|
||||
string responseText = streamReader.ReadToEnd();
|
||||
if (!string.IsNullOrEmpty(responseText))
|
||||
{
|
||||
builder.AppendFormat("; Response={0}", responseText);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// post数据到指定的网址,获取cookie数据,和返回页
|
||||
/// </summary>
|
||||
@@ -37,7 +110,9 @@ namespace Lskj.Util
|
||||
try
|
||||
{
|
||||
HttpWebRequest httpWebRequest;
|
||||
ConfigureTransport();
|
||||
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
|
||||
ConfigureRequest(httpWebRequest);
|
||||
|
||||
httpWebRequest.ContentType = ContentType;
|
||||
httpWebRequest.Accept = Accept;
|
||||
@@ -105,10 +180,16 @@ namespace Lskj.Util
|
||||
|
||||
return httpWebResponse;
|
||||
}
|
||||
catch (WebException ex)
|
||||
{
|
||||
Console.WriteLine(ex.ToString());
|
||||
result = BuildWebExceptionMessage(ex);
|
||||
return httpWebResponse;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
result = ex.Message;
|
||||
Console.WriteLine(ex.ToString());
|
||||
result = BuildExceptionMessage(ex);
|
||||
return httpWebResponse;
|
||||
}
|
||||
}
|
||||
@@ -134,7 +215,9 @@ namespace Lskj.Util
|
||||
}
|
||||
}
|
||||
HttpWebRequest httpWebRequest;
|
||||
ConfigureTransport();
|
||||
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(builder.ToString());
|
||||
ConfigureRequest(httpWebRequest);
|
||||
httpWebRequest.ContentType = ContentType;
|
||||
//httpWebRequest.Referer = url;
|
||||
httpWebRequest.Accept = Accept;
|
||||
@@ -163,10 +246,16 @@ namespace Lskj.Util
|
||||
|
||||
return httpWebResponse;
|
||||
}
|
||||
catch (WebException ex)
|
||||
{
|
||||
Console.WriteLine(ex.ToString());
|
||||
result = BuildWebExceptionMessage(ex);
|
||||
return httpWebResponse;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
result = ex.Message;
|
||||
Console.WriteLine(ex.ToString());
|
||||
result = BuildExceptionMessage(ex);
|
||||
return httpWebResponse;
|
||||
}
|
||||
}
|
||||
@@ -186,7 +275,9 @@ namespace Lskj.Util
|
||||
{
|
||||
string boundary = "----WebKitFormBoundary" + DateTime.Now.Ticks.ToString("x");
|
||||
|
||||
ConfigureTransport();
|
||||
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
|
||||
ConfigureRequest(httpWebRequest);
|
||||
httpWebRequest.Method = "POST";
|
||||
httpWebRequest.Accept = Accept;
|
||||
httpWebRequest.UserAgent = UserAgent;
|
||||
@@ -231,10 +322,16 @@ namespace Lskj.Util
|
||||
|
||||
return httpWebResponse;
|
||||
}
|
||||
catch (WebException ex)
|
||||
{
|
||||
Console.WriteLine(ex.ToString());
|
||||
result = BuildWebExceptionMessage(ex);
|
||||
return httpWebResponse;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
result = ex.Message;
|
||||
Console.WriteLine(ex.ToString());
|
||||
result = BuildExceptionMessage(ex);
|
||||
return httpWebResponse;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user