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
I have found the solution for this, Here's the proper answer :
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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.