我正在尝试在 ASP 中制作 LINK FINDER 应用程序
它的工作分为5个步骤
Send http request to server to parsed link
我可以完成前4步,但第5步面临挑战
我收到 3 种类型的链接
1.)绝对链接:http://www.foo.com/file.asp
2.)来自根目录的链接,需要域名,例如/folder2/file2.asp
3.)相对链接:../file3.asp
当我请求 www.foo.com 时,301 重定向 到 www.foo.com/folder3/folder3/file3.asp
我正在获取重定向页面的html内容,但没有获取重定向的url并且无法检查第三种类型的链接
使用以下代码
Set ServerXmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0") ServerXmlHttp.open "GET", "http://www.foo.com" ServerXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" ServerXmlHttp.send PostData If ServerXmlHttp.status = 200 Then //My CODE
希望尽快回复... 或任何其他关于 asp 、 vb.net 中的链接查找器的想法
它超出了 ServerXMLHTTP 功能。
相反,您必须使用 IWinHttpRequest 或其他能够管理重定向的第三方组件。
在以下示例中,即使重定向,
req.Option(WHR_URL)
也会返回当前 url。req.option(WHR_EnableRedirects)
默认为 True
,如 ServerXMLHTTP。Const WHR_URL = 1
Const WHR_EnableRedirects = 6
'Enum constants are listed at http://msdn.microsoft.com/en-us/library/windows/desktop/aa384108(v=vs.85).aspx
Dim req
Set req = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
'req.Option(WHR_EnableRedirects) = False 'don't follow the redirects
req.open "GET", "http://www.foo.com", False
req.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
req.send PostData
If req.Status = 200 Then
Response.Write "Last URL : " & req.Option(WHR_URL)
End If
我也一直在挣扎,但解决方案是验证最终url是否等于起始url。
在“发送”之后立即调用 getOption(-1) 将为您提供最终的 url。
然后使用此最终网址重新发送您的请求。
这是我使用 MSXML2.ServerXMLHTTP.3.0 来自 sql server 的工作示例:
声明@err_code INT、@response_text VARCHAR(MAX)、@oa_object INT、@status int 声明 @url varchar(4000)='https://api.insocial.nl/v2/customers/' 声明@final_url varchar(4000) 声明 @result_table 表(结果 varchar(max))
EXEC @err_code=sp_OACreate 'MSXML2.ServerXMLHTTP.3.0', @oa_object OUT; Exec @err_code=sp_OAMethod @oa_object, 'open', NULL, 'GET', @url,'false', null, null Exec @err_code=sp_OAMethod @oa_object, 'setTimeouts',NULL,5000,5000,10000,1200000 Exec @err_code=sp_OAMethod @oa_object, 'setRequestHeader', NULL, 'X-AUTH-TOKEN', '***' Exec @err_code=sp_OAMethod @oa_object, '发送', Null, null
Exec @err_code=sp_OAMethod @oa_object,'getOption(-1)',@final_url OUT 打印@final_url if (@final_url <> @url) 开始 Exec @err_code=sp_OAMethod @oa_object, 'open', NULL, 'GET', @final_url,'false', null, null Exec @err_code=sp_OAMethod @oa_object, 'setTimeouts',NULL,5000,5000,10000,1200000 Exec @err_code=sp_OAMethod @oa_object, 'setRequestHeader', NULL, 'X-AUTH-TOKEN', '***' Exec @err_code=sp_OAMethod @oa_object, '发送', Null, null 结束
SET @response_text = null --确保我们不会返回垃圾 插入@result_table(结果) EXEC @err_code = sp_OAGetProperty @oa_object, 'responseText' 从@result_table中选择*
执行 sp_OADestroy @oa_object