url 中文编解码
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.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/3819685.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
2013-07-02 pci转并口卡的安装使用