asp模拟网站登录采集需要验证的页面信息

  本示例实现asp模拟登录网站,然后捉取或者采集需要session验证的asp页面信息,效果如下

asp模拟网站登录采集需要验证的页面信息

 

提示:不能使用XMLHTTP/ServerXMLHTTP组件,需要使用底层的WinHttp.WinHttpRequest对象

 

asp源代码如下

登录页面:login.asp

<%
if request.Form("pwd")="123" then'asp登录认证,简单示例
  Session("user")="showbo"
  response.Redirect "index.asp"
  response.End
end if
 %>
 <form method="post">
 PassWord:<input type="password" name="pwd" /><input type="submit" value="Submit" />
 </form>

输出session的页面或者需要验证信息的页面:index.asp

<%
if session("user")<>"" then
  response.Write "Session(""user"")="&Session("user")
else
  response.Write "NULL"
end if %>

asp模拟登陆系统,然后捉取需要session验证的页面信息

<h1>第一组,WinHttp.WinHttpRequest.5.1直接请求输出session的首页,没有等到需要的值</h1>
<%
set xhr=Server.CreateObject("WinHttp.WinHttpRequest.5.1")
xhr.option(6)=false'不允许跳转
xhr.open "GET","http://root.w3dev.cn/index.asp",false
xhr.send
response.Write xhr.responseText
set xhr=nothing
 %>
 <hr />
 <h1>第二组,需要发送2次请求</h1>
 <%
set xhr=Server.CreateObject("WinHttp.WinHttpRequest.5.1")
xhr.option(6)=false'不允许跳转,要是允许跳转会保持cookie一致,就没什么意义了
xhr.open "POST","http://root.w3dev.cn/login.asp",false
xhr.setRequestHeader "content-type","application/x-www-form-urlencoded"
xhr.send "pwd=123"
header=replace(xhr.GetAllResponseHeaders,chr(10),"")'获取响应头
set xhr=nothing
arr=split(header,chr(13))
cookie=""
for i=0 to ubound(arr)'分析得到返回的此次请求的对应的session的cookie值,以便下一次请求一起发送cookie
  if instr(arr(i),"Set-Cookie")=1 then
    cookie=trim(replace(split(arr(i),";")(0),"Set-Cookie:",""))
    exit for
  end if
next
response.Write "ASPSession Cookie:"&cookie&"<br/>"'可以保存分析得到的cookie值,下次新建立的的httprequest对象可以将cookie头设置为保存的值,就可以获取到需要session验证的页面的内容了
'再次请求首页,附带上上一次的cookie
set xhr=Server.CreateObject("WinHttp.WinHttpRequest.5.1")'注意使用新httprequest对象,使用上面那个cookie会自动继承下来
xhr.open "GET","http://root.w3dev.cn/index.asp",false
xhr.setRequestHeader "cookie",cookie'可以注释掉这句看看效果,注释掉得到得到就是null,因为没有cookie
xhr.send
response.Write xhr.responseText
set xhr=nothing
%>

更多WinHttp.WinHttpRequest的使用帮助:asp WinHttp.WinHttpRequest对象方法属性事件api

加支付宝好友偷能量挖...


原创文章,转载请注明出处:asp模拟网站登录采集需要验证的页面信息

评论(0)Web开发网
阅读(652)喜欢(1)Asp/VBScript