URL Chinese Codec

Source: Internet
Author: User

Unit unit1;

Interface

Uses
Winapi. Windows, winapi. Messages, system. sysutils, system. variants,
System. Classes, VCL. Graphics,
VCL. Controls, VCL. Forms, VCL. dialogs, VCL. stdctrls, idbasecomponent,
Idcomponent, idtcpconnection, idtcpclient, idhttp, Web. httpapp;

Type
Tform1 = Class (tform)
HTTP: tidhttp;
Button1: tbutton;
Memo1: tmemo;
Procedure button1click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;

VaR
Form1: tform1;

Implementation

{$ R *. DFM}

// Convert Chinese to JavaScript strings
Function encodejsstr (const value: widestring): widestring;
VaR
P: pwidechar;
Begin
Result: = '';
P: = pwidechar (value );
While P ^ <> #0 do
Begin
Case P ^
'"','\','/':
Result: = Result + '\' + P ^;
# $08:
Result: = Result + '\ B ';
# $ 0C:
Result: = Result + '\ F ';
# $ 0a:
Result: = Result + '\ n ';
# $ 0d:
Result: = Result + '\ R ';
# $09:
Result: = Result + '\ T ';
Else
If word (P ^)> $ FF then
Result: = Result + lowercase (format ('\ U % x', [word (P ^)])
Else
Result: = Result + P ^;
End;
INC (P );
End;
End;

// Restores a javascript string to a Chinese character
Function decodejsstr (const value: widestring): widestring;
VaR
P: pwidechar;
V: widechar;
TMP: widestring;
Begin
Result: = '';
P: = pwidechar (value );
While P ^ <> #0 do
Begin
V: = #0;
Case P ^
'\':
Begin
INC (P );
Case P ^
'"','\','/':
V: = P ^;
'B ':
V: = # $08;
'F ':
V: = # $ 0C;
'N ':
V: = # $ 0a;
'R ':
V: = # $ 0d;
'T ':
V: = # $09;
'U ':
Begin
TMP: = copy (p, 2, 4 );
V: = widechar (strtoint ('$' + TMP ));
INC (p, 4 );
End;
End;
End;
Else
V: = P ^;
End;
Result: = Result + V;
INC (P );
End;
End;

Function urlencode (STR: string): string;
VaR
B: byte;
Begin
For B in bytesof (utf8encode (STR) do
Result: = format ('% S %. 2x', [result, B]);
End;

Procedure tform1.button1click (Sender: tobject );
VaR
S, R: string;
Begin
// S: = urlencode ('yunnan studio ');
// S: = httpencode (ansitoutf8 ('yunnan studio '));
S: = httpencode (utf8encode ('yunnan studio '));
R: = http. Get ('HTTP: // localhost: 8080/YN/rest/tservermethods1/ECHO/'+ S );
// Memo1.text: = httpdecode (R );
Memo1.text: = decodejsstr (R );
End;

End.

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.