这个功能经常出现在采集的时候,有的网站一些信息限制了非会员访问,所以要采集之前,需要登录,然后才能采集相关的信息。这里介绍一下使用asp程序来模拟登录这些网站这个功能。采集的功能这里就不介绍了,自己去看我以前写的一个采集例子去:asp简单的采集代码教程。
asp模拟登录代码如下:
<% '强制浏览器重新访问服务器下载页面,而不是从缓存读取页面 Response.Buffer = True Response.Expires = -1 Response.ExpiresAbsolute = Now() - 1 Response.Expires = 0 Response.CacheControl = "no-cache" '================================================== '函数名:PostHttpPage '作 用:登录 '参 数:RefererUr-----登录地址 '参 数:PostUrl-----提交地址 '参 数:PostData-----用户参数 '================================================== Function PostHttpPage(RefererUrl,PostUrl,PostData) Dim xmlHttp Dim RetStr Set xmlHttp = CreateObject("Msx" & "ml2.XM" & "LHT" & "TP") xmlHttp.Open "POST", PostUrl, False XmlHTTP.setRequestHeader "Content-Length",Len(PostData) xmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" xmlHttp.setRequestHeader "Referer", RefererUrl xmlHttp.Send PostData If Err.Number <> 0 Then Set xmlHttp=Nothing PostHttpPage = "$False$" Exit Function End If PostHttpPage=bytesToBSTR(xmlHttp.responseBody,"utf-8") Set xmlHttp = nothing End Function Function getHTTPPage(url) On Error Resume Next dim http set http=Server.createobject("Msx" & "ml2.XM" & "LHT" & "TP") Http.open "GET",url,false Http.send() if Http.readystate<>4 then exit function end if getHTTPPage=bytesToBSTR(Http.responseBody,"utf-8") set http=nothing If Err.number<>0 then Response.Write "<p align='center'><font color='red'><b>服务器获取文件内容出错</b></font></p>" Err.Clear End If End Function Function BytesToBstr(strBody,CodeBase) dim obj set obj=Server.CreateObject("Adodb.Stream") obj.Type=1 obj.Mode=3 obj.Open obj.Write strBody obj.Position=0 obj.Type=2 obj.Charset=CodeBase BytesToBstr=obj.ReadText obj.Close set obj=nothing End Function '模拟登陆 str = PostHttpPage("http://www.aspbc.com/login.html","http://www.aspbc.com/login.asp","username=***&password=***&login="&dopost("登录")&"") response.write str %> <script language="javascript" runat="server" type="text/javascript"> function dopost(str) { str=escape(str); return str; } </script>(鼠标移到代码上去,在代码的顶部会出现四个图标,第一个是查看源代码,第二个是复制代码,第三个是打印代码,第四个是帮助)
注意:"username=***&password=***&login="&dopost("登录")&""这里是页面提交的参数和值,这个要根据那个登录页面来设置的,可以查看页面源文件得到这些值,然后写成相应的字符串放在这里。我建议使用httpWatch软件来得到这些值比较容易看清楚。
原创文章,请注明从asp编程网(www.aspbc.com)转载,谢谢。