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# 超时类的更多相关文章

  1. HttpStack及其实现类

    HttpStack及其实现类 前两篇已经对网络请求流程已经梳理了个大概,这次我们着重看一下HttpStack和它的其实现类.我们之前在Network篇讲过它仅有一个实现类,而今天我们讲的HttpSta ...

  2. 谷歌Volley网络框架讲解——HttpStack及其实现类

    前两篇已经对网络请求流程已经梳理了个大概,这次我们着重看一下HttpStack和它的其实现类.我们之前在Network篇讲过它仅有一个实现类,而今天我们讲的HttpStack有两个实现类. 其中Htt ...

  3. Java类的继承与多态特性-入门笔记

    相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...

  4. linux 时间管理——概念、注意点(一)【转】

    转自:http://www.cnblogs.com/openix/p/3324243.html 参考:1.http://bbs.eyeler.com/thread-69-1-1.html        ...

  5. gevent程序员指南

    gevent程序员指南 由Gevent社区编写 gevent是一个基于libev的并发库.它为各种并发和网络相关的任务提供了整洁的API.   介绍 本指南假定读者有中级Python水平,但不要求有其 ...

  6. netty的数据通信之心跳检测

    问题1:我们想实现客户端和服务端建立连接之后,5秒钟之后如果没有数据传输就关闭与客户端的连接. 解决办法:在服务端加上下面一条代码 ch.pipeline().addLast(new ReadTime ...

  7. 仿LOL项目开发第一天

    ---恢复内容开始--- 仿LOL项目开发第一天 by---草帽 项目源码研究群:539117825 最近看了一个类似LOL的源码,颇有心得,所以今天呢,我们就来自己开发一个类似于LOL的游戏demo ...

  8. gevent For the Working Python Developer

    Gevent指南   gevent程序员指南 由Gevent社区编写 gevent是一个基于libev的并发库.它为各种并发和网络相关的任务提供了整洁的API. 介绍 贡献者 核心部分 Greenle ...

  9. Scala具体解释---------Scala是什么?可伸展的语言!

    Scala是什么 Scala语言的名称来自于"可伸展的语言". 之所以这样命名,是由于他被设计成随着使用者的需求而成长.你能够把Scala应用在非常大范围的编程任务上.从写个小脚本 ...

随机推荐

  1. selenium学习记录

    browser = webdriver.Firefox()browser是一个WebDriver类,常用的方法有 'add_cookie',添加cookie 'back',返回上一页 'close', ...

  2. PHP memcached 扩展的安装

    PHP memcached 扩展的安装 1.下载相关的源码包(软件版本自己选择) memcached-1.4.15 , http://memcached.org/ //Memcached服务端. me ...

  3. makefile 中 $@ $^ %< 使用【转】

    转自:http://blog.csdn.net/kesaihao862/article/details/7332528 这篇文章介绍在LINUX下进行C语言编程所需要的基础知识.在这篇文章当中,我们将 ...

  4. [转]学习 WCF (6)--学习调用WCF服务的各种方法

    转自:http://www.cnblogs.com/gaoweipeng/archive/2009/07/26/1528263.html 作者这篇博文写得很全面. 根据不同的情况,我们可以用不同的方法 ...

  5. [转]如何:在设备上安装 SQL Server Compact 3.5

    将设备连接到计算机,或者将仿真程序插入底座. 有关更多信息,请参见如何:将设备仿真程序插入底座和移除底座. 说明: 计算机上必须已安装了 Windows Mobile Device Center 或 ...

  6. 【PHP设计模式 10_ShiPeiQi.php】适配器模式

    <?php /** * [适配器模式] * 对于服务器的代码,需要不同的客户端都可以调用 * 也可能是新的程序员要修改以前程序员写的老代码 */ header("Content-typ ...

  7. POJ 2192 :Zipper(DP)

    http://poj.org/problem?id=2192 Zipper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1 ...

  8. 让popUpWindow之外的区域显示阴影效果

    /** * 让popupwindow以外区域阴影显示 * @param popupWindow */ private void popOutShadow(PopupWindow popupWindow ...

  9. Github for Windows安装

    下载软件:https://desktop.github.com/ 安装之前要求系统先要有安装.net framework 4.5,不然软件安装的时候会自动上网下载安装,这软件下载起来非常慢. 第一次操 ...

  10. 0. WP8.1学习笔记

    应用程序生命周期: 运行: 在程序NotRunning状态下点击图标,应用将处于Running状态,这会触发一个Actived事件 挂起: 在程序Running状态下, 点击返回键或win键会触发一个 ...