1

I have a WebClient object and am using the DownloadFileAsync method to get a file, although before I can I'm needing to log into the site, which seems to be a difficult. I'm wanting to log into https://ssl.rapidshare.com/ (which appears to be using Forms authentication). I've found the following WebClient code that is cookie aware:

public class CookieAwareWebClient:WebClient {
    private CookieContainer m_container=new CookieContainer();
    protected override WebRequest GetWebRequest(Uri address) {
        WebRequest request=base.GetWebRequest(address);
        if(request is HttpWebRequest) {
            (request as HttpWebRequest).CookieContainer=m_container;
        }
        return request;
    }
}

However, I'm afraid my code doesn't appear to be co-operating:

gWebClient=new CookieAwareWebClient();
if(gGroupEntry.GetStringProperty(CGroupEntry.EStringProperties.DownloadURL).Contains("rapidshare.com"))
    gWebClient.Credentials=new System.Net.NetworkCredential(CSaverLoader.gUsername, CSaverLoader.gPassword, "https://ssl.rapidshare.com/");
gWebClient.DownloadFileCompleted+=new AsyncCompletedEventHandler(gWebClient_DownloadFileCompleted);
gWebClient.DownloadProgressChanged+=new DownloadProgressChangedEventHandler(gWebClient_DownloadProgressChanged);
gWebClient.DownloadFileAsync(new Uri(gGroupEntry.GetStringProperty(CGroupEntry.EStringProperties.DownloadURL)), gDownloadDirectory+"/"+gGroupEntry.GetStringProperty(CGroupEntry.EStringProperties.FileName));

Why could this be?

R4D4
  • 1,382
  • 2
  • 13
  • 34

2 Answers2

1

Are you able to use the HttpWebRequest Class in the System.Net namespace? This is a much more flexible class than the light-weight WebClient for making HTTP requests and includes stuff like HttpWebRequest.CookieContainer Property.

Dan Diplo
  • 25,076
  • 4
  • 67
  • 89
  • Hi Dan, yes I suppose I could although as far as I'm aware the code above makes the WebClient cookie aware, so I'm not sure why that couldn't be used? – R4D4 Jun 15 '11 at 17:19
  • @R4D4 Because it doesn't co-operate with your code, maybe? :) – Dan Diplo Jun 15 '11 at 17:53
  • Hahaha, well I suppose that could be one reason why! :) Although there seems to be a few answers relating to this that all say that overloading the WebClient is possible (for example http://stackoverflow.com/questions/1777221/c-using-cookiecontainer-with-webclient-class). If I switched to HttpWebRequest I think I'd still be at a loss exactly how to handle the cookies... Thanks. – R4D4 Jun 15 '11 at 18:00
  • Yeah, HttpWebRequest will do then I think... :) – R4D4 Jun 15 '11 at 19:11
0

If the website uses cookie-based authentication rather than HTTP basic authentication, you need to put a cookie into the CookieContainer, instead of setting the Credentials.

dtb
  • 213,145
  • 36
  • 401
  • 431
  • Thanks very much for the comment; although I can't say I'm too sure how to even get a cookie using WebClient? – R4D4 Jun 15 '11 at 16:20