views:

43

answers:

1

Hail! I want to fetch image from NOT PUBLIC Google Site's page. I'm using WebClient for this purposes.

var uri =
    new Uri("http://sites.google.com/a/MYDOMAIN.COM/SITENAME/" +
            "_/rsrc/1234567890/MYIMAGE.jpg");
string fileName = "d:\\!temp\\MYIMAGE.jpg";
if (File.Exists(fileName))
    File.Delete(fileName);
using (var webClient = new WebClient())
{
    var networkCredential = new NetworkCredential("USERNAME", "PASSWORD");
    var credentialCache = new CredentialCache
    {
        {new Uri("sites.google.com"), "Basic", networkCredential},
        {new Uri("www.google.com"), "Basic", networkCredential}
    };
    webClient.Credentials = credentialCache;
    webClient.DownloadFile(uri, fileName);
}

It doesn't download image, but html file with login form is downloaded. If i open this link in browser it shows me login form then i enter username and password and then i can see the image.

How i must use my credentials to download file with WebClient or HttpWebRequest?

A: 

Use a CookieContainer, attach it to your request. Then first do a HTTP POST to the login form, with your credentials, and then do a HTTP GET to download the file.

Jan Jongboom