简体   繁体   English

Java使用UTF-8发送http POST

[英]Java send http POST with UTF-8

I need to send HTTP POST with Google FCM. 我需要通过Google FCM发送HTTP POST。 With code below, it's OK to send English message but Chinese characters. 使用下面的代码,可以发送英文消息但可以发送中文字符。 I did many trials by adding UTF-8 here and there... Need help. 我通过在各处添加UTF-8进行了许多试验...需要帮助。

The payload of my message is str2 in the code below. 我的邮件的有效负载是以下代码中的str2。 The result shown in Android APP is Hello+%E6%88%91 Android APP中显示的结果为Hello +%E6%88%91

E68891 is the correct UTF-8 code, but I need it to be shown as a Chinese character. E68891是正确的UTF-8代码,但我需要将其显示为中文字符。

package tryHttpPost2;
import java.io.DataOutputStream;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;

public class TryHttpPost2 
{
    public static void main(String[] args) throws Exception {
        String url = "https://fcm.googleapis.com/fcm/send";
        URL obj = new URL(url);
        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json;x-www-form-urlencoded;charset=UTF-8");
        con.setRequestProperty("Accept-Charset", "UTF-8");
        con.setRequestProperty("Authorization", "key=...............");

        String str1 = "{\"to\":\"/topics/1\",\"notification\":{\"title\":\"";
        String str2 = URLEncoder.encode("Hello 我", "utf-8");
        String str3 = "\"}}";
        String urlParameters = str1+str2+str3;
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());

        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();
        con.getResponseCode();
    }
}

There are two problems: 有两个问题:

  1. writeBytes : as the Java documentation says: writeBytes :如Java文档所述:

    For each character, one byte is written, the low-order byte, in exactly the manner of the writeByte method . 对于每个字符,完全按照writeByte方法的方式写入一个字节,即低位字节。 The high-order eight bits of each character in the string are ignored. 字符串中每个字符的高8位被忽略。

    So this method can not write unicode strings. 因此,该方法不能编写unicode字符串。

  2. URLEncoder is intended to be used for GET requests or POST request with the content-type of application/x-www-form-urlencoded . URLEncoder旨在用于内容类型为application/x-www-form-urlencoded GET请求或POST请求。 But you transmit data using the content-type application/json . 但是您使用content-type application/json传输数据。 You somehow try to also use url encoding there, but this does not work. 您以某种方式尝试在那里也使用url编码,但这不起作用。 (See the relevant RFC for more information) 有关更多信息,请参见相关的RFC

To fix this, use the right method to transfer the data: As utf-8, without any encoding in the JSON: 要解决此问题,请使用正确的方法传输数据:作为utf-8,JSON中没有任何编码:

con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setRequestProperty("Accept-Charset", "UTF-8");
con.setRequestProperty("Authorization", "key=...............");

String str1 = "{\"to\":\"/topics/1\",\"notification\":\"title\":\"";
String str2 = "Hello 我";
String str3 = "\"}}";
String urlParameters = str1+str2+str3;
con.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream(), "UTF-8");

wr.write(urlParameters);
wr.flush();
wr.close();
con.getResponseCode();

Thanks for Kuhn's great help. 感谢Kuhn的大力帮助。 Here is what I do now and works. 这是我现在要做的工作。

  1. Let Content-Type as just "application/json". 让Content-Type只是“ application / json”。
  2. Let str2 be just the payload string. 令str2只是有效​​负载字符串。
  3. Replace writeBytes thing by wr.write(urlParameters.getBytes("utf-8")); 用wr.write(urlParameters.getBytes(“ utf-8”))替换writeBytes事物;
    1. The property "Accept-Charset" seems useless here. 属性“ Accept-Charset”在这里似乎没有用。 Works with or without it. 不管有没有它都可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM