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

标签 c# perl

在我参与的一个项目中,有一个需求是一定的价格 将从某些 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/

相关文章:

windows - 如何让分支管道在 Windows 上的 Perl 中工作?

perl - 如何在 Perl 中同时捕获 stderr、stdout 和退出代码?

Perl 意想不到的结果

c# - 通用类型代码中的类型参数是否有命名约定(bracy flavoured)

c# - 在具有多个 Where 语句的循环中过滤 IQueryable

C# - 二进制数据可以编译成模块吗?

windows - 我怎样才能使批处理文件像使用 Perl 的简单 grep 一样运行?

html - 如何使用 Perl 去除字符串中的 HTML?

c# - IronRuby 作为 .NET 中的脚本语言

c# - MonoMac NSWorkspace.RecycleUrls 与 recycleURLs :completionHandler