Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send string by http post in UTF-8

Tags:

android

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

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

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

like image 527
user1884872 Avatar asked Mar 10 '13 12:03

user1884872


2 Answers

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

 httpPost.setEntity(new StringEntity(body, HTTP.UTF_8));
like image 144
DjHacktorReborn Avatar answered Oct 11 '22 14:10

DjHacktorReborn


This worked for me:

HttpPost httpPost = new HttpPost("http://someurl.com");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, HTTP.UTF_8));
like image 32
ymerdrengene Avatar answered Oct 11 '22 14:10

ymerdrengene