delphi 与 c# post 返回不同的字符串 - utf 问题?
我发布了两种形式 - 一种在 C# 中,一种在 delphi 中。但结果字符串似乎不同:
c# 返回:¤@@1@@@@1@@@@1@@xśm˱Â0Đ...
delphi 返回: #$1E'@@1@@@@1@@@@1@@x'#$009C...
和 sice 都是压缩流我在尝试时遇到错误解压缩它... C# 是“正确的” - 即。 我不是 delphi 方面的专家 - 我只需要将一些代码从 c# 转换为 delphi。
c# 代码:
string GetData(Hashtable aParam, string ServerURL)
{
string Result = "";
WebRequest Request = HttpWebRequest.Create(ServerURL);
Request.Method = "POST";
Request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
UTF8Encoding encUTF8 = new System.Text.UTF8Encoding(false);
StreamWriter writer = new StreamWriter(Request.GetRequestStream(), encUTF8);
foreach (DictionaryEntry element in aParam)
{
writer.Write(element.Key + "=" + element.Value + "&");
}
writer.Close();
writer.Dispose();
WebResponse Response = Request.GetResponse();
StreamReader Reader = new StreamReader(Response.GetResponseStream(), System.Text.Encoding.Default);
Result = Reader.ReadToEnd();
Reader.Close();
Response.Close();
Reader.Dispose();
return Result;
}
delphi 代码:
function GetData(aParam:TStringList; ServerURL:string):string;
var
req: TIdHTTP;
res: string;
begin
req := TIdHTTP.Create();
with req do
begin
Request.ContentType := 'application/x-www-form-urlencoded; charset=UTF-8';
Request.Method := 'POST';
Request.CharSet := 'utf-8';
Request.AcceptCharSet := 'utf-8';
res := Post(ServerURL, aParam);
end;
Result := res;
req.Free;
end;
-edit- 我使用的是delphi 2010
I'm posting two forms - one in c# and one in delphi. But the result string seems to be different:
c# returns: ¤@@1@@@@1@@@@1@@xśm˱Â0Đ...
delphi returns: #$1E'@@1@@@@1@@@@1@@x'#$009C...
and sice both are compressed streams I'm getting errors while trying to decompress it... The C# is 'correct' - ie. extracts. I'm not an expert on delphi - I just need to convert some piece of code from c# to delphi.
c# code:
string GetData(Hashtable aParam, string ServerURL)
{
string Result = "";
WebRequest Request = HttpWebRequest.Create(ServerURL);
Request.Method = "POST";
Request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
UTF8Encoding encUTF8 = new System.Text.UTF8Encoding(false);
StreamWriter writer = new StreamWriter(Request.GetRequestStream(), encUTF8);
foreach (DictionaryEntry element in aParam)
{
writer.Write(element.Key + "=" + element.Value + "&");
}
writer.Close();
writer.Dispose();
WebResponse Response = Request.GetResponse();
StreamReader Reader = new StreamReader(Response.GetResponseStream(), System.Text.Encoding.Default);
Result = Reader.ReadToEnd();
Reader.Close();
Response.Close();
Reader.Dispose();
return Result;
}
delphi code:
function GetData(aParam:TStringList; ServerURL:string):string;
var
req: TIdHTTP;
res: string;
begin
req := TIdHTTP.Create();
with req do
begin
Request.ContentType := 'application/x-www-form-urlencoded; charset=UTF-8';
Request.Method := 'POST';
Request.CharSet := 'utf-8';
Request.AcceptCharSet := 'utf-8';
res := Post(ServerURL, aParam);
end;
Result := res;
req.Free;
end;
-edit-
I'm using delphi 2010
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 Delphi 2010 (2009+),您应该注意用于存储信息的本机字符串类型不是 UTF8,而是 16 位基数 (USC2)。 Hance,可能在某个地方数据从 UTF8 转换为 USC2,并且您的二进制数据也被转换。
一个问题:为什么以 UTF8 格式而不是八位字节(例如原始字节)发送二进制数据?
If you use Delphi 2010 (2009+) you should niote that the native sting type that you used to store the information is not UTF8 but 16bit bases (USC2). Hance, somewhere probably the data is converted from UTF8 to USC2 and ehance your binary data is converted.
One question: why send binary data as UTF8 and not as octets (eg raw bytes)?
事实证明,将 post 方法更改为使用流的方法解决了问题。正如我在某些网站上发现的那样:“TIdHTTP.Post() 尚不支持从 TStringList 发布 Unicode。您必须先将 Unicode 保存到单独的 TStream,然后再发布它。”
It turns out that changing the post method to the one that uses streams solved the issue. As I've found on some site: "TIdHTTP.Post() does not support posting Unicode from a TStringList yet. You will have to save the Unicode to a separate TStream first and then post that instead."