0

I am trying to send the sms on mobile through the bulk sms sending site . I am trying to send the sms through java api by using the following code. its not showing any error but message is not being sent.

 String urlParameters="usr=username &pwd=1234 &ph=9015569447 &text=Hello";
 //String request = "http://hapi.smsapi.org/SendSMS.aspx?";
 String request="http://WWW.BULKSMS.FELIXINDIA.COM/send.php?";
try{                        
URL url = new URL(request); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection();           
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false); 
connection.setRequestMethod("POST"); 


connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" +         Integer.toString(urlParameters.getBytes().length));
  connection.setUseCaches (false);

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

    wr.flush();
wr.close();
connection.disconnect();
}
catch(Exception ex)
{
 System.out.print(ex);
}
adesh singh
  • 1,727
  • 9
  • 38
  • 70

2 Answers2

1

I see some space between request parameters URL :

 String urlParameters="usr=username&pwd=1234&ph=9015569447&text=Hello";

This might be the issue.

Jeevan Patil
  • 6,029
  • 3
  • 33
  • 50
1

You should check the response code after writing to the stream to be able to tell what's going on:

int rc = connection.getResponseCode();
if(rc==200)
{
    //no http response code error
    //read the result from the server
    rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    sb = new StringBuilder();
    //get the returned data too
    returnString=sb.toString();
}
else
{
    System.out.println("http response code error: "+rc+"\n");
}

(Code pasted from here)

Also NEVER do this:

catch(Exception ex)
{
    System.out.print(ex);
}

This is bad for your health: the next one debugging your code will slap you with a hard and heavy object finding this!

Either

catch(Exception ex)
{
    ex.printStackTrace();
}

or

catch(Exception ex)
{
    LOG.error("Something went wrong (adequate error message here please)", ex);
}

MUST be done!!!

ppeterka
  • 20,583
  • 6
  • 63
  • 78