【问题标题】:java.io.IOException: Server returned HTTP response code: 400 for URL? why am getting this exception [duplicate]java.io.IOException:服务器返回 HTTP 响应代码:URL 为 400?为什么会出现此异常[重复]
【发布时间】:2017-07-09 00:48:35
【问题描述】:

我有一种向群发成员发送短信的方法 这是我的java方法

public void sendSMS(String mobilno,String msg)
{
    URLConnection myURLConnection=null;
    URL myURL=null;
    BufferedReader reader=null;
    String mainUrl="http://smsstreet.in/websms/sendsms.aspx?userid=id&password=pwd&sender=sender&mobileno="+mobilno+"&msg="+msg;
    StringBuilder sbPostData= new StringBuilder(mainUrl);
    mainUrl = sbPostData.toString();  
    try
    {

        myURL = new URL(mainUrl);
        myURLConnection = myURL.openConnection();
        myURLConnection.connect();
        reader= new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
        String response;
        while ((response = reader.readLine()) != null)
        System.out.println(response);
        reader.close();
    }  
    catch (IOException e)
    {                                
            e.printStackTrace();
    }
}

这是我的服务:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    // TODO Auto-generated method stub
    String mobilnmbers=request.getParameter("mobName");
    System.out.println(mobilnmbers);
    String message=request.getParameter("comment");
    System.out.println(message);  
    SMSMethod sms=new SMSMethod();
    sms.sendSMS(mobilnmbers, message);
}

当我将它调用到 servlet 时,它会给出以下异常

java.io.IOException: Server returned HTTP response code: 400 for URL: http://smsstreet.in/websms/sendsms.aspx?userid=id&password=pwd&sender=sender&mobileno=9014780300,8309518663,9920221173,9849133273,9849656287,7013083104,8759197399,1234567890,8096613756,7075472796,8121104305,9182176200,9949327590,8309490418,9949913331,8125206381,9550371787,9502167674,9912193234,9912193234,9700237487,9246563935&msg=Have you started booking the train tickets

我是这个 smsGatway 的新手,谁能帮助我?

【问题讨论】:

  • 解决办法是什么?
  • 尝试去掉msg中的空格,这可能会导致您的网址格式错误。
  • 我怀疑你的网址。您是否与邮递员或类似的客户核实以访问该服务?
  • 如果我删除空格消息会不会是不可读的@ Asew

标签: java url


【解决方案1】:

添加: 字符串 mainUrl = mainUrl.replaceAll(" ", "%20");

在这一行之后: 字符串 mainUrl="http://smsstreet.in/..."

【讨论】:

  • 这 %20 会创建空间吗?
  • 非常感谢@Lord Tesla
  • 至少使用适当的转义功能,而不是尝试自己做。 -1
  • 是的,通过替换 %20 而不是空格,这也是在域名中表示空格的相同方式,您可能会解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-21
  • 1970-01-01
  • 1970-01-01
  • 2015-07-17
相关资源
最近更新 更多