So, one of my little side projects involves getting images from websites. Surprisingly, one of the easiest parts is actually requesting and receiving the Image. (note: exception handling code removed for your viewing pleasure.)
private
Image GetImage(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Image image = Image.FromStream(resp.GetResponseStream());
resp.Close();
return image;
}
Now, you can just as easily get the source of a web-page:
private
string GetText(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string text = sr.ReadToEnd();
resp.Close();
return text;
}
Brilliant! Of course, getting exceptionally large images (or files of any kind) in this manner should be done asynchronously by utilizing BeginGetResponse and EndGetResponse instead of just GetResponse. You could also use a Thread to make these kinds of operations asynchronous.