0

I am going to send sms via java. The problem is the sms gateway ask me to send in this format

http://push1.maccesssmspush.com/servlet/com.aclwireless.pushconnectivity.listen
ers.TextListener?userId=xxxxx&pass=xxxx&appid=xxxx&subappid=xxxx&msgtyp
e=1&contenttype=1&selfid=true&to=9810790590,9810549717&from=ACL&dlrre
q=true&text=This+is+a+test+msg+from+ACL&alert=

The problem how to call this from a java application is it possible or does it need special libraries? IS it using HttpURLConnection will do the job? Thank you.

A Sample code I have done below is this correct.

URL sendSms1 = new URL("http://push1.maccesssmspush.com/servlet/com.aclwireless.pushconnectivity.listen
    ers.TextListener?userId=xxxxx&pass=xxxx&appid=xxxx&subappid=xxxx&msgtyp
    e=1&contenttype=1&selfid=true&to=9810790590,9810549717&from=ACL&dlrre
    q=true&text=This+is+a+test+msg+from+ACL&alert=");

URLConnection smsConn1 =
    sendSms1.openConnection();
user837306
  • 857
  • 3
  • 18
  • 32

4 Answers4

1

It's just an HTTP call, you don't need anything special in Java (or any modern language, I expect). Just build up the string as appropriate*, then make an HTTP request to that URL.

Take a peek at the Sun tutorial Reading from and Writing to a URLConnection if you need to pick up the basics of how to do the request part in Java. This uses the built-in classes, I'm sure there are dozens of libraries that handles connections in funky and/or convenient ways too, so by all means use one of those if you're familiar with it.

*One potential gotcha which might not have occurred to you - your query string arguments will have to be URL-encoded. So the + characters for example in the text parameter, are encoded spaces (which would have a different meaning in the URL). Likewise, if you wanted to send a ? character in one of your parameters, it would have to appear as %3F. Have a look at the accepted answer to HTTP URL Address Encoding in Java for an example of how you might build the URL string safely.

Community
  • 1
  • 1
Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
0

It looks like a simple GET request, you can use Apache HttpClient libarary for executing such a request. Have a look into a tutorial by Vogella here: http://www.vogella.de/articles/ApacheHttpClient/article.html for sample source code and explanations.

Kris
  • 5,714
  • 2
  • 27
  • 47
0

You can try to use java.net.URL library。

like this

// at this before you need to generate the urlString as "http://push1.maccesssmspush.com/servlet/com.aclwireless.pushconnectivity.listen
ers.TextListener?userId=xxxxx&pass=xxxx&appid=xxxx&subappid=xxxx&msgtyp
e=1&contenttype=1&selfid=true&to=9810790590,9810549717&from=ACL&dlrre
q=true&text=This+is+a+test+msg+from+ACL&alert="

URL url = new URL(urlString);
// send sms
URLConnection urlConnection = url.openConnection();// open the url
// and you, also can get the feedback if you want
BufferedReader br = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
  • yes I need to get the delivery status how will the bufferedreader will be helpful here.As part of the url I need to send dlrreq=true so that I get the delivery status. – user837306 Feb 09 '12 at 09:11
-2
URL url = new URL("http://smscountry.com/SMSCwebservice.asp");
HttpURLConnection urlconnection = (HttpURLConnection) url.openConnection();

[Edit]

urlconnection.setRequestMethod("POST"); 
urlconnection.setRequestProperty("Content-Type","application/x-www-form-urlenc‌​oded");     
urlconnection.setDoOutput(true); 

OutputStreamWriter out = new OutputStreamWriter(urlconnection.getOutputStream());
out.write(postData); 
out.close(); 

BufferedReader in = new BufferedReader(new InputStreamReader(urlconnection.getInputStream()));

String decodedString; 

while ((decodedString = in.readLine()) != null) { 
  retval += decodedString; 
}
Andrew
  • 13,757
  • 13
  • 66
  • 84
Sunil Kumar B M
  • 2,735
  • 1
  • 24
  • 31
  • how if I need to wait for some delivery status message after calling the openConnection is there any method for that? – user837306 Feb 09 '12 at 09:06
  • I don't think so. it is just plain HTTP request which is asynchronous. You can read the output like this `urlconnection.setRequestMethod("POST"); urlconnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); urlconnection.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(urlconnection.getOutputStream()); out.write(postData); out.close(); BufferedReader in = new BufferedReader( new InputStreamReader(urlconnection.getInputStream())); String decodedString; while ((decodedString = in.readLine()) != null) { retval += decodedString; }` – Sunil Kumar B M Feb 09 '12 at 09:12
  • Please learn how to format code blocks, then edit that mess of code (in the comment) into the answer. – Andrew Thompson Feb 09 '12 at 09:25
  • @Andrew Thompson: I guess you cannot add new line in comment – Sunil Kumar B M Feb 09 '12 at 09:28
  • No, you can't, but the style of formatting you used for the code in the answer was wrong as well, the double ` simply makes the text show as fixed width font with gray BG. Indenting lines by 4 chars produces a much better look. Try re-editing the original text both ways and compare them side-by-side. – Andrew Thompson Feb 09 '12 at 09:36
  • @Andrew Thompson: you must consider valid points before voting down the answer – Sunil Kumar B M Feb 09 '12 at 12:07
  • If I had not already voted it down, I would now since you seem to have ignored my advice. You 'must' live and learn. – Andrew Thompson Feb 09 '12 at 12:20
  • you can only vote down an answer if it is not useful and not if it is properly formatted – Sunil Kumar B M Feb 09 '12 at 12:22
  • @all so which way should I proceed now to capture the delivery status? – user837306 Feb 10 '12 at 04:24