Download Image from Website w/ Cookies

Discussion in 'Programming & Scripting' started by dschu012, Feb 18, 2009 with 6 replies and 267 views.

  1. dschu012

    dschu012 Member

    Messages:
    760
    Ratings:
    381
    I am trying to download a image from a website that requires you to be signed into the site to get the correct image. I am also using a webBrowser control that is already logged onto the site (however I don't want to navigate to the page to download the image). Currently this is what I am doing.
    Code:
    WebRequest requestPic = WebRequest.Create(url);
    requestPic.Headers[HttpRequestHeader.Cookie] = webBrowser1.Document.Cookie;
    WebResponse responsePic = requestPic.GetResponse();
    Image responseImage = Image.FromStream(responsePic.GetResponseStream());
    responsePic.Close();
    
    However this doesn't seem to work. I am guessing I am doing something wrong with the cookie part. Does anyone know how to do this correctly?

     
  2. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    I know this isn't exactly the method you might be looking for, but with some modification where need be, you should be able to download the image.
    Anyways, you'll need a webbrowser control (that can remain out of site if you so choose)

    I used youtube as an example. You're gonna need to look through the html on the site you're talking about, to find the elements relevant to logging in (username textbox, password textbox, and the submit button), because obviously different sites set their logins up different ways.

    Code:
    WebBrowser1.Navigate("http://www.youtube.com/login?next=/");
    while ((WebBrowser1.ReadyState != WebBrowserReadyState.Complete)) {
    Application.DoEvents();
    }
    WebBrowser1.Document.GetElementById("username").InnerText = "username";
    WebBrowser1.Document.GetElementById("password").InnerText = "password";
    WebBrowser1.Document.GetElementById("loginForm").InvokeMember("submit");
    WebBrowser1.Navigate("http://s.ytimg.com/yt/img/youtube_logo_small_grey-vfl41206.png");
    while ((WebBrowser1.ReadyState != WebBrowserReadyState.Complete)) {
    Application.DoEvents();
    }
    WebBrowser1.ShowSaveAsDialog();
    
    What this does is navigates to [Click here to view the link] (the login page), waits til the page is fully loaded, then sends the text to the textboxes on the page and then submits the information causing you to login. Then navigates to the url of this image
    [​IMG]
    waits for the page to fully load, then prompts the user to save the image.

    Hope this helps. :smile:

    edit: Use a try.. catch statement around the actual login, in case the application has the login information saved in a cookie, that way if it doesn't work it can just pass over those lines.
     
  3. OP
    dschu012

    dschu012 Member

    Messages:
    760
    Ratings:
    381
    Not exactly what I needed. I already have logged into the site using the same method you did above. Now I need the WebBrowser to stay on the same site it is on but use a WebRequest using the same cookies from the WebBrowser to download an image from another website. It kinda confusing to explain.... :-/
     
  4. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    Why not just use a second webbrowser control?
     
  5. OP
    dschu012

    dschu012 Member

    Messages:
    760
    Ratings:
    381
    That is a good suggestion. However my IE doesn't seem to work correctly so that won't work :-/. I can go to the page on Firefox and it shows the image. If I try to navigate to it on IE or the WebBrowser control I get "Cannot Display XML page." When the link is a direct link to the image, nothing else on the page.

    When I use the WebRequest I atleast get an image that displays "Error" which is the default image if you are not logged in to the website.
     
  6. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    To be honest you can completely avoid using the webrequest class by just using my method in a separate webbrowser to get to the image. Also if the image is a .xml file somehow then that could be your problem. It needs to be the link to an actual image (bmp, jpg, png, gif, tif, etc) Not trying to insult your intelligence here, but I'm just saying, if it's giving you an error referencing an xml page, that was just my first guess at the problem.
     
  7. OP
    dschu012

    dschu012 Member

    Messages:
    760
    Ratings:
    381
    Ya I know. The XML error is only on IE not Firefox or anything else. My IE is all f**ked up it probably would work on another computer correctly and not mine. Yes it is a JPG it isn't an XML file.