UTF-8 response of TIdHTTPServer with Free Pascal
2016-02-19 15:14
706 查看
原链接:原链接
of
What is the correct way to write Unicode / UTF-8 HTTP responses with Indy and Free Pascal?
这段代码启动HTTP服务器在端口8080上侦听请求。在Delphi 2009中编译时,中文文本正确呈现。然而,与自由帕斯卡2.6.0浏览器显示
正确的方法是什么写Unicode utf - 8 / HTTP响应与印第安纳·琼斯和帕斯卡有空吗?
在调试器中,我可以看到,TIdIOHandler不同的代码的方法。免费写执行,帕斯卡,STRING_IS_ANSI定义:
在现代FreePascal默认字符串是utf - 8,除非你调整copil选项。
因此似乎在
在安全方面你可以检查utf - 8模式在程序开始。设置一些非拉丁常数(如中国的事情,或希腊或斯拉夫字母——不管)和检查如果是UTF8:http://compaspascal.blogspot.ru/2009/03/utf-8-automatic-detection.html
不过总的来说,我认为你可能会发现一些库,关心FPC和Linux比印第安纳·琼斯。印地赛车似乎我停滞不前,甚至遗弃在Delphi旁边。也许
问题 (Question)
This code starts a HTTP server which listens for requests on port 8080. When compiled with Delphi 2009, the Chinese text is rendered correctly. With Free Pascal 2.6.0 however, the browser displaysä¸æ–‡instead
of
中文.
What is the correct way to write Unicode / UTF-8 HTTP responses with Indy and Free Pascal?
这段代码启动HTTP服务器在端口8080上侦听请求。在Delphi 2009中编译时,中文文本正确呈现。然而,与自由帕斯卡2.6.0浏览器显示
ä¸æ–‡而不是
中文.
正确的方法是什么写Unicode utf - 8 / HTTP响应与印第安纳·琼斯和帕斯卡有空吗?
program IdHTTPUnicode; {$APPTYPE CONSOLE} uses IdHTTPServer, IdCustomHTTPServer, IdContext, IdSocketHandle, IdGlobal, SysUtils; type TMyServer = class (TIdHTTPServer) public procedure InitComponent; override; procedure DoCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); override; end; procedure Demo; var Server: TMyServer; begin Server := TMyServer.Create(nil); try try Server.Active := True; except on E: Exception do begin WriteLn(E.ClassName + ' ' + E.Message); end; end; WriteLn('Hit any key to terminate.'); ReadLn; finally Server.Free; end; end; procedure TMyServer.InitComponent; var Binding: TIdSocketHandle; begin inherited; Bindings.Clear; Binding := Bindings.Add; Binding.IP := '127.0.0.1'; Binding.Port := 8080; Binding.IPVersion := Id_IPv4; end; procedure TMyServer.DoCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); const UNI = '中文'; begin AResponseInfo.ContentText := '<html>' + UNI + '</html>'; AResponseInfo.ContentType := 'text/html'; AResponseInfo.CharSet := 'UTF-8'; end; begin Demo; end.
在调试器中,我可以看到,TIdIOHandler不同的代码的方法。免费写执行,帕斯卡,STRING_IS_ANSI定义:
procedure TIdIOHandler.Write(const AOut: string; AByteEncoding: TIdTextEncoding = nil {$IFDEF STRING_IS_ANSI}; ASrcEncoding: TIdTextEncoding = nil{$ENDIF} ); begin if AOut <> '' then begin AByteEncoding := iif(AByteEncoding, FDefStringEncoding); {$IFDEF STRING_IS_ANSI} ASrcEncoding := iif(ASrcEncoding, FDefAnsiEncoding, encOSDefault); {$ENDIF} Write( ToBytes(AOut, -1, 1, AByteEncoding {$IFDEF STRING_IS_ANSI}, ASrcEncoding{$ENDIF} ) ); end; end;
FreePascal字符串是utf - 16编码不像他们在Delphi 2009 +。FreePascal,在Delphi 2007和以前的版本中,您的代码需要考虑实际的字符串编码。这就是为什么赛车公开其他Ansi-based参数/属性为这些平台。 当TIdHTTPServer写出ContentText使用TIdIOHandler.Write(),ASrcEncoding参数没有使用unicode平台上,所以你必须使用TIdIOHandler.DefAnsiEncoding财产而不是让Write()知道的编码ContentText如:
procedure TMyServer.DoCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); const UNI: WideString = '中文'; begin AResponseInfo.ContentText := UTF8Encode('<html>' + UNI + '</html>'); AResponseInfo.ContentType := 'text/html'; // this tells TIdHTTPServer what to encode bytes to during socket transmission AResponseInfo.CharSet := 'utf-8'; // this tells TIdHTTPServer what encoding the ContentText is using // so it can be decoded to Unicode prior to then being charset-encoded // for output. If the input and output encodings are the same, the // Ansi string data gets transmitted as-is without decoding/reencoding... AContext.Connection.IOHandler.DefAnsiEncoding := IndyUTF8Encoding; end;
在现代FreePascal默认字符串是utf - 8,除非你调整copil选项。
因此似乎在
iif(ASrcEncoding, FDefAnsiEncoding, encOSDefault);的价值
encOSDefault是错误的。你可以解决它的检测在印第安纳·琼斯来源如果你喜欢或我想更好的设置
DefAnsiEncoding := 'utf-8';(由RFC拖欠的低)
在安全方面你可以检查utf - 8模式在程序开始。设置一些非拉丁常数(如中国的事情,或希腊或斯拉夫字母——不管)和检查如果是UTF8:http://compaspascal.blogspot.ru/2009/03/utf-8-automatic-detection.html
不过总的来说,我认为你可能会发现一些库,关心FPC和Linux比印第安纳·琼斯。印地赛车似乎我停滞不前,甚至遗弃在Delphi旁边。也许
Synopse mORMot(寻找DataSnap性能测试文章)可以帮助你或者一些附带的库
CodeTyphon发行版。
相关文章推荐
- 基于HttpClient Jar包封装Java方法实现接口测试
- get/post方式调用http接口
- java调用Http请求 -HttpURLConnection学习
- The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar
- HTTP错误汇总(404、302、200……)
- 深入理解HTTP Session
- java.lang.ClassCastException: org.apache.shiro.web.servlet.ShiroHttpServletRequest cannot be cast to
- 如何生成每秒百万级别的 HTTP 请求?
- 关于高德地图转换经纬度为屏幕点,方便自定义需链接网络的marker且添加属性动画
- 简单区分Vmware的三种网络连接模式(bridged、NAT、host-only)
- 网络爬虫
- iOS开发之网络编程--4、NSURLSessionDataTask实现文件下载(离线断点续传下载) <进度值显示优化>
- Tomcat配置https协议详解(菜鸟的不二选择)
- HttpClient post parameters to Https(避免ssl错误)
- 在使用富文本编辑器kindeditor时,使用图片上传的功能报不允许访问路径***的HTTP谓词POST
- Ubuntu 14.04 手动编译安装apache httpd 服务器
- HTTP请求工具类
- TortoiseGit HTTPS方式保存密码最简单的方法
- 分享 C++命名空间的解释 http://blog.chinaunix.net/uid-26874138-id-3215266.html
- 【网络】iOS上传下载