Sunday, June 3, 2007

Grabbing secure data from the web.

Recently, I was working on a little image recognition for some anti-bot type image. No no, I wasn't working on any spamming type of application, just a little side project of mine.

Regardless, I needed to load the image from a website while being logged into the website. I did a bit of research and learned that there's no real way, or at least no easy way, to pull an image from a WebBrowser control. So I went about Load'ing it with a PictureBox until I found out that the manner in which the image is loaded doesn't retain the session cookie in the WebBrowser. Thus I ran into a rather large dilemma, I needed to load the image while remaining logged in and the PictureBox was definitely not the correct solution nor was trying to pull it out of the WebBrowser control.

I did quite a bit more research and came across the HttpWebRequest and the HttpWebResponse classes. I knew that if I could some how send out the correct cookie data from my WebBrowser control with my request for the image, I'd get what I wanted. So I quick parsed the cookie credentials from my WebBrowser control (which was a bit of a pain because they each have their different forms of storing the cookie information), added it to my request, and got my image perfectly.

HttpWebRequest h = (HttpWebRequest)WebRequest.Create(URL);
h.CookieContainer = new CookieContainer();

foreach (string Cooky in webBrowser.Document.Cookie.Split(';'))
    h.CookieContainer.Add(new Cookie(Cooky.Split('=')[0].Replace(" ", ""), Cooky.Split('=')[1].Replace(" ", ""), DOMAIN, URL));

HttpWebResponse response = (HttpWebResponse)h.GetResponse();
Stream simg = response.GetResponseStream();
Image bimg = new Bitmap(simg);

I posted this code at the CodeProject where a few people were looking to do the same thing.

No comments: