在.NET中调用HttpHandler文件的最简单方法是什么?

时间:2008-10-14 15:49:01

标签: .net asp.net http

我的网络服务器上有一个HttpHandler,它采用“https://servername/myhandler?op=get&k=Internal&m=jdahug1”形式的URL。我需要从我的.NET应用程序中调用此URL并捕获输出内容。有谁知道我怎么做到这一点?我希望它很简单,以便我只返回带有输出的字符串,并且我可以指定自己的超时。

  • 谢谢!

4 个答案:

答案 0 :(得分:6)

尝试使用System.Net.WebClient类。

您可以使用.DownloadStringAsync()方法强制执行超时。

答案 1 :(得分:3)

Shawn Wildermuth概述了您拥有的两个选项:WebClient和WebRequest(http://wildermuth.com/2008/09/27/WebClient_vs_WebRequest_in_Silverlight_2)。 WebClient只是一个更高级别的抽象,可以为您处理更多细节。由于您只是希望获得一个字符串,我将使用WebClient,正如Shawn所描述的那样,它有一个DownloadString方法,等待您使用。

答案 2 :(得分:2)

正如乔尔所说,WebClient会做的伎俩..

string handlerResponse = new System.Net.WebClient().DownloadString("https://servername/myhandler?op=get&k=Internal&m=jdahug1");

当然,鉴于你自己的超时和良好做法,你可能不想内联电话,但你明白了。

答案 3 :(得分:1)

我们在产品的后端使用了以下内容(这只是核心代码,而不是超时错误处理等)。

using System.Net;

using System.IO;

HttpWebRequest req = (HttpWebRequest) WebRequest.Create(WebPageUrl);

WebResponse resp = req.GetResponse();

Stream stream = resp.GetResponseStream();

StreamReader reader = new StreamReader(stream);

output.Write(reader.ReadToEnd());