|
@@ -25,7 +25,7 @@ namespace SufeiUtil
|
|
|
{
|
|
|
#region 后台进程;
|
|
|
static int ms_ProcessID = -1;
|
|
|
- static int ms_ProcessID2= -1;
|
|
|
+ static int ms_ProcessID2 = -1;
|
|
|
private static readonly object ms_Lock = new object();
|
|
|
public static bool Tracert = false;
|
|
|
private void CallTracertBat()
|
|
@@ -46,7 +46,7 @@ namespace SufeiUtil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if ( ms_ProcessID == -1 )
|
|
|
+ if (ms_ProcessID == -1)
|
|
|
{
|
|
|
Process bat = new Process();
|
|
|
bat.StartInfo.FileName = "route-tracert.bat";
|
|
@@ -56,7 +56,7 @@ namespace SufeiUtil
|
|
|
// 记录进程id;
|
|
|
ms_ProcessID = bat.Id;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
Monitor.Exit(ms_Lock);
|
|
|
}
|
|
|
private void CallPingBat()
|
|
@@ -112,13 +112,27 @@ namespace SufeiUtil
|
|
|
/// </summary>
|
|
|
/// <param name="item">参数类对象</param>
|
|
|
/// <returns>返回HttpResult类型</returns>
|
|
|
+ ///
|
|
|
public HttpResult GetHtml(HttpItem item)
|
|
|
{
|
|
|
+ HttpResult result = null;
|
|
|
+ for (int i = 0; i < 3; i++ )
|
|
|
+ {
|
|
|
+ if (GetHtml(item, out result))
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private bool GetHtml(HttpItem item, out HttpResult result)
|
|
|
+ {
|
|
|
+ bool bResutl = true;
|
|
|
// 计算函数耗时;
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
stopwatch.Start();
|
|
|
//返回参数
|
|
|
- HttpResult result = new HttpResult();
|
|
|
+ result = new HttpResult();
|
|
|
try
|
|
|
{
|
|
|
//准备参数
|
|
@@ -126,13 +140,16 @@ namespace SufeiUtil
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
+ bResutl = false;
|
|
|
// 停止计时;
|
|
|
- stopwatch.Stop();
|
|
|
+ stopwatch.Stop();
|
|
|
Log.WriteTimesdLog(string.Format("\r\n{0} url={1}\r\nPostdata={2}\r\nElapsed={3}ms\r\n", ex.Message, item.URL, item.Postdata, stopwatch.ElapsedMilliseconds.ToString()));
|
|
|
stopwatch.Reset();
|
|
|
//配置参数时出错
|
|
|
- return new HttpResult() { Cookie = string.Empty, Header = null, Html = ex.Message, StatusDescription = "配置参数时出错:" + ex.Message };
|
|
|
+ result = new HttpResult() { Cookie = string.Empty, Header = null, Html = ex.Message, StatusDescription = "配置参数时出错:" + ex.Message };
|
|
|
+ return bResutl;
|
|
|
}
|
|
|
+
|
|
|
try
|
|
|
{
|
|
|
//请求数据
|
|
@@ -167,28 +184,33 @@ namespace SufeiUtil
|
|
|
response = null;
|
|
|
}
|
|
|
// 停止计时;
|
|
|
- stopwatch.Stop();
|
|
|
- Log.WriteTimesdLog(string.Format("\r\nurl={0}\r\nPostdata={1}\r\nResult={2}\r\nStatus={3}\r\nElapsed={4}ms\r\n",
|
|
|
- item.URL, item.Postdata, result.Html, result.StatusDescription, stopwatch.ElapsedMilliseconds.ToString()));
|
|
|
+ stopwatch.Stop();
|
|
|
+ Log.WriteTimesdLog(string.Format("\r\nurl={0}\r\nPostdata={1}\r\nResult={2}\r\nStatus={3}\r\nElapsed={4}ms\r\nStatusCode={5}\r\n",
|
|
|
+ item.URL, item.Postdata, result.Html, result.StatusDescription, stopwatch.ElapsedMilliseconds.ToString(), result.StatusCode));
|
|
|
stopwatch.Reset();
|
|
|
|
|
|
- switch ( result.StatusCode )
|
|
|
- {
|
|
|
- case HttpStatusCode.BadRequest:
|
|
|
- case HttpStatusCode.NotFound:
|
|
|
- case HttpStatusCode.GatewayTimeout:
|
|
|
- case HttpStatusCode.BadGateway:
|
|
|
- if (Tracert)
|
|
|
- {
|
|
|
- CallPingBat();
|
|
|
- CallTracertBat();
|
|
|
- }
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
+ #region 异常时调用批处理;
|
|
|
+ //switch (result.StatusCode)
|
|
|
+ //{
|
|
|
+ // case HttpStatusCode.BadRequest:
|
|
|
+ // case HttpStatusCode.NotFound:
|
|
|
+ // case HttpStatusCode.GatewayTimeout:
|
|
|
+ // case HttpStatusCode.BadGateway:
|
|
|
+ // if (Tracert)
|
|
|
+ // {
|
|
|
+ // CallPingBat();
|
|
|
+ // CallTracertBat();
|
|
|
+ // }
|
|
|
+ // break;
|
|
|
+ // default:
|
|
|
+ // break;
|
|
|
+ //}
|
|
|
+ #endregion
|
|
|
+ // 只要大于400以上的,都认为请求失败.
|
|
|
+ if (Convert.ToInt32(result.StatusCode) >= 400)
|
|
|
+ bResutl = false;
|
|
|
|
|
|
- return result;
|
|
|
+ return bResutl;
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
@@ -329,7 +351,6 @@ namespace SufeiUtil
|
|
|
///<param name="item">参数列表</param>
|
|
|
private void SetRequest(HttpItem item)
|
|
|
{
|
|
|
-
|
|
|
// 验证证书
|
|
|
SetCer(item);
|
|
|
if (item.IPEndPoint != null)
|
|
@@ -339,10 +360,13 @@ namespace SufeiUtil
|
|
|
request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);
|
|
|
}
|
|
|
//设置Header参数
|
|
|
- if (item.Header != null && item.Header.Count > 0) foreach (string key in item.Header.AllKeys)
|
|
|
+ if (item.Header != null && item.Header.Count > 0)
|
|
|
+ {
|
|
|
+ foreach (string key in item.Header.AllKeys)
|
|
|
{
|
|
|
request.Headers.Add(key, item.Header[key]);
|
|
|
}
|
|
|
+ }
|
|
|
// 设置代理
|
|
|
SetProxy(item);
|
|
|
if (item.ProtocolVersion != null) request.ProtocolVersion = item.ProtocolVersion;
|
|
@@ -357,7 +381,7 @@ namespace SufeiUtil
|
|
|
request.Host = item.Host;
|
|
|
}
|
|
|
if (item.IfModifiedSince != null) request.IfModifiedSince = Convert.ToDateTime(item.IfModifiedSince);
|
|
|
- if (item.Date!=null)
|
|
|
+ if (item.Date != null)
|
|
|
{
|
|
|
request.Date = Convert.ToDateTime(item.Date);
|
|
|
}
|