2011-12-12 69 views
1

我想使用webbrowser class.i下载文件,我可以使用webbrowser进行登录和浏览。自动登录和文件下载

但该文件位于使用JavaScript的网站上。

我试图使用网页浏览器的cookies使用HttpWebRequest(fileurl)获取文件。

登录后下载文件的代码如下。

string myUri = @"url_of_file"; 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUri); 
request.CookieContainer = new CookieContainer(); 
foreach (string cookie in webBrowser1.Document.Cookie.Split(';')) 
{ 
    string name = cookie.Split('=')[0]; 
    string value = cookie.Substring(name.Length + 1); 
    string path1 = "/"; 
    string domain = ".abcde.com"; //change to your domain name 

    request.CookieContainer.Add(new Cookie(name.Trim(), value.Trim(), path1, domain)); 
} 

WebResponse res = request.GetResponse();  

StreamReader sReader = new StreamReader(res.GetResponseStream()); 
StreamWriter sWriter = new StreamWriter(@"D:\file.csv"); 

while (sReader.Peek() >= 0) 
sWriter.WriteLine(sReader.ReadLine()); 

sWriter.Close(); 

但是,响应流没有文件,而是在html body.as中有“url_of_file”。

<html><head></head> 
<body onload="this.location.href='/Marketinfo/SubViewSubscriptionFile?product_code=DOL_INT&amp;file_id=460916&amp;subscription_id=318342&amp;loadnow=true'"> 
</body></html>   

请人所说的其实是错在这

回答

0

因此,下一步是使用现有的cookie加上将所有的HTTP头的服务器预计欺骗服务器。
如果您想在请求和响应中查看背景的完整视图,请打开Chrome浏览器,按Ctrl + Shift + I打开开发人员工具,选择网络选项卡,然后从一个用户开始重新开始此过程。通过点击名称列上的文件,您将获得请求和响应的所有相关信息。好处是你可以拥有任何需要完全欺骗服务器而无需使用Web浏览器控件。

+0

谢谢,我可以下载文件。因为响应流有oonload.true.i只是加载&loadnow = true来回url.It工作。 – Radha