使用C#从网站下载文件 [英] Downloading a file from a website using C#

查看:58
本文介绍了使用C#从网站下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码从网站下载文件:

I am trying to download a file from a website using the following code:

WebClient webClient = new WebClient();
webClient.DownloadFile("http://www.nseindia.com/content/historical/EQUITIES/2011/MAR/cm07MAR2011bhav.csv.zip", @"c:\myfile.txt");

显示的异常是禁止错误403"

The exception shown is "forbidden error 403"

这意味着找不到页面,但是我可以使用Java代码下载该文件,也可以直接从该网站下载该文件.

It means page not found but I can download that file using java code and also I can directly download it from that website.

如何使用C#代码下载此代码?

How do I download this using C# code?

推荐答案

我使用wget测试了该URL,并收到403错误.我可以通过在标题中添加用户代理字符串来解决该问题

I tested that URL with wget, and got a 403 error. I was able to solve that problem by adding a user-agent string to the header

尝试使用 webClient.Headers.Add(HttpRequestHeader.UserAgent,"blah")

这篇关于使用C#从网站下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆