I am trying to send an sms from my mobile number to another number using a java program, I am not allowed to use any third party gateways . My code as follows:
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.net.ssl.HttpsURLConnection;
class SendSMS
{
public static void main(String args[])
{
boolean debug=true;
String phone ="10 digit number";
String message ="hi";
String ppgHost ="http://localhost:8800/";
String username ="";
String password ="";
try
{
phone=URLEncoder.encode("8366xxx", "UTF-8");
if(debug)
System.out.println("phone------>"+phone);
message=URLEncoder.encode("SendMsg via Now.SMS", "UTF-8");
if(debug)
System.out.println("message---->"+message);
}
catch (UnsupportedEncodingException e)
{
System.out.println("Encoding not supported");
}
String url_str=ppgHost+"?PhoneNumber="+phone+"&Text="+message;
//String url_str=ppgHost+"?user="+username+"&password="+password+"&PhoneNumber="+phone+"&Text="+message;
if(debug)
System.out.println("url string->"+url_str);
try
{
URL url2=new URL(url_str);
HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
connection.setDoOutput(false);
connection.setDoInput(true);
if(debug)
System.out.println("Opened Con->"+connection);
String res=connection.getResponseMessage();
if(debug)
System.out.println("Get Resp ->"+res);
int code = connection.getResponseCode () ;
if ( code == HttpURLConnection.HTTP_OK )
{
connection.disconnect() ;
}
}
catch(IOException e)
{
System.out.println("unable to create new url"+e.getMessage());
}
}
}
Please guide me guys. I connected my mobile using data cable into my system and tried to send but I am getting the error as follows:
C:\SMS>java SendSMS
phone------>8366xxx
message---->SendMsg+via+Now.SMS
url string->http://localhost:8800/?PhoneNumber=8366xxx&Text=SendMsg+via+Now.SMS
Opened Con->sun.net.www.protocol.http.HttpURLConnection:http://localhost:8800/?P
honeNumber=8366xxx&Text=SendMsg+via+Now.SMS
unable to create new urlConnection refused: connect
Please help guys
- I do not want to use any third party gateway
- The amount of sms will be used from my mobile balance
What I doing wrong here? I am new to this. Almost all the examples are given are using third party libraries and gateways. Help guys!