Getting/Reading HTTP Responses
There has been a semi-frequent request on forums and such for people who want to do an HTTP request from their application (either web or client) and get the response as a string back so that they can process it from there. I had some code that did something similar, so I figured I'd modify it and post it here for all to use.
This will work in any application, either Windows Client, or as part of a Web Application (if you wanted to create a magic proxy site).
using System;
using System.IO;
using System.Net;
namespace HTTPReader
{
class HTTPReader
{
public static string GetHTML(string url, string proxy)
{
System.Net.WebRequest wr = System.Net.WebRequest.Create(url);
wr.Timeout = 2500; // 2.5 seconds
if (!String.IsNullOrEmpty(proxy))
{
System.Net.WebProxy myProxy = new System.Net.WebProxy(proxy, true);
wr.Proxy = myProxy;
}
using (System.Net.WebResponse wresp = wr.GetResponse())
{
Stream s = wresp.GetResponseStream();
using (StreamReader sr = new StreamReader(s))
{
return sr.ReadToEnd();
}
}
}
}
}
Edit1: You know, one thing I love about the community, you tell me when I'm wrong, or can do things better. This is slightly updated after poppyto provided a few suggestions! Thanks!
Edit2: Thanks to toub, I've added his suggested changes. It's fair to note, that I'm not catching any exceptions in here, and that is intentional. You could wrap this in a try catch block, but I'd rather you handle this how you want your app to deal with it.