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.
Comments
- Anonymous
March 02, 2006
You should use String.IsNullOrEquals for the proxy string comparaison. Also, it's better to specify a Timeout for the request. - Anonymous
March 02, 2006
You should also wrap the WebResponse and the StreamReader with using clauses so that their Dispose methods are called before the method exits. The StreamReader will in turn close the underlying response stream. Failure to close the stream or the response can cause your application to run out of connections. - Anonymous
March 02, 2006
Why not just DownloadString with WebClient?
http://msdn2.microsoft.com/en-us/library/system.net.webclient.downloadstring.aspx
construct the WebClient, set the Proxy property if desired, then DownloadString - seems simpler and more the Right Tool For The Job, unless I'm missing somethinghere. - Anonymous
March 03, 2006
PeteL demonstrated in his two recent blog entries something that i find myself doing far too often for... - Anonymous
March 05, 2006
These is some lines I got Http Response in my application, encode with UTF-8 to StringBuilder.
HttpWebRequest webrequest = null;
HttpWebResponse webresponse = null;
StringBuilder sbuild = new StringBuilder();
string temp = "";
try
{
webrequest = (HttpWebRequest)System.Net.WebRequest.Create(strChannel);
webresponse = (HttpWebResponse)webrequest.GetResponse();
StreamReader webstream = new StreamReader(webresponse.GetResponseStream(), Encoding.UTF8);
while ((temp = webstream.ReadLine()) != null)
{
sbuild.Append(temp);
}
webstream.Close();
MessageBox.Show(sbuild.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
webresponse.Close();
}