C# 超时类
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();
} }
C# 超时类的更多相关文章
- HttpStack及其实现类
HttpStack及其实现类 前两篇已经对网络请求流程已经梳理了个大概,这次我们着重看一下HttpStack和它的其实现类.我们之前在Network篇讲过它仅有一个实现类,而今天我们讲的HttpSta ...
- 谷歌Volley网络框架讲解——HttpStack及其实现类
前两篇已经对网络请求流程已经梳理了个大概,这次我们着重看一下HttpStack和它的其实现类.我们之前在Network篇讲过它仅有一个实现类,而今天我们讲的HttpStack有两个实现类. 其中Htt ...
- Java类的继承与多态特性-入门笔记
相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...
- linux 时间管理——概念、注意点(一)【转】
转自:http://www.cnblogs.com/openix/p/3324243.html 参考:1.http://bbs.eyeler.com/thread-69-1-1.html ...
- gevent程序员指南
gevent程序员指南 由Gevent社区编写 gevent是一个基于libev的并发库.它为各种并发和网络相关的任务提供了整洁的API. 介绍 本指南假定读者有中级Python水平,但不要求有其 ...
- netty的数据通信之心跳检测
问题1:我们想实现客户端和服务端建立连接之后,5秒钟之后如果没有数据传输就关闭与客户端的连接. 解决办法:在服务端加上下面一条代码 ch.pipeline().addLast(new ReadTime ...
- 仿LOL项目开发第一天
---恢复内容开始--- 仿LOL项目开发第一天 by---草帽 项目源码研究群:539117825 最近看了一个类似LOL的源码,颇有心得,所以今天呢,我们就来自己开发一个类似于LOL的游戏demo ...
- gevent For the Working Python Developer
Gevent指南 gevent程序员指南 由Gevent社区编写 gevent是一个基于libev的并发库.它为各种并发和网络相关的任务提供了整洁的API. 介绍 贡献者 核心部分 Greenle ...
- Scala具体解释---------Scala是什么?可伸展的语言!
Scala是什么 Scala语言的名称来自于"可伸展的语言". 之所以这样命名,是由于他被设计成随着使用者的需求而成长.你能够把Scala应用在非常大范围的编程任务上.从写个小脚本 ...
随机推荐
- html插入视频
http://www.jb51.net/web/168548.html http://www.w3school.com.cn/html/html_media.asp
- UserSelector兼容
1.更新到asp.net2.0或以上,将Microsoft.Web.UI.TreeView更换为新的System.Web.UI.WebControls.TreeView 2.将UserId,UserT ...
- 关于Win7 64位 mysql 5.7下载安装问题
1.从官网下载mysql: 网址:http://dev.mysql.com/downloads/mysql/ 这是我们要找的,win7 64位 点击下载: 出现如图所示,我们不必要登录注册,点击红线内 ...
- 使用MeanJS Yeoman Generator
1.首先全局安装该生成器 sudo npm install -g generator-meanjs 2.为项目创建一个路径 mkdir xmen && cd xmen 3.创建app ...
- Linux软件管理——yum命令详解
yum install <softwarename> #安装指定软件 yum remove <softwarename> #卸载指定软件 yum update <soft ...
- Codeforces 745C:Hongcow Builds A Nation(并查集)
http://codeforces.com/problemset/problem/744/A 题意:在一个图里面有n个点m条边,还有k个点是受限制的,即不能从一个受限制的点走到另外一个受限制的点(有路 ...
- ab测试大并发错误
转载自http://xmarker.blog.163.com/blog/static/226484057201462263815783 apache 自带的ab工具测试,当并发量达到1000多的时候报 ...
- 仿iOS底部弹出popUpWindow
上面为弹出来的效果 popUpWindow布局: <?xml version="1.0" encoding="utf-8"?> <Linear ...
- tar 报错gzip: stdin: not in gzip format
今天在linux下 用tar -zxvf xxx.tar.bz2 然后就报这个错. gzip: stdin: not in gzip formattar: Child returned status ...
- YTU 3001: 判断操作是否合法(栈和队列)
3001: 判断操作是否合法(栈和队列) 时间限制: 1 Sec 内存限制: 128 MB 提交: 4 解决: 2 题目描述 假设以I和O分别表示进栈和出栈操作,栈的初态和终态均为空,进栈和出栈的 ...