【问题标题】:Cannot Send Whatsapp Message using Java无法使用 Java 发送 Whatsapp 消息
【发布时间】:2021-12-10 14:37:51
【问题描述】:

我需要你的帮助来解决这个问题,我的方法是想使用 Java 发送 Whatsapp 消息并使用 Whatsapp 网关 请在下面找到java代码

import java.net.*;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class JsonExample1 {
        // Your Business ID Gateway
        private static final String ID_CLIENT = "ID Client xxx";
        // Your token Gateway
        private static final String CLIENT_SECRET = "Client";
        // Gateway URL
        private static final String URL_GATEWAY = "URL Whatsapp Gateway"; 
        //Main method
        public static void main(String[] args) throws Exception {
            // Whatsapp number
            String number = "81275704515";  
            // Whatsapp message
            String message = "Test WhatsApp message"; 
            sendwhatsapp(number, message);
        }

    public static void sendwhatsapp(String number, String message) {

        String payload = "{ Number: " + number + " | Message: " + message;

        try {
            
            URL url = new URL(URL_GATEWAY);
            HttpURLConnection conex = (HttpURLConnection) url.openConnection();
            Post(conex);
            OutputStream output = conex.getOutputStream();
            output.write(payload.getBytes());
            output.flush();
            output.close();

            int StateCode = conex.getResponseCode();
            System.out.println("Request Gateway: \n");
            System.out.println("State Code: " + StateCode);
            InputStreamReader Writing = null;
            BufferedReader br = new BufferedReader(Writing);
            String outputS;
            while ((outputS = br.readLine()) != null) {
                System.out.println(outputS);
            }
            conex.disconnect();
        } catch (Exception e) {
            System.out.println("Could not send the message");
        }
    }
    public static void Post(HttpURLConnection conex) throws ProtocolException {
        conex.setDoOutput(true);
        conex.setRequestMethod("POST");
        conex.setRequestProperty("ID", ID_CLIENT);
        conex.setRequestProperty("CLIENT_SECRET", CLIENT_SECRET);
        conex.setRequestProperty("Content-Type", "application/json");
    } 
}

我总是得到这样的回应: 请求网关:

州代码:500 无法发送消息

请您帮忙。谢谢

【问题讨论】:

  • Writing 为空时,你知道你有new BufferedReader(Writing); 吗?
  • 是的@c0der,我知道这一点,感谢您注意到我,但假设我正在删除它,我得到的回应是什么,我的意思是什么都没发生。请帮忙。谢谢

标签: java arrays json whatsapp hardcode


【解决方案1】:

我已经找到了解决方案, 这是正确的答案:

public class SendWhatsApp{
    public static void main(String[] args) throws IOException {
        //Method for Whatsapp sending message
        //URL Whatsapp API Gateway
        URL url = new URL("https://Your Whatsapp API Gateway");
        HttpURLConnection http = (HttpURLConnection)url.openConnection();
        http.setRequestMethod("POST");
        http.setDoOutput(true);
        http.setRequestProperty("Accept", "application/json");
        //Property for token/authorization
        http.setRequestProperty("Authorization", "Bearer (Input Your Token Here) ");
        http.setRequestProperty("Content-Type", "application/json");
        //still hardcode :(
        String data = "{\n    \"token\": \"\",\n    \"to\": [\"\"],\n    \"param\": [\"\"]\n}";
        
        byte[] out = data.getBytes(StandardCharsets.UTF_8);

        OutputStream stream = http.getOutputStream();
        stream.write(out);

        System.out.println(http.getResponseCode() + " " + http.getResponseMessage());
        http.disconnect();

    }
}

【讨论】:

    猜你喜欢
    • 2013-10-08
    • 2017-02-04
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 2019-11-13
    • 2018-11-26
    相关资源
    最近更新 更多