今天凌晨双子座的流星雨,北京,冷,我看到了3颗^^。逆向技术有了一个层次的提高后,我又回到web2.0上了,ASP.NET(C#)开始跳跃着看了,oocos也已经策划好,也许会等到英语六级过后,再着手开发吧(有点紧张)。回到主题!看下文:
这次实验,一个是针对新浪的天气预报,一个针对百度的搜索引擎。如果他们有像Google那样开放AJAX API的话,我就不用这样做了。写这篇文章时,我还有一些疑问没解开,其实是还没继续扩展下去。文中的一些代码并非原创,比如bytes2BSTR函数,我到现在还不知道如何抛弃这个函数……
AJAX在本地获取远程数据,其实就是XMLHttpRequest对象的使用。先看获取新浪天气预报的效果截图:
加载数据时……

数据加载完毕……

相关代码:
ajaxWeather.html文件的代码如下(看注释):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>AJAX异步天气预报</title>
//xmlhttp_cos.js是我自己的ajax库文件,xRequest(method,url,asynch,x_handle)为接口函数。
<script type="text/javascript" src="xmlhttp_cos.js"></script>
//encode.vbs中包含上面说的bytes2BSTR函数,作用是将远程返回的数据正确编码。
<script type="text/vbscript" src="encode.vbs"></script>
<script type="text/javascript">
function $(d){return document.getElementById(d);}
function getWeather(){
var city = $("city").value;
var method = "get";
var url = "http://php.weather.sina.com.cn/search.php?city=" + city;
var asynch = true;
$("weather").innerHTML = "<img src=\"loading.gif\" border=0 />";
//xmlhttp_cos.js中的接口函数xRequest(),有四个参数。
xRequest(method,url,asynch,x_handle);
}
//x_handle()函数的作用:用来判断异步调用状态与处理远程返回的数据。
function x_handle(){
try{
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
var data = bytes2BSTR(xmlhttp.responseBody);
var begin = data.indexOf("天气状况 begin");
var end = data.indexOf("Weather_SM");
var weather = data.substring(begin + 14,end - 12);
$("weather").innerHTML = weather;
}
}
}catch(e){
alert("服务器出现异常,也许已经被外星人劫持!请重试。");
}
}
</script>
<style type="text/css">
//省略CSS样式,表现层的东西我们不管……
</style>
</head>
<body>
<br />
<input type="text" id="city" value="北京" />
//点击“天气查询”按钮时触发getWeather(),getWeather()函数在上面定义了。
<input type="button" value="天气查询" onclick="getWeather()" />
<br /><br />
<div id="weather"></div>
</body>
</html>
xmlhttp_cos.js文件的代码如下(看注释):
var xmlhttp = null;
function xRequest(method,url,asynch,x_handle){
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest(); //火狐浏览器。
}
else if(window.ActiveXObject){
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); //IE浏览器。
if(!xmlhttp){xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
}
if(xmlhttp){
if(method.toLowerCase() != "post"){ //判断method参数的值是post还是get。
initRequest(method,url,asynch,x_handle);
}else{
var args = arguments[4];
if(args != null && args.length > 0){
initRequest(method,url,asynch,x_handle,args);
}
}
}else{
alert("您的浏览器居然不支持XMLHttpRequest组件!")
}
}
function initRequest(method,url,asynch,x_handle){
try{
xmlhttp.onreadystatechange = x_handle; //调用x_handle()函数,用来判断异步调用状态与处理远程返回的数据。
xmlhttp.open(method,url,asynch); //加载url对应的远程服务器来处理我们提交的数据。
if(method.toLowerCase() == "post"){
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send(arguments[4]); //发送post类型的异步请求。
}else{xmlhttp.send(null);} //发送get类型的异步请求。
}catch(e){
alert("连接出现异常,也许是外星电波干涉!请重试。")
}
}
encode.vbs文件的代码如下(bytes2BSTR函数的作用是将远程返回的数据正确编码):
Function bytes2BSTR(vIn)
Dim strReturn,i,ThisCharCode,innerCode,Hight8,Low8,NextCharCode
strReturn = ""
For i = 1 to LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i + 1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
结束。AJAX的核心知识也在里面,并且使用了错误异常处理,这样代码会更健壮!异步获取百度搜索引擎的数据就不介绍了,其实和上面的方法是一样的。关键原理是下面这样的参数传递,比如新浪天气预报的查询url是这样的:
http://php.weather.sina.com.cn/search.php?city=城市
等号=后面跟着我们要赋予的查询值,而百度搜索引擎的查询url是这样的:http://www.baidu.com/s?wd=关键词。原理一样。完整实例可以到我的网盘上去下载http://ycosxhack.ys168.com/,“网页设计”目录,文件名为“AJAX本地获取远程数据.rar”。