dongqiya9552 2013-07-25 11:02
浏览 45

UTF-8内容Java请求和PHP响应

I send a request from a java code to php server then on server side I just echo what has received as response.
So in theory I will receive what I send. but I have problem on sending UTF-8 contents, when I send arabic characters I receive unexpected characters.
My java request code:

HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
            TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);

String requestString = "سلام";

StringEntity entity = new StringEntity(requestString, "UTF-8");
entity.setContentType("application/json");
entity.setContentEncoding("UTF-8");

HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "application/json; charset=utf-8");
httpPost.setHeader("Accept-Charset", "utf-8");

ResponseHandler<String> responseHandler = new BasicResponseHandler();
HttpClient httpClient = new DefaultHttpClient(httpParams);
String responseString=null;
try
{
    responseString = httpClient.execute(httpPost, responseHandler);
}
catch (IOException e)
{  e.printStackTrace(); }

My code on server side:

<?php
    echo file_get_contents('php://input');
?>

In this test I send string "سلام" but in response I receive "سÙاÙ".
I also tried to solve the problem with changing charset with iconv(...) method on php but I failed.
I even don't know the problem is in client or server. Has anybody a help idea?

  • 写回答

1条回答 默认 最新

  • doujiekeyan0622 2013-07-25 11:31
    关注

    Answering to my own question:
    In my case the problem was in server side. I changed the header of response of server and the problem solved:

    header('Content-Type:application/json; charset=utf-8');

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?