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

2 回答 2

1

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

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

这可能是问题所在。

于 2013-03-07T10:21:57.610 回答
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");
}

(从这里粘贴的代码)

永远不要这样做:

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

必须完成!!!

于 2013-03-07T10:22:50.350 回答