zl程序教程

您现在的位置是:首页 >  后端

当前栏目

获取软件下载的真实地址!再谈获取Response.redirect重定向的URL

地址软件下载重定向 获取 url 真实 response
2023-06-13 09:13:41 时间

http://www.im286.com/viewthread.php?tid=1550010&extra=page%3D1

其实这个问题落伍谈了n次了
其中care4也说了两次所以如果你有问题最好先搜索一下说不定问题早有人解决了
http://www.im286.com/viewthread....;highlight=%2Bcare4
http://www.im286.com/viewthread....;highlight=%2Bcare4
care4的代码有一个小缺点就是需要组件。
第一个是.net组件二不是但用组件始终不太方便有没有asp直接获取的方式呢

答案是有的
我写的一个vb简单的winsock获取的代码
http://www.im286.com/viewthread....t=Response.redirect


当时我说用asp好像不能获得,那是当时没有去csdn混去,现在搞定了

首先我们要了解为什么xmlhttp组件无法获得这样的跳转真实地址
用Response.Redirect跳转,PHP里面是Header("Location",$URL);
这两种方式都是一样的原理,就是在输出的HTTP头里面加上一个Location字段
同时把返回的HTTP状态值设为302,浏览器就会认为当前请求的页面已经
被移动到Location指定的路径
那么为什么xmlhttp无法获得呢?
原因很简单
XMLHTTP组件在处理包含Location头的302消息时太智能了,直接给跳转到最后的页面,也就是说~我们看不到中间的过程!比尔自作聪明阿!

不过还好MSXML4里面提供了一个可用的新的组件:WinHttp.WinHttpRequest.5.1,这个也是MSXML4XMLHTTP组件的核心。WinHttp.WinHttpRequest有一个十分关键的属性:Option,这个属性的第六个索引就是指示是否自动跳转,然后就可以轻松的使用XMLHTTP组件的getResponseHeader和getAllResponseHeaders方法来获取返回的HTTP头信息了。

好接下来就看代码了
DimoHttp
SetoHttp=Server.CreateObject("WinHttp.WinHttpRequest.5.1"
oHttp.Option(6)=0      "禁止自动Redirect,最关键的  剩下的就简单读取数据都估计大家都会
oHttp.SetTimeouts5000,5000,30000,5000  "设置超时~和ServerXMLHTTP组件一样
oHttp.Open"GET",sUrl,False    "以同步模式打开URL
IfoHttp.Status<>200AndoHttp.Status<>302Then
"oHttp.Status对应返回的HTTP状态,如果是200,表示这个就是最终页面,没有Location跳转
"如果是302,表示当前请求的URL已经被移动,需要根据HTTP头来跳转
"对于其他数值的状态,基本上我们不要处理,但是你要处理也可以比如440或者别的状态你自己处理就可以了!
Else
  "在这里对返回的HTTP头和文档内容进行处理
EndIf

好了比较完整的代码比较长
我传了个到空间上自己看去
http://test.aymtv.com/url.asp
默认的输入栏里的代码是crsky的一个下载地址你可以测试一下就知道了
点查看源代码就可以看见这个asp文件的源代码!

一切搞定over继续去csdn混分去了

完整代码:
复制代码代码如下:

<%
PublicFunctionBytes2BSTR(v)
	Dimr,i,t,n:r=""
	Fori=1ToLenB(v)
		t=AscB(MidB(v,i,1))
		Ift<&H80Then
			r=r&Chr(t)
		Else
			n=AscB(MidB(v,i+1,1))
			r=r&Chr(CLng(t)*&H100+CInt(n))
			i=i+1
		EndIf
	Next
	Bytes2BSTR=r
EndFunction
"==========================================================================================
	IfRequest.QueryString="ViewSource"Then
		DimoFso:SetoFso=Server.CreateObject("Scripting.FileSystemObject")
		DimoFil:SetoFil=oFso.OpenTextFile(Server.MapPath("URL.Asp"))
		DimsTxt:sTxt=oFil.ReadAll()
		oFil.Close:SetoFil=Nothing:SetoFso=Nothing
		Response.ContentType="text/plain"
		Response.WritesTxt
		Response.ENd
	EndIf
%><?xmlversion="1.0"encoding="gb2312"standalone="yes"?>
<!doctypehtmlpublic"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns:v="http://www.eglic.com/">
	<head>
		<title></title>
		<metaname="Generator"content="EditPlus"/>
		<metaname="Author"content="eglic"/>
		<metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
		<metaname="CharSet"content="GB2312"/>
		<linkrel="stylesheet"type="text/css"href="/styles/default.css"/>
		<styletype="text/css">
			@mediaall{
				
			}
		</style>
		<scriptlanguage="javascript"src="/scripts/default.js"></script>
		<scriptlanguage="javascript"src="/scripts/xml.js"></script>
		<scriptlanguage="javascript">//<!--
			
		//--></script>
	</head>
	<body>
		<formaction=""method="POST">
			要检测的URL:<inputtype="text"name="URL"size="50"value="<%
				IfRequest.Form("URL")<>""THen
					Response.WriteTrim(Request.Form("URL"))
				Else
					Response.Write"http://www.crsky.com/view_down.asp?downd_id=8&downd=0&ID=20780&down=yes"
				EndIf
			%>"/>
			<inputtype="submit"value="提交"/>
			<inputtype="button"value="查看源代码"onclick="JavaScript:window.open("<%=URLSelf%>?ViewSource");"/>
		</form>
		<%
			PublicFunctionGetAbsoluteURL(sUrl,ByRefiStep)
				DimbUrl,bDat
				IfiStep>15Then
					Err.RaisevbObejctError,"递归错误","递归嵌套超过15层可能会引起程序崩溃"
				EndIf
				IfInStr(sUrl,"://")<=0ThensUrl="http://"&sUrl
				IfInStr(sUrl,"?")>0THen
					DimtmpUrl:tmpUrl=split(sUrl,"?")
					bUrl=tmpUrl(0)
					bDat=tmpUrl(1)
				Else
					bUrl=sUrl
					bDat=""
				EndIf
				Response.Write"<pstyle=""border:solid1pxsilver;border-top:solid2pxred;padding:5px;margin:2px;"">"
				Response.Write"第"&iStep&"步:"
				Response.Write"正在准备获取"&bUrl&"<br/>"
				iStep=iStep+1
				ifbDat<>""ThenResponse.Write"  >>参数:"&bDat&"<br/>"
				DimoHttp:SetoHttp=Server.CreateObject("WinHttp.WinHttpRequest.5.1")
				oHttp.Option(6)=0	"禁止自动Redirect,最关键的
				"oHttp.Option()
				oHttp.SetTimeouts5000,5000,30000,5000
				oHttp.Open"GET",sUrl,False
				OnErrorResumeNext
					oHttp.SendbDat
					IfErr.Number<>0Then
						Response.Write"<fontcolor=""red"">发生错误:"&Err.Description&"</font><br/>"
						Err.Clear
						GetAbsoluteURL=""
						SetoHttp=Nothing
						Response.Write"</p>"
						ExitFunction
					EndIf
				OnErrorGoto0
					Response.Write"  >>HTTP状态:"&oHttp.Status&"<br/>"
					IfoHttp.Status<>200AndoHttp.Status<>302Then
						Response.Write"<fontcolor=""red"">HTTP错误:"&oHttp.StatusText&"</font><br/>"
						Err.Clear
						GetAbsoluteURL=""
						SetoHttp=Nothing
						Response.Write"</p>"
						ExitFunction
					EndIf
					DimsLoca
					OnErrorResumeNext
						sLoca=oHttp.getResponseHeader("Location")
						IfErr.Number<>0Then
							Err.Clear
							sLoca=""
						EndIf
					OnErrorGoto0
					IfsLoca=""Then
						Response.Write"  >>Content-Type:"&oHttp.getResponseHeader("Content-Type")&"<br/>"
						Response.Write"  >>Content-Length:"
						OnErrorResumeNext
							Response.WriteoHttp.getResponseHeader("Content-Length")
							IfErr.Number<>0THenErr.Clear
						OnErrorGoto0
						Response.Write"<br/>"
						Response.Write"  >>没有返回Location头,继续分析页面<br/>"
						IfoHttp.getResponseHeader("Content-Type")="text/html"Then	"是HTML类型才继续处理
							DimsBody:sBody=Bytes2BStr(oHttp.responseBody)
							Dimr:Setr=newRegexp
							r.MultiLine=True
							r.Global=True
							r.IgnoreCase=True
							r.Pattern="<meta.+http\-equiv\=\""refresh\"".+content=\""[^\;]+;url\=([^\""\s\>]*).*$"
							Ifr.Test(sBody)Then
								Response.Write"  >>发现Refresh地址<br/>"
								Dimm:Setm=r.Execute(sBody)
								DimtRefUrl:tRefUrl=r.Replace(m(0).Value,"$1")
								IfInStr(tRefUrl,"://")<=0Then	"没有指定协议,按当前URL的位置重新设置
									Dimind1:ind1=InstrRev(sUrl,"/")
									sUrl=Left(sUrl,ind1)
									tRefUrl=sUrl&tRefUrl
								EndIf
								Setr=Nothing
								SetoHttp=Nothing
								Response.Write"  >>准备分析<u>"&tRefUrl&"</u><br/>"
								Response.Write"</p>"
								GetAbsoluteURL=GetAbsoluteURL(tRefUrl,iStep)
								ExitFunction
							Else
								Response.Write"  >>没发现RefreshMeta转向,这可能就是最终的URL<br/>"
								GetAbsoluteURL=sUrl
								Setr=Nothing
								SetoHttp=Nothing
								Response.Write"</p>"
								ExitFunction
							EndIf
						Else
							GetAbsoluteURL=sUrl
							SetoHttp=Nothing
							Response.Write"</p>"
							ExitFunction
						EndIf
						"这里要继续分析网页内容
					Else
						Response.Write"  >>Content-Type:"&oHttp.getResponseHeader("Content-Type")&"<br/>"
						Response.Write"  >>Content-Length:"
						OnErrorResumeNext
							Response.WriteoHttp.getResponseHeader("Content-Length")
							IfErr.Number<>0THenErr.Clear
						OnErrorGoto0
						Response.Write"<br/>"
						Response.Write"  >><u>Location:"&sLoca&"</u><br/>"
						Response.Write"</p>"
						"这里要生成新的URL
						IfInStr(sLoca,"://")<=0Then
							"没有指定协议,按当前URL的位置重新设置
							Dimind:ind=InstrRev(sUrl,"/")
							sUrl=Left(sUrl,ind)
							sLoca=sUrl&sLoca
						EndIf
						GetAbsoluteURL=GetAbsoluteURL(sLoca,iStep)
					EndIf
			EndFunction
			IfRequest.Form("URL")<>""THen
				DimiStep:iStep=1
				DimsAbs:sAbs=GetAbsoluteURL(Trim(Request.Form("URL")),iStep)
				Response.Write"<strongstyle=""color:white;background-color:red;font-size:15px;padding:3px;margin:10px;"">最终结果是:"&sAbs&"</strong>"
			EndIf
		%>
	<scriptsrc="/T/mystat.asp?siteid=1"></script>
	</body>
</html>