更新时间:2023-02-23 20:59:32
如果这确实是CodeProject中的文件,则很可能您在尝试下载文件之前忘记了身份验证(登录).我建议您改用System.Net.HttpWebRequest
类:
http://msdn.microsoft.com/en-us/library/system.net. httpwebrequest.aspx [ ^ ].
以下是几个问题,并附有解释方法的答案:
http://***.com/questions/9841344/c-sharp-https-登录并下载文件 [ ^ ],
http://***.com/questions/4699938/如何使用httpwebrequest-and-httpwebresponse-classcookies下载文件 [
If this is really a file from CodeProject, most likely you forgot to authenticate (login) before trying to download a file. I would advise that you use the classSystem.Net.HttpWebRequest
instead:
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx[^].
Here are a couple of question with answers explaining how to do it:
http://***.com/questions/9841344/c-sharp-https-login-and-download-file[^],
http://***.com/questions/4699938/how-to-download-the-file-using-httpwebrequest-and-httpwebresponse-classcookies[^].—SA
private void button1_Click(object sender, EventArgs e)
{
string url = downloadURL.Text;
WebBrowser bweb = new WebBrowser();
bweb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(bweb_DocumentCompleted);
bweb.Navigate(new Uri(url));
}
void bweb_DocumentCompleted(object sender, webBrowserDocumentCompletedEventArgs e)
{
string url = downloadURL.Text;
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("ravi", "ravi");
client.BaseAddress = "http://www.codeproject.com";
string FileName = url.Substring(url.LastIndexOf("/") + 1,
(url.Length - url.LastIndexOf("/") - 1));
client.DownloadFile(url, savePath.Text + FileName);
}