An AJAX agent automatically judges character encoding

Source: Internet
Author: User
Tags comparison return client
Ajax| Code | program

Because Ajax has problems with Cross-domain access, the best way to do this is to act as an agent. wrote an agent program and experience.

In order to do AJAX agent, the study of the server-side XMLHTTP and the client's Ajax in the XMLHTTP made a comparison, the background code is ASP.

Server-side XMLHTTP is the ASP thief program, I changed the code to JavaScript.

1. XMLHTTP on the server side. Open ("Get", Url,false) asynchronous must be closed, and the client's asynchronous is open, which is well understood.
2. XMLHTTP on the server side. Responsebody here with responsebody instead of ResponseText or responsexml, at first I used responsetext, but when the function bytestobstr the conversion code, it prompts the error, After the comparison found that other ASP thieves in the code are responsebody, analysis, found that the body returned to the binary data rather than responsetext or responsexml like the return of characters or DOM objects.

Introduction to ASP Proxy functions for Ajax:
Send_request (URL), URL is address

The server-side code follows automatically to determine all character encodings, tested Japanese Korean traditional:

<% @LANGUAGE = "JAVASCRIPT" codepage= "65001"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"

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

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) {
Automatic Judgment Coding Start
var charresult = http_request. Responsetext.match (/charset= (\s+) \ ">/i);
if (Charresult!= null) {
var Cset = charresult[1];
}else{cset = "gb2312"}//is gb2312 encoded for Web sites that are not available and can be changed by itself

Automatic Judgment Coding End
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>

Author: Llinzzi

<

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.