ajax代理程序自动判断字符编码

来源:互联网 发布:python 确定文件存在 编辑:程序博客网 时间:2024/04/29 03:35
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

由于ajax在跨域的访问上有问题,目前最好的方法是做代理.写了个代理程序和心得.

为了做ajax的代理,研究了下服务器端的xmlhttp并和客户端的ajax中的xmlhttp做了个比较,后台代码是asp的.

服务器端的xmlhttp也就是asp小偷程序,我把代码改成了javascript.

1.在服务器端的xmlhttp.Open("GET",url,false)异步必须是关闭的,而客户端的异步是打开的,这个很好理解.
2.在服务器端的xmlhttp.Responsebody 这里用的是Responsebody而不是ResponseText或ResponseXml,一开始我是用ResponseText,但在函数bytesToBSTR转换编码的时候提示错误,经过比较发现其他的asp小偷程序里的代码都是Responsebody,分析后,发现body返回来的是二进制数据而不是像ResponseText或ResponseXml那样返回字符或dom对象.

ajax的asp代理函数介绍:
send_request(url) ,url为地址

服务器端代码如下带自动判断所有字符编码,已测试 日语 韩语 繁体:

<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>XMLHTTP</title>
</head>
<%

Server.ScriptTimeout=9999999;
function send_request(url) {
 var codedtext;
 http_request = Server.CreateObject("Microsoft.XMLHTTP");
 http_request.Open("GET",url,false);
 http_request.Send(null);
 if (http_request.ReadyState == 4){
  //自动判断编码开始
  var charresult = http_request.ResponseText.match(/CharSet=(/S+)/">/i);
  if (charresult != null){
  var Cset = charresult[1];
  }else{Cset = "gb2312"}//对获取不到的网站采用gb2312编码,可自行更改
  
//自动判断编码结束
  codedtext = bytesToBSTR(http_request.Responsebody,Cset);
  }else{
  codedtext = "Erro";
  }
 return(codedtext);
}

function bytesToBSTR(body,Cset){
var objstream;
objstream = Server.CreateObject("Adodb.Stream");
objstream.Type = 1;
objstream.Mode = 3;
objstream.Open();
objstream.Write(body);
objstream.Position = 0;
objstream.Type = 2;
objstream.Charset = Cset;
bytesToBSTR = objstream.Readtext;
objstream.Close;
return(bytesToBSTR);
}

%>
<body>
<% Response.Write(send_request("http://www.daum.net")) %>
</body>
</html>

作者:llinzzi 

<<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击