.net每隔几秒去请求接口 怎么做_C# .net 中 Timeout 的处理及遇到的问题
C# 中 Timeout 的處理
前言
最近在項(xiàng)目中要實(shí)現(xiàn)一個(gè)功能,是關(guān)于?Timeout?的,主要是要在要在 TCP 連接建立的時(shí)間 和 整個(gè)請(qǐng)求完成的時(shí)間,在這兩個(gè)時(shí)間層面上,如果超出了設(shè)置的時(shí)間,就拋出異常,程序中斷。
研究了一下項(xiàng)目的代碼中,發(fā)現(xiàn)在使用HTTP協(xié)議,發(fā)送請(qǐng)求時(shí),主要用的是微軟的?Microsoft.Net.HttpWebRequest?這個(gè)類(lèi)來(lái)發(fā)起請(qǐng)求和接收請(qǐng)求的。當(dāng)時(shí)我隱約記得這個(gè)類(lèi)怎么有點(diǎn)熟悉呀,好像還有?WebRequst?和?HttpClient?這兩個(gè)把,還沒(méi)開(kāi)始真正開(kāi)始去了解Timeout在HttpWebRequest中 如何實(shí)現(xiàn)的,我先去看了看這三者到底有何不同?
WebRequest , HttpWebRequest , HttpClient
WebRequest?是 一個(gè)抽象類(lèi),是HttpWebRequest?的父類(lèi)。是.NET中請(qǐng)求和獲取網(wǎng)絡(luò)中的數(shù)據(jù)的一個(gè)類(lèi)。
HttpWebRequest?是WebRequest 的一個(gè)實(shí)現(xiàn),不僅對(duì)WebRequest中的屬性和方法進(jìn)行了支持,而且還有額外的方法通過(guò)Http協(xié)議來(lái)和服務(wù)端交互。
上面那兩個(gè)現(xiàn)在在微軟官方文檔上都不推薦使用了,
現(xiàn)在所推薦的是?HttpClient。由于項(xiàng)目中遺留之前使用的是HttpWebRequest,所以就在原來(lái)的基礎(chǔ)上進(jìn)行實(shí)現(xiàn),何況的是在HttpClient中沒(méi)有找到TCP連接建立的時(shí)間屬性的設(shè)定。
HttpClient?優(yōu)點(diǎn)自不必多說(shuō):
連接池
一次初始化,整個(gè)生命周期的重用
和 .Net Core 的融合
以及性能的提升等等
雖然說(shuō)性能可能提升了,如果你這樣用,那也是涼涼
using(HttpClient clinet = new HttpClient()){
var result = await client.GetAsync("http://aspnetmonsters.com");
Console.WriteLine(result.StatusCode);
}
用完?Using?后,調(diào)用了IDispose接口。那下次還是會(huì)重新初始化
這樣使用就沒(méi)問(wèn)題了
private static HttpClient Client = new HttpClient();Timeout, ReadWriteTimeout
也可能是我英文的理解能力有點(diǎn)差,在開(kāi)始我就注意到這個(gè)類(lèi)里面的這兩個(gè)屬性,但是我如何也無(wú)法和 TCP 建立前所用的連接時(shí)間 與 請(qǐng)求完成的時(shí)間聯(lián)系起來(lái)。微軟官方文檔解釋如下:
Timeout:
Timeout?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.
設(shè)置其實(shí)是很方便的,如下所示:
HttpWebRequest request = (HttpWebRequest)WebRequest.Creat("");request.Timeout = 1000;
request.ReadWriteTimeout = 3000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
WebClient.Exception.Timeout 和 OperationCanceledException
最后在捕捉異常的時(shí)候,發(fā)現(xiàn)了一個(gè)很奇怪的地方,就是使用兩段代碼,卻拋出了不同的異常,
第一段代碼:
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();
初步估計(jì)的原因是,Http 請(qǐng)求中 Keep-Alive的關(guān)系,還有一個(gè)可能是 Timeout 的這個(gè)說(shuō)明:
The?Timeout?applies to the entire request and response, not individually to the?GetRequestStream?and?GetResponse?method calls.?然而具體的還沒(méi)搞清楚,還需要去驗(yàn)證。
后記
其實(shí),設(shè)置這兩個(gè)屬性你可能只需要找到文檔,分分鐘就可以搞定,但是我為什么會(huì)在這個(gè)過(guò)程遇到這些問(wèn)題呢?一方面是對(duì)于C# 中網(wǎng)絡(luò)請(qǐng)求的不熟悉,其次是找個(gè)時(shí)間,把以前的代碼需要重構(gòu)一下了,比如把HttpWebRequest?換成?HttpClient?等等。也讓我加深了對(duì)Http協(xié)議的理解。
原文地址:https://www.cnblogs.com/xiyin/p/10548319.html
.NET社區(qū)新聞,深度好文,歡迎訪問(wèn)公眾號(hào)文章匯總 http://www.csharpkit.com
總結(jié)
以上是生活随笔為你收集整理的.net每隔几秒去请求接口 怎么做_C# .net 中 Timeout 的处理及遇到的问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: butterknife 插件_知道这个插
- 下一篇: c# 获取excel单元格公式结果_每日