如何通过UTF-8中的http post发送字符串 [英] how to send string by http post in UTF-8

查看:312
本文介绍了如何通过UTF-8中的http post发送字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to send string "Привет мир!"

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脚本将其保存:

And save it by php script:

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

但是我得到了?????? ??????在我的Web服务器上,我尝试使用utf编码重新打开文件,但这无济于事.我该怎么解决.

But I get ?????? ????? on my web server, I try reopen file with utf encoding, but it doesn't help. How can I resolve it.

推荐答案

StringEntity的字符集为UTF-8.这些行将达到目的:

The StringEntity's charset to UTF-8. These lines will do the trick:

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

这篇关于如何通过UTF-8中的http post发送字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆