C# 超时类

时间:2022-11-30 13:52:36
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 时间管理——概念、注意点&lpar;一&rpar;【转】

    转自: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. Renderer&period;materials

    修改方法 meshBody.renderer.materials[].mainTexture= clothes[]; meshBody.renderer.materials[]=maters[]; 以 ...

  2. SweetAlert2 使用教程

    SweetAlert2是一款功能强大的纯Js模态消息对话框插件.SweetAlert2用于替代浏览器默认的弹出对话框,它提供各种参数和方法,支持嵌入图片,背景,HTML标签等,并提供5种内置的情景类, ...

  3. 利用CCProxy管理小型企业的上网行为

    本实验以实例方式,从操作条件.背景.需求.以及具体要求的几个部分进行说明. 1. 操作条件: 装有Windows Server 2003系统,安装了代理服务程序的虚拟机一台 2. 背景: 为了提高员工 ...

  4. 调用支付宝接口Android客户端没有支付宝APP的情况下解决无法调用支付宝页面的问题

    这几天一直研究支付宝接口调用,因为当前应用中需要调用支付宝接口作移动支付. 遇到一个问题困扰几天,就是当我们的手机端未安装支付宝APP的时候,需要在自己应用中调用支付宝的登陆网页进行支付.我是Andr ...

  5. &lpar;转&rpar;收集:Hibernate中常见问题 No row with the given identifier exists问题的原因及解决

    Hibernate中No row with the given identifier exists问题的原因及解决 产生此问题的原因: 有两张表,table1和table2.产生此问题的原因就是tab ...

  6. WEKA使用教程&lpar;经典教程转载&rpar;

    http://blog.csdn.net/yangliuy/article/details/7589306 WEKA使用教程(经典教程转载) 标签: lift算法csv数据挖掘class任务 2012 ...

  7. Contains Duplicate leetcode

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  8. web前端面试题 -- 2019最新&comma;最全

    最近在找工作,面试了好多家公司,结果都不怎么理想.要么公司环境氛围不行,要么工资达不到理想的薪资.大部分公司对程序员的面试流程几乎都一样,来了先填一份登记表,写一套面试题,然后技术面,人事面.至于有的 ...

  9. ionic2 隐藏滚动条

    方法 在全局样式,即app.scss里添加样式: ::-webkit-scrollbar { display: none !important; }

  10. Docker for windows10 配置阿里云镜像

    到官网下载 并且 安装 Docker for windows  (注意 官方要求 windows10 是企业版才行  天朝你懂的 ) 关于 Docker for windows  要求有  带有 hy ...