2013-03-10 82 views
4

我嘗試發送字符串「Приветмир!」如何通過UTF-8中的http發送字符串

String link = POST_URL; 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(link); 
String xml ="Привет мир"; 
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("file", xml)); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
HttpResponse response = httpclient.execute(httppost); 

而且通過PHP腳本保存:

if(!empty($_REQUEST['file'])){ 
$fp = fopen("C:\\windows\\temp\\1.xml", "w"); 
$mytext =$_POST["file"]; 
$test = fwrite($fp, $mytext); 
fclose($fp); 

,但我得到?????? ?????在我的Web服務器上,我嘗試使用utf編碼重新打開文件,但它沒有幫助。我該如何解決它。

+3

'UrlEncodedFormEntity'用途['ISO 8859-1'(http://ru.wikipedia.org/wiki/ ISO_8859-1)編碼默認不支持俄文字符。你可以在UrlEncodedFormEntity的構造函數中編碼。 '新的UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8)' – 2013-03-10 12:22:57

+0

夢幻般的謝謝:) @vmironov – Neo 2014-08-15 11:44:59

回答

13

StringEntity的字符集轉換爲UTF-8。這些線路將這樣的伎倆:

httpPost.setEntity(new StringEntity(body, HTTP.UTF_8)); 
4

這爲我工作:

HttpPost httpPost = new HttpPost("http://someurl.com"); 
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, HTTP.UTF_8)); 
相關問題