我正在尝试使用struts在手机号码上发送短信

时间:2017-06-16 07:29:57

标签: java

我在struts工作,我正在使用eclipse,我正在处理手机号码发送短信并且它正在工作,但我无法取消未发送的消息,因为如果我发送无效号码的消息,那我怎样才能获得信息号码无效?每次显示200个响应代码。

public class LessonAction extends Action
{
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
        String message = " Greetings";
        String username = "91886665555";
        String password = "xyz";
        String number = "0000000";
        String requestUrl  = "http://www.smszone.in/sendsms.asp?page=SendSmsBulk&"+
                             "username=" + URLEncoder.encode(username, "UTF-8")+
                             "&password=" + URLEncoder.encode(password, "UTF-8")+
                            "&number=" + URLEncoder.encode(number, "UTF-8")+
                             "&message=" + URLEncoder.encode(message, "UTF-8");
      System.out.println(requestUrl);
        URL url = new URL(requestUrl);
        HttpURLConnection uc = (HttpURLConnection)url.openConnection();
        System.out.println(uc.getResponseMessage());
        System.out.println(uc.getResponseCode());
        uc.setDoOutput(true);
        OutputStream os= uc.getOutputStream();       
        StringBuilder r = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        String line;
        while ( (line = br.readLine()) != null)
        r.append("status="+line);
        br.close();
        os.close();
        System.out.println(r.toString());     
        uc.disconnect();
        System.out.println("jignesh");              
        }
        catch (Exception e) 
        {
            System.out.println(e);
        }
        return mapping.findForward("success");  
    }   
}

2 个答案:

答案 0 :(得分:0)

public boolean sendSms() throws Exception 
{
    // Prepare Url
    URLConnection myURLConnection = null;
    URL myURL = null;
    BufferedReader reader = null;

    // encoding message
    String encoded_message = URLEncoder.encode(message);

    // Send SMS API
    String mainUrl = "http://sms.hspsms.com/sendSMS?";

    // Prepare parameter string
    StringBuilder sbPostData = new StringBuilder(mainUrl);
    sbPostData.append("username=" + username);
    sbPostData.append("&message=" + encoded_message);
    sbPostData.append("&sendername=" + sendername);
    sbPostData.append("&smstype=" + smstype);
    sbPostData.append("&numbers=" + numbers);
    sbPostData.append("&apikey=" + apikey);

    // final string
    mainUrl = sbPostData.toString();
    try 
    {
        // prepare connection
        myURL = new URL(mainUrl);
        myURLConnection = myURL.openConnection();
        myURLConnection.connect();
        reader = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
        // reading response
        String response1;
        while ((response1 = reader.readLine()) != null)
            // print response
            System.out.println(response1);
            // finally close connection
        reader.close();
    }
    catch (IOException e) 
    {
        e.printStackTrace();
    }
    return true;
}

答案 1 :(得分:-1)

<javascript>文件中使用.jsp标签,编写js代码或jQuery代码来实现Ajax功能。另一种方法是编写一些servlet doGet函数,例如request.getParameter()。显示您的代码,可以为它回答更多。