Ajax agent automatically determines the character encoding

Source: Internet
Author: User

Because ajax has a problem with cross-origin access, the best method is to act as a proxy. I wrote a proxy program and my experiences.
In order to act as an ajax proxy, I studied xmlhttp on the lower server and compared it with xmlhttp on the client ajax. The background code is asp
The xmlhttp on the server is the asp thief program. I changed the code to javascript.
1. the xmlhttp. Open ("GET", url, false) Asynchronization on the server must be disabled, while the client Asynchronization is enabled, which is understandable.
2. the xmlhttp. Responsebody on the server uses Responsebody instead of ResponseText or ResponseXml. At first, I used ResponseText, but in the function

An error is prompted when bytesToBSTR is converted. After comparison, it is found that the code in other asp thieves programs is Responsebody. After analysis, it is found that the body returns binary data and

It does not return characters or dom objects like ResponseText or ResponseXml.
Introduction to asp proxy functions of ajax
Send_request (url). The url is the address.
The server-side code is as follows with automatic identification of all character codes. The Japanese, Korean, and traditional Chinese characters have been tested.Copy codeThe Code is as follows: <% @ 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 ){
// Automatically determines the start of Encoding
Var charresult = http_request.ResponseText.match (/CharSet = (\ S +) \ ">/I );
If (charresult! = Null ){
Var Cset = charresult [1];
} Else {Cset = "gb2312"} // gb2312 code is used for websites that cannot be obtained. You can change the code on your own.

// Automatically determines the end Of the Encoding
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>

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.