Skip to content

Commit 9085bf7

Browse files
author
邹嵩
committed
修复HttpClient的Timeout设置单位出错, 导致卡死的问题
1 parent 5568a5a commit 9085bf7

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/DotnetSpider.Core.Test/Downloader/HttpClientDownloaderTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,14 @@ public void GetTargetUrlWhenRedirect()
135135
Assert.DoesNotContain("1231222221111123", page.TargetUrl);
136136
Assert.True(page.TargetUrl.Contains("www.jd.com/") || page.TargetUrl.Contains("global.jd.com"));
137137
}
138+
139+
[Fact(DisplayName = "SetTimeout")]
140+
public void SetTimeout()
141+
{
142+
HttpClientDownloader downloader = new HttpClientDownloader();
143+
var entry = HttpClientDownloader.HttpClientPool.GetHttpClient("a");
144+
downloader.PrepareHttpClient(entry);
145+
Assert.Equal(8, entry.Client.Timeout.TotalSeconds);
146+
}
138147
}
139148
}

src/DotnetSpider.Core/Downloader/HttpClientDownloader.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ protected virtual string ReadContent(Site site, HttpResponseMessage response)
197197
}
198198
}
199199

200+
internal void PrepareHttpClient(HttpClientEntry httpClientEntry)
201+
{
202+
httpClientEntry.Init(AllowAutoRedirect, () =>
203+
{
204+
if (!Equals(httpClientEntry.Client.Timeout.TotalSeconds, _timeout))
205+
{
206+
httpClientEntry.Client.Timeout = new TimeSpan(0, 0, (int)_timeout / 1000);
207+
}
208+
}, CopyCookieContainer);
209+
}
210+
200211
private Page HandleResponse(Request request, HttpResponseMessage response, Site site)
201212
{
202213
string content = ReadContent(site, response);
@@ -223,17 +234,6 @@ private Page HandleResponse(Request request, HttpResponseMessage response, Site
223234
return page;
224235
}
225236

226-
private void PrepareHttpClient(HttpClientEntry httpClientEntry)
227-
{
228-
httpClientEntry.Init(AllowAutoRedirect, () =>
229-
{
230-
if (!Equals(httpClientEntry.Client.Timeout.TotalSeconds, _timeout))
231-
{
232-
httpClientEntry.Client.Timeout = new TimeSpan(0, 0, (int)_timeout);
233-
}
234-
}, CopyCookieContainer);
235-
}
236-
237237
private CookieContainer CopyCookieContainer()
238238
{
239239
using (MemoryStream stream = new MemoryStream())

0 commit comments

Comments
 (0)