Example api code for Transactional 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.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";
String gwid = "2";

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

URL url = new URL(apiUrl + "?" + urlParams);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder response = new StringBuilder();
String line;

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

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

 

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

import java.io.BufferedReader;
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";
String gwid = "2";

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

URL url = new URL(apiUrl + "?" + urlParams);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder response = new StringBuilder();
String line;

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

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

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Example api code for Transactional SMS in asp.net

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

Example api code for Transactional SMS in php

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

Example api code for Transactional SMS in ruby

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

Example api code for Transactional SMS in node.js

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

Example api code for Transactional SMS in python

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