为什么.NET核心HTTPCLCLIENT或WebClient的时间比Python更长

发布于 2025-02-12 10:44:50 字数 1359 浏览 1 评论 0原文

我正在尝试从URL检索内容,我尝试了.NET Core的HTTPClient和WebClient,这两者都需要约10秒钟才能加载此特定网站。

但是,当我使用python的urllib。重新加载时,它会在同一秒内加载。我尝试了几乎所有不同的组合,包括:下载string,getStringAsync,getStreamAsync,getAsync,OpenRead等。

如果需要,我可以提供特定的URL。有可能的想法吗?

尝试#1

WebClient client = new WebClient();
Stream data = client.OpenRead("https://www.faa.gov/air_traffic/flight_info/aeronav/digital_products/dtpp/search/");
StreamReader reader = new StreamReader(data);
string s = await reader.ReadToEndAsync();
data.Close();
reader.Close();
return s;

尝试#2

using (var wc = new HttpClient())
        {
            var test = wc.GetAsync("https://www.faa.gov/air_traffic/flight_info/aeronav/digital_products/dtpp/search/").Result;
            var contents = test.Content.ReadAsStringAsync().Result;
            return contents;
        }

尝试#3

using (var wc = new HttpClient())
        {
            HTML = await wc.GetStringAsync("https://www.faa.gov/air_traffic/flight_info/aeronav/digital_products/dtpp/search/");
            return HTML;
        }

所有三个尝试都可以使用,每次都需要大约10秒。如果我在python中运行相同的东西,它将在同一秒内返回。

Python版本

with urllib.request.urlopen('https://www.faa.gov/air_traffic/flight_info/aeronav/digital_products/dtpp/search/') as response:
html = response.read()

I am trying to retrieve content from a URL, I have tried .NET Core's HttpClient and WebClient, both of which take ~10 seconds to load this specific website.

However, when I use Python's urllib.request it loads within the same second. I have tried pretty much all the different combinations including: DownloadString, GetStringAsync, GetStreamAsync, GetAsync, OpenRead, etc.

I can provide the specific URL if needed. Any possible ideas?

Attempt #1

WebClient client = new WebClient();
Stream data = client.OpenRead("https://www.faa.gov/air_traffic/flight_info/aeronav/digital_products/dtpp/search/");
StreamReader reader = new StreamReader(data);
string s = await reader.ReadToEndAsync();
data.Close();
reader.Close();
return s;

Attempt #2

using (var wc = new HttpClient())
        {
            var test = wc.GetAsync("https://www.faa.gov/air_traffic/flight_info/aeronav/digital_products/dtpp/search/").Result;
            var contents = test.Content.ReadAsStringAsync().Result;
            return contents;
        }

Attempt #3

using (var wc = new HttpClient())
        {
            HTML = await wc.GetStringAsync("https://www.faa.gov/air_traffic/flight_info/aeronav/digital_products/dtpp/search/");
            return HTML;
        }

All three attempts work, just take ~10 seconds everytime. If I run this the same sort of thing in Python it returns within the same second.

Python Version

with urllib.request.urlopen('https://www.faa.gov/air_traffic/flight_info/aeronav/digital_products/dtpp/search/') as response:
html = response.read()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文