通过http登录并下载xml文件?

时间:2012-03-30 16:23:35

标签: c# http authentication httpwebrequest web-scraping

需要从以下网址登录并下载xml文件:

http://www.radionyhetene.net/download/nyheter/mp3/nyheter.xml

我已经知道如何使用WebClient获取和发布数据,HttpWebRequest。但是对于上面的网址,我完全不知道该怎么做。

我尝试使用Live Http Header为上面的Url。它显示了这样的事情:

http://www.radionyhetene.net/download/nyheter/mp3/nyheter.xml
GET /download/nyheter/mp3/nyheter.xml HTTP/1.1
Host: www.radionyhetene.net
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115                Firefox/3.6
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Authorization: Basic aGF1Z2FsYW5kOjlmNm5kOGtmMw==

HTTP/1.1 200 OK
Date: Fri, 30 Mar 2012 16:09:35 GMT
Server: Apache/2.2.15 (Win32) PHP/5.3.2
Last-Modified: Fri, 30 Mar 2012 14:54:13 GMT
Etag: "4730000000053f1-194-4bc77038957a2"
Accept-Ranges: bytes
Content-Length: 404
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/xml

所以问题是如何将登录信息传递给此网址?请提供您的建议。 感谢。

1 个答案:

答案 0 :(得分:3)

易,

WebClient web = new WebClient();
web.Credentials = new NetworkCredential("h.....", "9.......");
string s = web.DownloadString("http://www.radionyhetene.net/download/nyheter/mp3/nyheter.xml");