簡體   English   中英

使用Java Spring MVC發送短信不起作用

[英]send SMS using java spring mvc not working

我正在使用一個Web應用程序Spring MVC.i,希望使用Web發送短信。 我嘗試下面的代碼。 如果我使用main()運行單個java文件,則它可以正常工作,而當我通過網絡嘗試運行時,它不起作用。

誰能幫我解決這個問題。 下面是我的代碼

   public static String doSendSMS(String url_str) {
    StringBuffer response = new StringBuffer();
    try {
        URL obj = new URL(url_str);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        // optional default is GET
        con.setRequestMethod("GET");
        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url_str);
        System.out.println("Response Code : " + responseCode);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
    } catch (Exception e) {
    }
    return response.toString();
}

下面是工作代碼。

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

}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM