gpt4 book ai didi

c# - 是否有与 Perl 的 LWP::UserAgent 等效的 C#?

转载 作者:行者123 更新时间:2023-11-30 13:34:48 25 4
gpt4 key购买 nike

在我参与的一个项目中,有一个需求是一定的价格将从某些 Web 界面查询股票并以某种方式显示。

我知道需求的“查询”部分可以使用 LWP::UserAgent 等 Perl 模块轻松实现。但出于某种原因,C# 已被选为实现显示部分的语言。我不想将任何 IPC(如套接字,或通过数据库间接添加)添加到这个小项目中,所以我的问题是是否存在与 Perl 的 LWP::UserAgent 等效的 C#?

最佳答案

您可以使用 System.Net.HttpWebRequest 对象。

看起来像这样:

// Setup the HTTP request.
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.google.com");

// This is optional, I'm just demoing this because of the comments receaved.
httpWebRequest.UserAgent = "My Web Crawler";

// Send the HTTP request and get the response.
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

if (httpWebResponse.StatusCode == HttpStatusCode.OK)
{
// Get the HTML from the httpWebResponse...
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string html = reader.ReadToEnd();
}

关于c# - 是否有与 Perl 的 LWP::UserAgent 等效的 C#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1926552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com