Ajax agent actively determines the character encoding

Source: Internet
Author: User
Because ajax has a title on cross-origin visits, the best way to do it is to act as a proxy and write a proxy program and experience. In order to act as an ajax proxy, I studied the xmlhttp on the server and compared it with the xmlhttp in the client's ajax. the background code is asp.

Because ajax has a title on cross-origin visits, the best way to do it is to act as a proxy and write a proxy program and experience.

In order to act as an ajax proxy, I studied the xmlhttp on the server and compared it with the xmlhttp in the client's ajax. the background code is asp.

The xmlhttp server is an asp thief program. I changed the code to javascript.

1. the xmlhttp. Open ("GET", url, false) asynchronization on the server must be closed, and the client can Open the xmlhttp. Open ("GET", url, false) asynchronization.

2. xmlhttp on the server. responsebody uses Responsebody instead of ResponseText or ResponseXml. at the beginning, I use ResponseText, but an error is prompted when the bytesToBSTR function is used for encoding conversion, after comparison, the code in other asp thieves were found to be Responsebody. after analysis, the invention body returned binary data instead of returning characters or dom objects like ResponseText or ResponseXml.

Asp proxy functions of ajax:

Send_request (url). The url is the address.

The server code is as follows: take the initiative to determine all character encoding. the Japanese and Korean traditional Chinese have been tested:

Reference content is as follows:

<% @ LANGUAGE = "JAVASCRIPT" CODEPAGE = "65001" %>


Http://www.w3.org/TR/html4/loose.dtd>



XMLHTTP

<%

 

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 ){
// Actively determine 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.

// Actively determine 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 );
}

%>

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




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.