在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace Interfaces_Helper { public delegate string DoHandler(string HttpItemXml); public class TimeOut { private ManualResetEvent mTimeoutObject; //标记变量 private bool mBoTimeout; public string _Data = string.Empty; public DoHandler Do; public TimeOut() { // 初始状态为 停止 this.mTimeoutObject = new ManualResetEvent(true); } ///<summary> /// 指定超时时间 异步执行某个方法 ///</summary> ///<returns>执行 是否超时</returns> public bool DoWithTimeout(TimeSpan timeSpan, string HttpItemXml) { if (this.Do == null) { return false; } this.mTimeoutObject.Reset(); this.mBoTimeout = true; //标记 this.Do.BeginInvoke(HttpItemXml, DoAsyncCallBack, null); // 等待 信号Set if (!this.mTimeoutObject.WaitOne(timeSpan, false)) { this.mBoTimeout = true; } return this.mBoTimeout; } ///<summary> /// 异步委托 回调函数 ///</summary> ///<param name="result"></param> private void DoAsyncCallBack(IAsyncResult result) { try { _Data = this.Do.EndInvoke(result); // 指示方法的执行未超时 this.mBoTimeout = false; } catch (Exception ex) { this.mBoTimeout = true; } finally { this.mTimeoutObject.Set(); } } } }
//调用 string HttpItemXml = XmlUtil.ToBinary<HttpItem_>(item_);//参数 TimeOut timeout = new TimeOut(); timeout.Do = DoHttpCs;//要执行的方法名 bool IsTimeOut = timeout.DoWithTimeout(new TimeSpan(0, 0, 0, 600), HttpItemXml); if (IsTimeOut) { //请求超时 //... } else { //请求未超时 } private string DoHttpCs(string HttpItemXml) { // 休眠 5秒 //System.Threading.Thread.Sleep(new TimeSpan(0, 0, 0, 3)); string Data = string.Empty; HttpItem_ item_ = XmlUtil.FromBinary<HttpItem_>(HttpItemXml); item_.CerPath = GetCerPath(item_.ProductCode); item_.Method = GetMethod(item_.IsPost); item_.ContentType = GetContentType(item_.ContentType); item_.Postdata = GetEncode(item_.IsEncode, item_.ProductCode, item_.Postdata); HttpResult_ result_ = null; Encoding PostEncoding_ = GetPostEncoding(item_.IsEncode, item_.ProductCode); HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = item_.URL,//URL 必需项 Method = item_.Method,//URL 可选项 默认为Get Cookie = item_.Cookie,//字符串Cookie 可选项 Postdata = item_.Postdata,//Post数据 可选项GET时不需要写 //Timeout = item_.Timeout,//连接超时时间 可选项默认为100000 //ReadWriteTimeout = item_.ReadWriteTimeout,//写入Post数据超时时间 可选项默认为30000 ContentType = item_.ContentType,//返回类型 可选项有默认值 CerPath = item_.CerPath,//证书绝对路径 可选项不需要证书时可以不写这个参数 Connectionlimit = item_.Connectionlimit,//最大连接数 可选项 默认为1024 Expect100Continue = item_.Expect100Continue, PostEncoding = PostEncoding_,//设置或获取Post参数编码,默认的为Default编码(平安用UTF-8) ResultType = item_.ResultType,//设置返回类型String和Byte ProxyIp = item_.Channel//代理服务器ID 可选项 不需要代理 时可以不设置这三个参数 }; HttpResult result = null; try { result = http.GetHtml(item); result_ = new HttpResult_(); result_.Html = result.Html; result_.Cookie = result.Cookie; result_.ServiceCookie = item_.Cookie; result_.ResultByte = result.ResultByte; if (result.Header != null && result.Header.Count > 0) { if (result.Header["Location"] != null) { result_.Header_Location = result.Header["Location"].ToString(); } if (result.Header["x-iCore_fa-digest"] != null) { result_.x_iCore_fa_digest = result.Header["x-iCore_fa-digest"].ToString(); } } if (result_.Html.Contains("302 Moved Temporarily") && item_.UseServerCookie == true) { Data = "X_C_Er_0"; } else if (result_.Html.Contains("无法连接到远程服务器")) { Data = "X_C_Er_1"; } else { Data = result_.Html; //Data = XmlUtil.ToBinary<HttpResult_>(result_); } return Data; } catch (Exception ex) { result_ = new HttpResult_(); result_.Html = ex.Message; Data = XmlUtil.ToBinary<HttpResult_>(result_); return Data; } finally { Global.ClearMemory(); } }
|
请发表评论