我正试图通过批量短信发送站点在移动设备上发送短信。我试图通过使用以下代码通过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);
}
您应该在写入流之后检查响应代码,以便能够判断发生了什么:
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");
}
(从这里粘贴的代码)
同样永远不要这样做:
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);
}
必须完成!
我看到请求参数之间有一些空格URL:
String urlParameters="usr=username&pwd=1234&ph=9015569447&text=Hello";
这可能是问题所在