5

我尝试发送字符串“Привет мир!”

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); 

但我明白了?????????在我的网络服务器上,我尝试使用 utf 编码重新打开文件,但它没有帮助。我该如何解决。

4

2 回答 2

17

StringEntity 的字符集为 UTF-8。这些行可以解决问题:

 httpPost.setEntity(new StringEntity(body, HTTP.UTF_8));
于 2013-03-10T12:07:06.183 回答
4

这对我有用:

HttpPost httpPost = new HttpPost("http://someurl.com");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, HTTP.UTF_8));
于 2014-07-21T14:02:40.110 回答