使用java spring mvc发送短信不起作用

时间:2018-02-15 09:49:41

标签: java spring-mvc sms

我正在使用一个Web应用程序spring MVC.i想要使用web发送短信。 我试过下面的代码。 如果我使用main()运行单个java文件然后它的工作,当我通过web尝试它不工作时。

任何人都可以帮我解决这个问题。 以下是我的代码

string.prototype.replace()

1 个答案:

答案 0 :(得分:0)

以下是工作代码。

 public class sms {
    private static String sessionCookie;
    public static String loginSMS(String userName, String password,String url) {
    String cookie = null;
    URL urlLogin;
    String loginContent;
    HttpURLConnection loginConnection;

    try {
        //UTF-8 encoding is the web standard so data must be encoded to UTF-8
        userName = URLEncoder.encode(userName, "UTF-8");
        password = URLEncoder.encode(password, "UTF-8");
        urlLogin = new URL(url);
        loginConnection = (HttpURLConnection) urlLogin.openConnection();
        loginContent = "username=" + userName + "&password=" + password;
        loginConnection.setDoOutput(true);
        loginConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
        loginConnection.setRequestProperty("Content-Length", String.valueOf(loginContent.length()));
        loginConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        loginConnection.setRequestProperty("Accept", "*/*");
        loginConnection.setRequestProperty("Referer", url);
        loginConnection.setRequestMethod("POST");
        loginConnection.setInstanceFollowRedirects(false);
        //Writing the Content to the site
        PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(loginConnection.getOutputStream()), true);
        printWriter.print(loginContent);
        printWriter.flush();
        printWriter.close();
        //Reading the cookie
        cookie = loginConnection.getHeaderField("Set-Cookie");
    } catch (MalformedURLException ex) {
        System.err.println("Login URL Error");
    } catch (UnsupportedEncodingException ex) {
        System.err.println("Error in encoding Username or Password");
    } catch (IOException ex) {
        System.err.println("Can not connect to Login URL");
    }
    if (cookie == null || cookie.isEmpty()) {
        System.err.println("Some error occured...Try again in a few seconds..If still problem exists check your username and password");
    }
    sessionCookie = cookie;
    return cookie;

}

public static void sendSMS( String action,String urlString,String content) {
    loginSMS("user", "user123","url");
    URL sendURL;
    HttpURLConnection sendConnection;
    String sendContent;
    try {
        sendURL = new URL(urlString);
        sendConnection = (HttpURLConnection) sendURL.openConnection();
        // sendContent="custid=undefined&HiddenAction=instantsms&Action="+action+"&login=&pass=&MobNo="+ phoneNumber+ "&textArea="+message;
        sendContent = content;
        sendConnection.setDoOutput(true);
        sendConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
        sendConnection.setRequestProperty("Content-Length", String.valueOf(sendContent.getBytes().length));
        sendConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        sendConnection.setRequestProperty("Accept", "*/*");
        sendConnection.setRequestProperty("Cookie", sessionCookie);
        sendConnection.setRequestMethod("POST");
        sendConnection.setInstanceFollowRedirects(false);

        PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(sendConnection.getOutputStream()), true);
        printWriter.print(sendContent);
        printWriter.flush();
        printWriter.close();
        //Reading the returned web page to analyse whether the operation was sucessfull
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(sendConnection.getInputStream()));
        StringBuilder SendResult = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            SendResult.append(line);
            SendResult.append('\n');
            //Message has been submitted successfully
        }
        System.out.println("Responce : " + SendResult);
        bufferedReader.close();
        logoutSMS();

    } catch (UnsupportedEncodingException ex) {
        System.err.println("Message content encoding error");
        //  System.exit(0);
    } catch (MalformedURLException ex) {
        System.err.println("Sending URL Error");
        //   System.exit(0);
    } catch (IOException ex) {

        System.err.println("Sending URL Connection Error");
        ex.printStackTrace();
        //  System.exit(0);
    }

}
}