【发布时间】:2011-03-03 22:17:49
【问题描述】:
我发布了两种形式 - 一种在 c# 中,另一种在 delphi 中。但结果字符串似乎有所不同:
c# 返回:¤@@1@@@@1@@@@1@@xśm˱Â0Đ...
德尔福返回:#$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;
}
德尔福代码:
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;
-编辑- 我正在使用delphi 2010
【问题讨论】:
-
哪个版本正确,可以解压?使用诸如 Fiddler2 之类的工具来监控实际发生的 HTTP 流量可能会很好。这样您就可以看到请求的不同之处。
-
c#版本是对的,刚刚想到fiddler...
-
我想我找到了原因:
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. -
您能否提供一个适用于您的 C# 版本代码的 URL?
-
我猜你知道#指的是一个字符,而#$指的是Delphi中的unicode代码点? 'A'#66'C' == 'ABC' 和 #$0040 == '@'
标签: c# delphi post delphi-2010 utf