2013-03-07 50 views
0

我想通过手机短信发送网站发送短信。我试图通过使用下面的代码通过Java API发送短信。它没有显示任何错误,但消息没有被发送。短信不在手机上使用批量短信网站发送

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); 
} 
+0

为什么你总是打印'“消息未发送”'? – ppeterka 2013-03-07 10:17:12

+0

它只是一个消息来检查它不会抛出异常没有别的 – 2013-03-07 10:19:45

+0

然后它应该说“消息发送”,不是吗?或甚至更好的“消息发送完成” – ppeterka 2013-03-07 10:24:13

回答

1

您应该写入流后检查响应代码能告诉这是怎么回事:

而且NEVER做到这一点

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"); 
} 

(从here粘贴代码):

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

这对您的健康有害:下一个调试您的代码将使用硬重物体找到这个!

要么

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

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

MUST来完成!

+0

对异常处理和重对象的评论+1: – SimonSez 2013-03-07 10:35:53

+0

异常isjava.lang.IllegalStateException:已连接并显示响应代码200 - – 2013-03-07 11:07:25

1

我看到请求参数的URL之间的一些空间:

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

这可能是问题。

+0

没有发现他们,很好的发现! – ppeterka 2013-03-07 10:25:38