Example api code for Promotional SMS in java

Replace user, password, msisdn, sid and message accordingly.

 

1. Below is the sample code for java for Single Message:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class Main {
public static void main(String[] args) {
String user = "abc";
String password = "xyz";
String msisdn = "919898xxxxxx";
String sid = "SenderId";
String message = "test message";
String fl = "0";

try {
String apiUrl = "http://sms.domainadda.com/vendorsms/pushsms.aspx?user="
+ URLEncoder.encode(user, "UTF-8")
+ "&password=" + URLEncoder.encode(password, "UTF-8")
+ "&msisdn=" + URLEncoder.encode(msisdn, "UTF-8")
+ "&sid=" + URLEncoder.encode(sid, "UTF-8")
+ "&msg=" + URLEncoder.encode(message, "UTF-8")
+ "&fl=" + URLEncoder.encode(fl, "UTF-8");

URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;

while ((line = reader.readLine()) != null) {
response.append(line);
}

reader.close();
connection.disconnect();

System.out.println("API response: " + response.toString());
} catch (IOException e) {
System.err.println("Error sending request: " + e.getMessage());
}
}
}

 

2. Below is the sample code for java for Multiple Messages:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class Main {
public static void main(String[] args) {
String user = "abc";
String password = "xyz";
String msisdns = "919898xxxxxx,919898xxxxxx"; // Comma-separated list of phone numbers
String sid = "SenderId";
String message = "test message";
String fl = "0";

try {
String apiUrl = "http://sms.domainadda.com/vendorsms/pushsms.aspx";
String urlParameters = "user=" + URLEncoder.encode(user, "UTF-8") +
"&password=" + URLEncoder.encode(password, "UTF-8") +
"&msisdn=" + URLEncoder.encode(msisdns, "UTF-8") +
"&sid=" + URLEncoder.encode(sid, "UTF-8") +
"&msg=" + URLEncoder.encode(message, "UTF-8") +
"&fl=" + URLEncoder.encode(fl, "UTF-8");

URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", String.valueOf(urlParameters.getBytes().length));
connection.setDoOutput(true);

DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;

while ((line = reader.readLine()) != null) {
response.append(line);
}

reader.close();
connection.disconnect();

System.out.println("API response: " + response.toString());
} catch (IOException e) {
System.err.println("Error sending request: " + e.getMessage());
}
}
}

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Example api code for Promotional SMS in php

Replace user, password, msisdn, sid and message accordingly.   1. Below is the sample code for...

Example api code for Promotional SMS in asp.net

Replace user, password, msisdn, sid and message accordingly.   1. Below is the sample code for...

Example api code for Promotional SMS in ruby

Replace user, password, msisdn, sid and message accordingly.   1. Below is the sample code for...

Example api code for Promotional SMS in node,js

Replace user, password, msisdn, sid and message accordingly.   1. Below is the sample code for...

Example api code for Promotional SMS in python

Replace user, password, msisdn, sid and message accordingly.   1. Below is the sample code for...