网站改版 报价,东莞专业的网站推广价格,a站下载,秀米h5制作教程C# 中 Timeout 的处理前言最近在项目中要实现一个功能#xff0c;是关于 Timeout 的#xff0c;主要是要在要在 TCP 连接建立的时间 和 整个请求完成的时间#xff0c;在这两个时间层面上#xff0c;如果超出了设置的时间#xff0c;就抛出异常#xff0c;程序中断。研究… C# 中 Timeout 的处理前言最近在项目中要实现一个功能是关于 Timeout 的主要是要在要在 TCP 连接建立的时间 和 整个请求完成的时间在这两个时间层面上如果超出了设置的时间就抛出异常程序中断。研究了一下项目的代码中发现在使用HTTP协议发送请求时主要用的是微软的 Microsoft.Net.HttpWebRequest 这个类来发起请求和接收请求的。当时我隐约记得这个类怎么有点熟悉呀好像还有 WebRequst 和 HttpClient 这两个把还没开始真正开始去了解Timeout在HttpWebRequest中 如何实现的我先去看了看这三者到底有何不同WebRequest HttpWebRequest HttpClientWebRequest 是 一个抽象类是HttpWebRequest 的父类。是.NET中请求和获取网络中的数据的一个类。HttpWebRequest 是WebRequest 的一个实现不仅对WebRequest中的属性和方法进行了支持而且还有额外的方法通过Http协议来和服务端交互。上面那两个现在在微软官方文档上都不推荐使用了现在所推荐的是 HttpClient。由于项目中遗留之前使用的是HttpWebRequest所以就在原来的基础上进行实现何况的是在HttpClient中没有找到TCP连接建立的时间属性的设定。HttpClient 优点自不必多说连接池一次初始化整个生命周期的重用和 .Net Core 的融合以及性能的提升等等虽然说性能可能提升了如果你这样用那也是凉凉using(HttpClient clinet new HttpClient()){var result await client.GetAsync(http://aspnetmonsters.com); Console.WriteLine(result.StatusCode);}用完 Using 后调用了IDispose接口。那下次还是会重新初始化这样使用就没问题了private static HttpClient Client new HttpClient();Timeout, ReadWriteTimeout也可能是我英文的理解能力有点差在开始我就注意到这个类里面的这两个属性但是我如何也无法和 TCP 建立前所用的连接时间 与 请求完成的时间联系起来。微软官方文档解释如下TimeoutTimeout is the number of milliseconds that a subsequent synchronous request made with the GetResponse method waits for a response, and the GetRequestStream method waits for a stream. The Timeout applies to the entire request and response, not individually to the GetRequestStreamand GetResponse method calls. If the resource is not returned within the time-out period, the request throws a WebException with the Status property set to WebExceptionStatus.Timeout.The Timeout property has no effect on asynchronous requests made with the BeginGetResponse or BeginGetRequestStream method.ReadWriteTimeout:The ReadWriteTimeout property is used when writing to the stream returned by the GetRequestStream method or reading from the stream returned by the GetResponseStream method.Specifically, the ReadWriteTimeout property controls the time-out for the Read method, which is used to read the stream returned by the GetResponseStream method, and for the Write method, which is used to write to the stream returned by the GetRequestStream method.设置其实是很方便的如下所示HttpWebRequest request (HttpWebRequest)WebRequest.Creat(your url)request.Timeout 1000; request.ReadWriteTimeout 3000;HttpWebResponse response (HttpWebResponse)request.GetResponse();WebClient.Exception.Timeout 和 OperationCanceledException最后在捕捉异常的时候发现了一个很奇怪的地方就是使用两段代码却抛出了不同的异常第一段代码HttpWebRequest request (HttpWebRequest) WebRequest.Create(https://www.alibabacloud.com);request.Timeout 5000;HttpWebResponse response (HttpWebResponse) request.GetResponse();HttpWebRequest request2 (HttpWebRequest) WebRequest.Create(https://www.cnblogs.com);request2.Timeout 1; HttpWebResponse response2 (HttpWebResponse) request2.GetResponse();第二段HttpWebRequest request (HttpWebRequest) WebRequest.Create(https://www.alibabacloud.com);request.Timeout 5000;HttpWebResponse response (HttpWebResponse) request.GetResponse();request (HttpWebRequest) WebRequest.Create(https://www.cnblogs.com);request.Timeout 1;response (HttpWebResponse) request.GetResponse();初步估计的原因是Http 请求中 Keep-Alive的关系还有一个可能是 Timeout 的这个说明The Timeout applies to the entire request and response, not individually to the GetRequestStream and GetResponse method calls. 然而具体的还没搞清楚还需要去验证。后记其实设置这两个属性你可能只需要找到文档分分钟就可以搞定但是我为什么会在这个过程遇到这些问题呢一方面是对于C# 中网络请求的不熟悉其次是找个时间把以前的代码需要重构一下了比如把HttpWebRequest 换成 HttpClient 等等。也让我加深了对Http协议的理解。原文地址https://www.cnblogs.com/xiyin/p/10548319.html.NET社区新闻深度好文欢迎访问公众号文章汇总 http://www.csharpkit.com