我正在尝试通过批量短信发送网站在移动设备上发送短信。我试图通过使用以下代码通过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 :(得分:1)
我在请求参数URL之间看到了一些空格:
String urlParameters="usr=username&pwd=1234&ph=9015569447&text=Hello";
这可能是个问题。
答案 1 :(得分:1)
您应该在写入流后检查响应代码,以便能够分辨出正在发生的事情:
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);
}
必须完成!!!