如何使用HttpPost发送日文字符

 wxxc 发布于 2023-01-30 14:32

我正在尝试将日文字符发送到我的API服务器,但发送的字符是乱码并且变成了????.所以我使用以下方法将编码设置为实体:

    StringEntity stringEntity = new StringEntity(message, "UTF-8");

但输出成了org.apache.http.entity.StringEntity@4316f850.我想知道是否转换stringEntity为字符串导致了这个,因为我想在我的服务器中发送它String.

这是我如何使用它:

public static String postSendMessage(String path, String message) throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 10000); // Timeout limit
    HttpPost httpPost = new HttpPost(SystemInfo.getApiUrl() + path);
    List value = new ArrayList();

    StringEntity stringEntity = new StringEntity(message, "UTF-8");
    value.add(new BasicNameValuePair("message", stringEntity.toString())); //Here's where I converted the stringEntity to string

    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value);
    httpPost.setEntity(entity);
    HttpResponse httpResponse = httpClient.execute(httpPost);

    HttpEntity httpEntity = httpResponse.getEntity();
    InputStream is = httpEntity.getContent();

    String result = convertStreamToString(is);
    return result;
}

哪里可能出错了?

1 个回答
  • 你不需要使用StringEntity.

    List<NameValuePair> value = new ArrayList<NameValuePair>();
    value.add(new BasicNameValuePair("message", message));
    

    相反,你必须传递第二个参数来初始化`UrlEncodedFormEntity.

    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value, "UTF-8");
    

    2023-01-30 14:33 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有