从Web登录C#下载文件 [英] C# download file from the web with login
问题描述
我已经可以使用此代码登录带有重定向(我正在保存cookie)的网页
I can already login to the web page with redirect (i am saving cookies) with this code
CookieCollection cookies = new CookieCollection();
HttpWebRequest cookieRequest = (HttpWebRequest)WebRequest.Create("https://www.loginpage.com/");
cookieRequest.CookieContainer = new CookieContainer();
cookieRequest.CookieContainer.Add(cookies);
HttpWebResponse cookieResponse = (HttpWebResponse)cookieRequest.GetResponse();
cookies = cookieResponse.Cookies;
string postData = "name=********&password=*********&submit=submit";
HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create("https://www.loginpage.com/");
loginRequest.CookieContainer = new CookieContainer();
loginRequest.CookieContainer.Add(cookies);
loginRequest.Method = WebRequestMethods.Http.Post;
loginRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
loginRequest.AllowWriteStreamBuffering = true;
loginRequest.ProtocolVersion = HttpVersion.Version11;
loginRequest.AllowAutoRedirect = true;
loginRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
loginRequest.ContentLength = byteArray.Length;
Stream newStream = loginRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
newStream.Close();
这很好,但是我需要从那里下载.xls文件,它位于这里(for例子)
This works fine, but i need to download .xls file from there, it is located here (for example)
https://www.loginpage.com/export_excel.php?export_type=list
为此我试过这段代码
HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create("https://www.loginpage.com/export_excel.php?export_type=list");
HttpWebResponse ws = (HttpWebResponse)wr.GetResponse();
Stream str = ws.GetResponseStream();
byte[] inBuf = new byte[100000];
int bytesReadTotal = 0;
string path = @"d:\test.xlsx";
FileStream fstr = new FileStream(path, FileMode.Create, FileAccess.Write);
while (true)
{
int n = str.Read(inBuf, 0, 100000);
if ((n == 0) || (n == -1))
{
break;
}
fstr.Write(inBuf, 0, n);
bytesReadTotal += n;
}
str.Close();
fstr.Close();
但它不起作用,现在我被困在这个
but its not working and now i am stuck with this
string dLink = "https://www.loginpage.com/export_excel.php?export_type=list";
HttpWebRequest fileRequest = (HttpWebRequest)HttpWebRequest.Create(dLink);
fileRequest.CookieContainer = new CookieContainer();
fileRequest.CookieContainer.Add(cookies);
fileRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
HttpWebResponse fileResponse = (HttpWebResponse)fileRequest.GetResponse();
for (int i = 0; i < fileResponse.Headers.Count; ++i)
richTextBox1.Text += "\nHeader Name: " + fileResponse.Headers.Keys[i] + ", Value :" + fileResponse.Headers[i];
当然不会下载文件。我现在正试图获取标题,以便了解我从网络上获得的内容?我已经使用我的脚本从rghost或filehippo这样的网页下载了一些文件,但是这个文件没有用。
Of course it is not downloading the file. I am trying to get headers now to just understand what I am getting from the web? I have already downloaded some files with my script from fil esharing pages like rghost or filehippo, but this one is not working.
推荐答案
这应该可以胜任!
CookieContainer cookieJar = new CookieContainer();
CookieAwareWebClient http = new CookieAwareWebClient(cookieJar);
string postData = "name=********&password=*********&submit=submit";
string response = http.UploadString("https://www.loginpage.com/", postData);
// validate your login!
http.DownloadFile("https://www.loginpage.com/export_excel.php?export_type=list", "my_excel.xls");
我使用过 CookieAwareWebClient
public class CookieAwareWebClient : WebClient
{
public CookieContainer CookieContainer { get; set; }
public Uri Uri { get; set; }
public CookieAwareWebClient()
: this(new CookieContainer())
{
}
public CookieAwareWebClient(CookieContainer cookies)
{
this.CookieContainer = cookies;
}
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = this.CookieContainer;
}
HttpWebRequest httpRequest = (HttpWebRequest)request;
httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
return httpRequest;
}
protected override WebResponse GetWebResponse(WebRequest request)
{
WebResponse response = base.GetWebResponse(request);
String setCookieHeader = response.Headers[HttpResponseHeader.SetCookie];
if (setCookieHeader != null)
{
//do something if needed to parse out the cookie.
if (setCookieHeader != null)
{
Cookie cookie = new Cookie(); //create cookie
this.CookieContainer.Add(cookie);
}
}
return response;
}
}
Source&信用: CookieAwareWebClient
这篇关于从Web登录C#下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!