2010-06-15 30 views
4

我發佈了兩種形式 - 一種在c#中,另一種在delphi中。但結果字符串似乎是不同的:delphi vs c#post返回不同的字符串 - utf問題?

C#的回報:¤@@[email protected]@@@[email protected]@@@[email protected]@xśm˱Â0Đ...
德爾福回報:#$1E'@@[email protected]@@@[email protected]@@@[email protected]@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; 

CNC中 我使用德爾福2010

+0

哪個版本是正確的,可以解壓?使用Fiddler2等工具來監控實際正在進行的HTTP流量可能會很好。這將允許您查看請求中的差異。 – 2010-06-15 10:46:59

+0

c#版本是正確的,我只想着提琴手... – argh 2010-06-15 11:02:11

+1

我想我找到了原因: 'TIdHTTP.Post()不支持從TStringList中發佈Unicode。您必須首先將Unicode保存到單獨的TStream中,然後將其發佈。# – argh 2010-06-15 11:35:22

回答

6

事實證明,改變後的方法到使用流的人解決了這個問題。正如我在某個站點上發現的:「TIdHTTP.Post()不支持從TStringList中發佈Unicode,您必須先將Unicode保存到單獨的TStream中,然後再發布。」

function GetData(aParam:TStringList; aurl:string):string; 
var 
    req: TIdHTTP; 
    i: integer; 
    vars: string; 
    reqStream, responseStream :TStringStream; 
begin 

    vars := ''; 

    for i := 0 to aParam.Count - 1 do 
    begin 
     vars := vars + aParam.Names[i] + '=' + aparam.ValueFromIndex[i] + '&'; 
    end; 

    reqStream := TStringStream.Create(vars); 
    responseStream := TStringStream.Create; 

    req := TIdHTTP.Create(); 
    with req do 
    begin 
     Request.URL := aurl; 
     Request.ContentType := 'application/x-www-form-urlencoded'; 
     Request.Method := 'POST'; 
     Request.CharSet := 'UTF-8'; 
     Request.AcceptCharSet := 'UTF-8'; 
    end; 

    req.Post(aurl, reqStream,responseStream); 

    Result := responseStream.DataString; 

    reqStream.Free; 
    responseStream.Free; 
    req.Free; 
end; 
1

如果使用德爾福2010年(2009+),你應該niote是您用來存儲信息的本地刺痛類型不是UTF8,但16位的鹼基(USC2)。 Hance,可能有些地方的數據會從UTF8轉換爲USC2,並且您的二進制數據會被轉換。

一個問題:爲什麼發送二進制數據爲UTF8而不是八位位組(例如原始字節)?

+0

我只是翻譯代碼我被給了 - 不要試圖理解它(我放棄了)。 – argh 2010-06-15 22:29:53