【问题标题】:how to send string by http post in UTF-8如何在 UTF-8 中通过 http post 发送字符串
【发布时间】:2013-03-10 12:04:46
【问题描述】:

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

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 编码重新打开文件,但它没有帮助。我该如何解决它。

【问题讨论】:

  • UrlEncodedFormEntity 默认使用不支持俄语字符的ISO 8859-1 编码。您可以在UrlEncodedFormEntity 的构造函数中指定编码。 new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)
  • 非常感谢 :) @vmironov

标签: android


【解决方案1】:

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

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

【讨论】:

  • 它已经被弃用了。请改用StandardCharsets.UTF_8
【解决方案2】:

这对我有用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 2013-05-13
    • 2015-01-28
    • 2022-07-14
    • 1970-01-01
    相关资源
    最近更新 更多