2017-06-04 58 views
2

當我發送一個字符串到PHP頁面時,它工作正常。如何使用TIdHTTP向PHP服務器發佈UTF-8編碼請求?

問題是當我使用像「محمد」這樣的阿拉伯語,我得到了像「????」這樣的結果。這裏是我的Delphi代碼:

var 
    s: string; 
    server: TIdHttp; 
begin 
    s := 'محمد'; 
    server := TIdHttp.Create; 
    server.Post('http://mywebsite.com/insert_studint.php?name1=' + s); 
    server.Free; 
end; 

我的PHP代碼:

<?php 
    $name1=$_post['name1']; 
    echo $name1; 
?> 

我怎麼編碼我的請求,UTF-8,讓我的PHP服務器上的一個正確的結果?

+0

https://stackoverflow.com/questions/44214787/get-utf8-string-from-php-page-in-c-sharp-windows-application –

回答

2

這不是由TIdHTTP類處理URL字符串查詢參數的方式。 Post方法的第一個參數就是目標URL。 URL字符串查詢參數需要作爲流或字符串列表集合作爲此方法的第二個參數傳遞。您需要在代碼中指定請求編碼,其餘的將在內部處理該類。試試這個:

var 
    Server: TIdHTTP; 
    Params: TStrings; 
begin 
    Server := TIdHTTP.Create; 
    try 
    { setup the request charset } 
    Server.Request.Charset := 'utf-8'; 
    { create the name=value parameter collection } 
    Params := TStringList.Create; 
    try 
     { add the name1 parameter (concatenated with its value) } 
     Params.Add('name1=محمد'); 
     { do the request } 
     Server.Post('http://mywebsite.com/insert_studint.php', Params); 
    finally 
     Params.Free; 
    end; 
    finally 
    Server.Free; 
    end; 
end; 
+0

也是他最可能需要的HTTP GET not HTTP POST –

+1

@Arioch,他們的代碼中的[$ _POST](http://php.net/manual/en/reserved.variables.post.php)變量似乎沒有這麼說。 – Victoria

+0

感謝您的回答。但現在我有其他問題 當我嘗試你的代碼沒有數據發出我的PHP。 –

相關問題