谷歌网站数据获取
冰雹! 我想从非公开的 Google 网站页面获取图像。 我正在使用 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 下载文件?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
CookieContainer
,将其附加到您的请求中。然后首先使用您的凭据对登录表单执行 HTTP POST,然后执行 HTTP GET 以下载文件。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.