indy httpserver 接收URL包含中文参数乱码的问题
来源:互联网 发布:mysql数据库建表 编辑:程序博客网 时间:2024/05/18 11:41
在测试TIdHttpServer的时候,发现浏览器提交的URL包含中文时会乱码,我用的是XE7 UPDATE1,INDY是10。
procedure TForm1.idhtpsrv1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo);
在这事件处理中文时,AResponseInfo好办,只要设置下
AResponseInfo.ContentType := 'text/html';
AResponseInfo.CharSet := 'utf-8';
浏览器就能正确显现中文。
对于 ARequestInfo,回头想想一开始决解方向就错了,不要想着各种编码转换,实际到这里,INDY已经把编码转换好了,因为到这里处理的文本,
ARequestInfo.Params.Text已经是string类型了(UNICODE)。网上说是改INDY的源码,因为没地方设置TIdHTTPRequestInfo编码属性。
改动的是IdCustomHTTPServer.pas的 TIdHTTPRequestInfo.DecodeAndSetParams(const AValue: String)方法:
procedure TIdHTTPRequestInfo.DecodeAndSetParams(const AValue: String);var i, j : Integer; s: string; LEncoding: IIdTextEncoding;begin // Convert special characters // ampersand '&' separates values {Do not Localize} Params.BeginUpdate; try Params.Clear; CharSet := 'utf-8'; //添加这一行 // which charset to use for decoding query string parameters. We // should not be using the 'Content-Type' charset for that. For // 'application/x-www-form-urlencoded' forms, we should be, though... LEncoding := CharsetToEncoding(CharSet); i := 1; while i <= Length(AValue) do begin j := i; while (j <= Length(AValue)) and (AValue[j] <> '&') do {do not localize} begin Inc(j); end; s := Copy(AValue, i, j-i); // See RFC 1866 section 8.2.1. TP s := StringReplace(s, '+', ' ', [rfReplaceAll]); {do not localize} Params.Add(TIdURI.URLDecode(s, LEncoding)); i := j + 1; end; finally Params.EndUpdate; end;end;
然后重新编译IndyProtocols包,注意bpl文件放在bin下,dcu和dcp文件放在Lib下的各个相应目录下(就是win32/win64, debug/release)
测试代码:
procedure TForm1.idhtpsrv1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);begin AResponseInfo.ContentType := 'text/html'; AResponseInfo.CharSet := 'utf-8'; mmo1.Lines.Add('====================='); mmo1.Lines.Add('Document:'); mmo1.Lines.Add(''); mmo1.Lines.Add(ARequestInfo.Params.Text); mmo1.Lines.Add('p1: '+ ARequestInfo.Params.Values['p1']); mmo1.Lines.Add('p2: '+ ARequestInfo.Params.Values['p2']); AResponseInfo.ContentText := '是中文吧'; // 'HttpServer OK';end;
浏览器URL:http://127.0.0.1:8000/test?p1=中文&p2=ghg
用 Chrome,edge,360浏览器测试正确。
0 0
- indy httpserver 接收URL包含中文参数乱码的问题
- springmvc 接收参数中文乱码的问题
- strust2接收URL参数为中文是乱码问题的解决
- asp.net接收URL中文参数的乱码解决方案
- spring mvc 框架URL接收中文参数的乱码解决方案
- 解决URL中文参数乱码的问题
- 解决URL中文参数乱码的问题
- url传递中文参数乱码的问题
- URL中包含中文参数造成乱码的解决之道
- 提交表单乱码问题 action 中文接收的参数乱码
- url中文参数乱码问题
- struts2 action接收到的参数中文乱码的问题
- URL中传递参数时包含中文的问题
- Url传递参数和接收参数时的中文乱码处理
- AJAX请求URL中带入中文参数,后台接收乱码问题
- Android接收服务器参数中文乱码问题
- Struts2 接收参数 中文乱码问题
- SpringMVC接收参数中文乱码问题
- JSP文件上传在servlet收集参数的详解
- linux lftp命令 详解
- Mysql 主主互备
- Part7 模板与泛型编程 Templates and Generic Programming
- SharedPreferences自动登录及存储多组相同类型数据
- indy httpserver 接收URL包含中文参数乱码的问题
- 2016-7-6 杂感
- hive-导入数据的方式
- Android Studio官方文档之书编写你的App(Write Your App )
- SpringMVC解决多人开发路径可能重复的问题
- Android 自定义底部导航栏&消息显示
- Swift 写一个简单界面
- 基于注解的SpringMVC简单介绍
- leetcode 151. Reverse Words in a String