I need your help to solve this question, My approach is want to send Whatsapp message using Java and use Whatsapp Gateway please find the java code below
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");
}
}
I always get response like this: Request Gateway:
State Code: 500 Could not send the message
Kindly need your help. Thank you