Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Google站点数据获取_C#_Webclient_Credentials_Google Sites - Fatal编程技术网

C# Google站点数据获取

C# Google站点数据获取,c#,webclient,credentials,google-sites,C#,Webclient,Credentials,Google Sites,万岁! 我想从非公共谷歌网站的页面获取图像。 我使用WebClient就是为了这个目的 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.Delet

万岁! 我想从非公共谷歌网站的页面获取图像。 我使用WebClient就是为了这个目的

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);
}
它不下载图像,但下载带有登录表单的html文件。 如果我在浏览器中打开此链接,它会显示我的登录表单,然后我输入用户名和密码,然后我可以看到图像

如何使用我的凭据通过WebClient或HttpWebRequest下载文件?

使用CookieContainer,将其附加到您的请求中。然后首先使用您的凭据向登录表单发送HTTP POST,然后执行HTTP GET以下载文件