2013-03-07 55 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