发生 HttpsURLConnection 400 Bad Request 错误

Posted

技术标签:

【中文标题】发生 HttpsURLConnection 400 Bad Request 错误【英文标题】:HttpsURLConnection 400 Bad Request error occured 【发布时间】:2018-11-21 00:27:21 【问题描述】:

我需要将以下 Swift 代码转换为 Java。 Swift 代码有效。 但是,Java 代码不起作用。

HttpsURLConnection 失败。

responseCode: 400 (HttpsURLConnection.getResponseCode())

message2:错误请求 (HttpsURLConnection.getResponseMessage())

如何解决 400 Bad Request 错误。

在 Swift 中(有效)

func sendNotification(token: String, message: String) 

 //Firebase CloudMessaging serverkey
    var firebaseServerKey = "AAAAA6qLps4:APA91bE7szGAgp3qYGOJsrSsrM1InhIgf5Fq1xxxxxx"

    let url = URL(string: "https://fcm.googleapis.com/fcm/send")!
    var request = URLRequest(url: url)
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")

    request.setValue("key=\(firebaseServerKey)", forHTTPHeaderField: "Authorization")
    request.httpMethod = "POST"

    request.httpBody = "\"to\":\"\(token)\",\"notification\":\"body\":\"\(message)\",\"badge\":\"1\"".data(using: .utf8)


我用JAVA写的(没用)

private void sendNotification(String token, String message) 
//Firebase CloudMessaging serverkey
    var firebaseServerKey = "AAAAA6qLps4:APA91bE7szGAgp3qYGOJsrSsrM1InhIgf5Fq1xxxxxx"

    try 
        URL url = new URL("https://fcm.googleapis.com/fcm/send");
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Authorization", "key="+firebaseServerKey);


        String str = "\"to\": " + token + ", \"notification\":  \"body\": " + message + ", \"badge\": \"1\"";


        byte[] outputInBytes = str.getBytes("UTF-8");
        OutputStream os = conn.getOutputStream();
        os.write(outputInBytes);
        os.close();

        int responseCode = conn.getResponseCode(); // responseCode: 400

        if (responseCode == HttpsURLConnection.HTTP_OK) 
            Log.d("Success", String.valueOf(responseCode));
         else 
            String code = String.valueOf(responseCode);
            String message2 = conn.getResponseMessage(); // message2: Bad Request
            Log.d("Fail", String.valueOf(responseCode));
            Log.d("Fail2", conn.getResponseMessage());
        

【问题讨论】:

【参考方案1】:

您的 JSON 请求正文不正确。它产生 "to": zxc 这是一个无效的 json。改成

String str = "\"to\": \"" + token + "\", \"notification\":  \"body\": \"" +
    message + "\", \"badge\": \"1\"";

【讨论】:

嗨,谢谢。我无法用你的代码建立成功。我对 Json 的东西不熟悉。我应该如何改变它? 嗨,我仍然没有像下图那样构建失败。 我更新了它。我检查了我的 IDE。再次放错地方对不起

以上是关于发生 HttpsURLConnection 400 Bad Request 错误的主要内容,如果未能解决你的问题,请参考以下文章

HttpsURLConnection 忽略 setSSLSocketFactory

Java HttpsURLConnection SSLHandshakeException

HttpsURLConnection HttpURLConnection下载图片

HttpsURLConnection 身份验证器错误

如何让 Jersey 客户端使用 HttpsURLConnection 中设置的 defaultSSLSocketFactory?

HttpClient 抛出 ssl 错误,但 webview 和 HttpsURLConnection 不会