C#如何用HttpWebRequest获取真实的下载地址



http://www.jubao163.com/it/bianchengwendang/2007-06-15/9299.shtml

怎么样能用C#获取真实的下载地址恩,累似与flashGet的下载功能?
现在好多的下载地址都是转向地址,如: http://www.xxx.com/down.asp?id=8848
转向到
http://www.xxx.com/downloadfile/love.zip

我怎么在程序中中能获取到真实的(http://www.xxx.com/downloadfile/love.zip)的下载地址?

---------------------------------------------------------------

分析http头信息。
下面是从crsky.com上下载软件的例子
你看到的软件地址是http://www.crsky.com/soft/Download.asp?ID=5758 

点击这个网址,下面是发送的http头信息情况。


GET /Download.asp?ID=5758 HTTP/1.1 
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://www.crsky.com/soft/3159.html
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
Host: www.crsky.com
Connection: Keep-Alive
Cookie: rtime=16; ltime=1136965348234; w08_eid=19321586-; ASPSESSIONIDCSDDQDCB=DMPMDCGDFMNCOLGJPGHFAEML; w0802=0


下面是返回的http头信息情况,在此服务器返回302 Object moved,并且在location中告诉了你真实地址,然后你到真实地址去下载就好了。

HTTP/1.1 302 Object moved
Server: Microsoft-IIS/5.0
Date: Wed, 11 Jan 2006 07:50:59 GMT
Location: http://xz1.crsky.com/efan/BitComet_0.61.rar   //真实地址
Content-Length: 164
Content-Type: text/html
Cache-control: private


具体代码实现:

private string GetAbsoluteURL(string url) 
	{ 
	HttpWebRequest Http=(HttpWebRequest)WebRequest.Create(url); 
	Http.AllowAutoRedirect=false; 
	WebResponse Resl=Http.GetResponse(); 
	string sLoca=""; 
	try{sLoca=Resl.Headers["Location"];} 
	catch(Exception){} 
	if(sLoca=="" || sLoca==null) return url; 
	else return GetAbsoluteURL(sLoca); 
}  


你可能感兴趣的:(C#如何用HttpWebRequest获取真实的下载地址)