Vous êtes sur la page 1sur 4

// The sendsms class can be used to send an SMS message via NowSMS.

// // For additional information, please see http://blog.nowsms.com/2009/02/nowsmsjava-example-send-sms-message.html // // This class supports most of the URL parameters that are defined for NowSMS at the following link // (plus additional parameters that have been added since that in newer releases of NowSMS): // // http://www.nowsms.com/documentation/ProductDocumentation/sending_messages/url _parameters_for_sending_messages.htm // // URL parameters are supported as methods for the sendsms class, with method na mes in all lower case. // // The init method initialises the SMS message object. // // The server method sets the URL address for the NowSMS server. // // The user and pass methods set the name of the user account ("SMS Users") on t he NowSMS server. // // The phonenumber method sets the recipient phone number (can be a comma delimi ted list or distribution list name). // // The text method sets the text of the SMS message. // // The send method submits the SMS message to NowSMS. // // The send method returns a list of message ids assigned for the submitted mess ages, with one message per line, in the following format: // // MessageID=xxxxxxxxxxxxxx.req, Recipient=xxxxxxxxxxx // // // Example - Sending a simple text message: // // sendsms.init(); // sendsms.server = "http://127.0.0.1:8800/"; // sendsms.user = "test"; // sendsms.password = "test"; // sendsms.phonenumber = "+9999999999"; // sendsms.text = "This is a test message"; // sendsms.send(); // // Example - Sending a text message to a specific application port for a Java ap plet running on the phone: // // sendsms.init(); // sendsms.server = "http://127.0.0.1:8800/"; // sendsms.user = "test"; // sendsms.password = "test"; // sendsms.phonenumber = "+9999999999"; // sendsms.text = "This is a test message"; // sendsms.destport = "9999"; // sendsms.send(); // // Example - Sending a WAP Push Message: //

// // // // // // // // //

sendsms.init(); sendsms.server = "http://127.0.0.1:8800/"; sendsms.user = "test"; sendsms.password = "test"; sendsms.phonenumber = "+9999999999"; sendsms.text = "This is a test message"; sendsms.wapurl = "http://www.nowsms.com/"; sendsms.send();

import java.net.*; import java.io.*; class sendsms { public public public public public public public public public public public public public public public public public public public public static static static static static static static static static static static static static static static static static static static static String String String String String String String String String String String String String String String String String String String String server; user; password; phonenumber; text; data; udh; pid; dcs; sender; validity; servicetype; smscroute; receiptrequested; sourceport; destport; delayuntil; voicemail; wapurl; wapsl;

public static String url_str; public static void init () { server = null; user = null; password = null; phonenumber = null; text = null; data = null; udh = null; pid = null; dcs = null; sender = null; validity = null; servicetype = null; smscroute = null; receiptrequested = null; sourceport = null; destport = null; delayuntil = null; voicemail = null; wapurl = null; wapsl = null;

} public static void setvar (String argname, String argvalue) { if (argname != null) { if (argvalue != null) { url_str = url_str + "&" + argname + "="; try { String encoded = URLEncoder.encode (argvalue, "UTF-8"); url_str = url_str + encoded; } catch (UnsupportedEncodingException e) { url_str = url_str + argvalue; } } } } public static String send () { String returnstring; returnstring = null; if (server == null) { System.out.println("sendsms.server value not set"); return returnstring; } url_str = server + "?"; setvar("user", user); setvar("password", password); setvar("phonenumber", phonenumber); setvar("text", text); setvar("data", data); setvar("udh", udh); setvar("pid", pid); setvar("dcs", dcs); setvar("sender", sender); setvar("validity", validity); setvar("servicetype", servicetype); setvar("smscroute", smscroute); setvar("receiptrequested", receiptrequested); setvar("sourceport", sourceport); setvar("destport", destport); setvar("delayuntil", delayuntil); setvar("voicemail", voicemail); setvar("wapurl", wapurl); setvar("wapsl", wapsl); try { URL url2=new URL(url_str); HttpURLConnection connection = (HttpURLConnection) url2.openConnection (); connection.setDoOutput(false); connection.setDoInput(true);

String res=connection.getResponseMessage(); System.out.println("Response Code ->"+res); int code = connection.getResponseCode () ; if ( code == HttpURLConnection.HTTP_OK ) { //Get response data. BufferedReader in = new BufferedReader(new InputStreamReader(connec tion.getInputStream())); String str; while( null != ((str = in.readLine()))) { if (str.startsWith("MessageID=")) { returnstring = returnstring + str + "\r\n"; System.out.println(str); } } connection.disconnect() ; } } catch(IOException e) { System.out.println("unable to create new url"+e.getMessage()); } return returnstring; } }

Vous aimerez peut-être aussi