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}

// 中文转换为javascript字串
function EncodeJSStr(const value: Widestring): Widestring;
var
P: PWideChar;
begin
Result := '';
P := PWideChar(value);
while P^ <> #0 do
begin
case P^ of
'"', '\', '/':
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;

// javascript字串还原为汉字
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^ of
'\':
begin
inc(P);
case P^ of
'"', '\', '/':
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('咏南工作室');
// s:= HttpEncode(AnsiToUtf8('咏南工作室'));
s:=HTTPEncode(UTF8Encode('咏南工作室'));
r := http.get('http://localhost:8080/yn/rest/TServerMethods1/echo/' + S);
// Memo1.Text:= HTTPDecode(r);
Memo1.Text := DecodeJSStr(r);
end;

end.

url 中文编解码的更多相关文章

  1. Web开发须知:URL编码与解码

    通常如果一样东西需要编码,说明这样东西并不适合传输.原因多种多样,如Size过大,包含隐私数据,对于Url来说,之所以要进行编码,是因为Url中有些字符会引起歧义. 例如,Url参数字符串中使用key ...

  2. 【转】Java web 编解码

    几种常见的编码格式 为什么要编码 不知道大家有没有想过一个问题,那就是为什么要编码?我们能不能不编码?要回答这个问题必须要回到计算机是如何表示我们人类能够理解的符号的,这些符号也就是我们人类使用的语言 ...

  3. Javascript中的url编码与解码(详解)

    摘要 本文主要针对URI编解码的相关问题做了介绍,对url编码中哪些字符需要编码.为什么需要编码做了详细的说明,并对比分析了Javascript中和编解码相关的几对函数escape / unescap ...

  4. Web开发:URL编码与解码(转)

    原文:http://www.cnblogs.com/greatverve/archive/2011/12/12/URL-Encoding-Decoding.html 通常如果一样东西需要编码,说明这样 ...

  5. Web开发:URL编码与解码

    通常如果一样东西需要编码,说明这样东西并不适合传输.原因多种多样,如Size过大,包含隐私数据,对于Url来说,之所以要进行编码,是因为Url中有些字符会引起歧义. 例如Url参数字符串中使用key= ...

  6. JSP与Servlet的编解码

    一.java web中涉及编解码的地方 (1)浏览器端向后台发起请求时:URL.Cookie.Parameter: (2)后台响应返回数据时:页面编码,数据库数据编码:

  7. URL编码和解码工具

    开发中发现需要进行URL的编解码,每次百度出来的还带广告而且比较慢,写了一个本地的工具,比较简单,希望对大家有帮助. <!DOCTYPE html PUBLIC "-//W3C//DT ...

  8. Java Web中涉及的编解码

    用户从浏览器发起一个HTTP请求,存在编码的地方是URL.Cookie.Paramiter.服务器端接收到HTTP请求后要解析HTTP协议,其中URL.Cookie和POST表单参数要解码,服务器端可 ...

  9. javascript实现URL编码与解码

    一.预备知识 URI是统一资源标识的意思,通常我们所说的URL只是URI的一种.典型URL的格式如下所示.下面提到的URL编码,实际上应该指的是URI编码. foo://example.com:804 ...

随机推荐

  1. Android 图片旋转(使用Matrix.setRotate方法)

    imageView2 = (ImageView) findViewById(R.id.img2); Bitmap bitmap = BitmapFactory.decodeResource(getRe ...

  2. 全选与反选(dom与jquery比较)

    <html> <head> <title>全选或反选(dom)</title> <meta http-equiv="Content-Ty ...

  3. OpenStack 部署运维实战

    http://www.ibm.com/developerworks/cn/cloud/library/1408_zhangxl_openstack/#icomments 本文为您介绍了网易公司基于 O ...

  4. 再分析 返回值加引用&,const

    本文主要分析,返回&,和返回值加const的作用. 返回& 定义一个数组模板: template<class T>class Array{ enum{size = 100} ...

  5. Ubuntu Telnet 配置(openbsd-inetd)

    Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.可以通过Telnet实现远程登录Ubuntu,但是Ubuntu 10.10默认没有安装Telnet,需 ...

  6. “WIZnet杯”以太网技术竞赛即将开始!

  7. 编程概念--使用async和await的异步编程

    Asynchronous Programming with Async and Await You can avoid performance bottlenecks and enhance the ...

  8. JsonPath详解

    JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPat ...

  9. multiselect2side双向选择列表插件改进版

    为了满足实际程序需要,特在multiselect2side插件的基础上改进已满足异步加载(如MVC)的需要.增加了加载url.saveurl属性以及控件实时刷新加载数据方法,请注意下面红色标记部分. ...

  10. 【转】10分钟搭建NDK的Android开发环境

    原文网址:http://blog.csdn.net/u012176591/article/details/23018913 作者:金良(golden1314521@gmail.com) csdn博客: ...