Java 未使用批量短信站点在手机上发送短信
我正在尝试通过批量短信发送站点发送手机短信。我正在尝试使用以下代码通过JavaAPI发送sms。它没有显示任何错误,但没有发送消息Java 未使用批量短信站点在手机上发送短信,java,http-headers,connection,sms-gateway,Java,Http Headers,Connection,Sms Gateway,我正在尝试通过批量短信发送站点发送手机短信。我正在尝试使用以下代码通过JavaAPI发送sms。它没有显示任何错误,但没有发送消息 String urlParameters="usr=username &pwd=1234 &ph=9015569447 &text=Hello"; //String request = "http://hapi.smsapi.org/SendSMS.aspx?"; String request="http://WWW.BULKSMS.F
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);
}
我看到请求参数URL之间有一些空格:
String urlParameters="usr=username&pwd=1234&ph=9015569447&text=Hello";
这可能就是问题所在。您应该在写入流后检查响应代码,以便能够知道发生了什么:
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();
}
或
必须完成 为什么你总是打印“未发送的消息”?这只是一条检查其未抛出异常的消息,其他什么都没有。它应该说“已发送的消息”,不是吗?或者更好的是,“消息发送完成”connection.getResponseCode()和connection.getResponseMessage()在说什么?你有没有尝试过用另一种方式发送消息,比如浏览器?它说什么错误消息,我猜印度的网站禁止发送批量消息,这与此有关吗,看一看,没有发现,发现得不错+1有关异常处理和重载对象的注释:)exception isjava.lang.IllegalStateException:已连接并显示响应代码200–
catch(Exception ex)
{
LOG.error("Something went wrong (adequate error message here please)", ex);
}