我尝试通过 INFOBIP 向我的手机发送短信时遇到问题

问题描述 投票:0回答:1

我在发送短信时遇到问题,我收到 200 http 状态,但我的手机未收到消息。

这是我的代码

        try {
            // Create the connection to the Infobip API.
            URL url = new URL(BASE_URL + "/sms/2/text/advanced");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Authorization", "App " + API_KEY);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Accept", "application/json");
//            connection.setRequestProperty("User-Agent", "Mozilla/4.76");
            connection.setDoOutput(true);

            // Create the SMS message JSON payload.
            String payload = String.format("{\"messages\":[{\"from\":\"%s\",\"destinations\":[{\"to\":\"%s\"}],\"text\":\"%s\"}]}",
                    SENDER,
                    RECIPIENT,
                    MESSAGE_TEXT
            );
            // Write the payload to the request.
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = payload.getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            // Get the response from the API.
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK) {
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
                    String line;
                    StringBuilder response = new StringBuilder();
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
                    System.out.println("Message sent successfully. Response Content: " + response.toString());
                }
            } else {
                // Print the response content for error requests.
                // Similar to the success case, adjust the parsing and print the full response content.
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) {
                    String line;
                    StringBuilder response = new StringBuilder();
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
                    System.out.println("Error sending message. Response Content: " + response.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

响应代码:200

这是回复:

消息发送成功。响应内容: {"messages":[{"messageId":"3915723960824335774697","status":{"description":"消息发送到下一个实例","groupId":1,"groupName":"PENDING"," id":26,"名称":"PENDING_ACCEPTED"},"收件人":"970597086266"}]}

此响应应如下返回:

 groupId: 3
        groupName: DELIVERED
        id: 5
        name: DELIVERED_TO_HANDSET
        description: Message delivered to handset
        action: null
java sms infobip infobip-api
1个回答
0
投票

已经过去了一段时间,我在这里发表评论是为了帮助其他人不要在这方面浪费太多时间。 我遇到了类似的问题,在寻找一些线索时我发现了你的问题。

我在调用 API 时得到了相同的响应。

{
  "messages": [
    {
      "messageId": "SOME_MESSAGE_ID",
      "status": {
        "description": "Message sent to next instance",
        "groupId": 1,
        "groupName": "PENDING",
        "id": 26,
        "name": "PENDING_ACCEPTED"
      },
      "to": "SOME_PHONE_NUMBER"
    }
  ]
}

如果您尝试再次通过 API 获取有关该 messageID 的一些信息 ( https://epqd3.api.infobip.com/sms/2/logs?messageId=THE_MESSAGE_ID ),您将得到一个空响应,例如:

{
    "results": []
}

如果您尝试检查 Infobip 中的仪表板,则没有此类请求/消息。

如果您尝试通过 Postman 发出请求(短信正文为纯文本),一切都很好,您可以在仪表板中看到该消息。

[问题原因]

原来问题是短信内容不是纯文本!它更像是渲染的 HTML 编码字符串。

[解决方案]

对我来说,只是确保短信正文是纯文本。这解决了我的问题。

© www.soinside.com 2019 - 2024. All rights reserved.