tags:

views:

852

answers:

4

I have an HttpHandler on my webserver that takes a URL in the form of "https://servername/myhandler?op=get&k=Internal&m=jdahug1". I need to call this URL from my .NET app and capture whatever the output is. Does anyone know how I can do that? I want it to be simple so that I just get back a string with the output, and that I can specify my own timeout.

  • Thanks!
+6  A: 

Try the System.Net.WebClient class.

You can use the .DownloadStringAsync() method to enforce a timeout.

Joel Coehoorn
This class doesn't have the ability to set a timeout like I wanted (as far as I can see) but I agree it is the simplest way to make the call.
skb
+1  A: 

we have used the following in the backend of our product (this is just the core code, not with timeout errorhandling etc.)

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());

Andrew Cox
+2  A: 

As Joel had said WebClient would do the trick..

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

of course given your own timeout and good practices you probably don't want to inline the call, but you get the idea.

Quintin Robinson
+2  A: 

Shawn Wildermuth gives a great overview of the two options you have: WebClient and WebRequest (http://wildermuth.com/2008/09/27/WebClient_vs_WebRequest_in_Silverlight_2). WebClient is just a higher level abstraction that handles more of the details for you. Since you are just looking to get a string back I would look to use the WebClient, which as Shawn describes, has a DownloadString method just waiting for you to use.

William Rohrbach