Web客户端下载实际文件的HTML页面 [英] Web client download html page instred of actual file
本文介绍了Web客户端下载实际文件的HTML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class CookiesAwareWebClient : WebClient
{
public CookieContainer CookieContainer;
public CookieContainer MyProperty
{
get { return CookieContainer;}
set { CookieContainer = value;}
}
public CookiesAwareWebClient()
{
CookieContainer = new CookieContainer();
}
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
((HttpWebRequest)request).CookieContainer = CookieContainer;
return request;
}
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = @"http://www.codeproject.com/KB/IP/SimpleFTPDemo/SimpleFTP20_src.zip";
using (CookiesAwareWebClient client = new CookiesAwareWebClient())
{
NameValueCollection values = new NameValueCollection();
values.Add("Email", "abc@gmail.com");
values.Add("password", "abc123" );
values.Add("x", "10" ); // <- I doubt the server cares about the x position of where the user clicked on the image submit button :-)
values.Add("y", "10"); // <- I doubt the server cares about the y position of where the user clicked on the image submit button :-)
values.Add("login", "login");
// We authenticate first
client.UploadValues("https://www.codeproject.com/script/Membership/LogOn.aspx?rp=%2f", values);
// Now we can download
client.DownloadFile(textBox1.Text,"D:\1\23.zip")
}
推荐答案
问题是,当您在非浏览器上使用下载网址时,该网址不合适.
还在html输入密码"上拼写错误
让我发布一个完全灵活的代码:
The problem is the url for download is not appropriate when you use it on non-browser..
Also spelling mistake on html input "Password"
Let me post a fully flexible code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
namespace CodeProjectDownload
{
class Program
{
static void Main(string[] args)
{
Download("xyx@domain.com", "mypassword", @"C:\Test", @"http://www.codeproject.com/KB/IP/SimpleFTPDemo/SimpleFTP20_src.zip");
}
private static void Download(string email, string password, string destination, string downloadUri)
{
using (CookiesAwareWebClient client = new CookiesAwareWebClient())
{
NameValueCollection values = new NameValueCollection();
values.Add("Email",email );
values.Add("Password", password);
values.Add("x", "10"); // <- I doubt the server cares about the x position of where the user clicked on the image submit button
values.Add("y", "10"); // <- I doubt the server cares about the y position of where the user clicked on the image submit button
values.Add("login", "login");
// We authenticate first
client.UploadValues("https://www.codeproject.com/script/Membership/LogOn.aspx?rp=%2f", values);
// Now we can download
string modifieduri = "http://www.codeproject.com/script/articles/download.aspx?file=" + downloadUri.Replace("http://www.codeproject.com","") + "&rp=";
string filename = System.IO.Path.GetFileName(downloadUri);
string filepathname = System.IO.Path.Combine(destination, filename);
client.DownloadFile(modifieduri, filepathname);
}
}
}
}
这篇关于Web客户端下载实际文件的HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文