簡體   English   中英

使用Java Swing開發SMS發送應用程序

[英]Developing an SMS Sending Application using Java Swing

我在課堂上有2個功能:

  1. login()登錄到站點

  2. Send_Sms()發送短信

在這里,我正在考慮為函數放置2個線程,以便可以通過某種方式控制Send_Sms函數的Send_Sms ,以便在login()成功完成后開始發送短信,否則程序將無法工作。

到目前為止,這是我所做的:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;


public class RunThreads extends Thread
{
    public void Login(String username,String password)
    {
        try
        {
            // creates the batch file for Logging into abc.com server
            Writer output = null;
            String UserAgent = "Mozilla/5.0 (Windows NT 5.1;KM:10.0.2)";
            String postdata = "\"" + "username="+username+ "&password=" + password + "&button=Login" + "\"";
            String MainStream = "c:\\wget.exe --output-document=login.html --user-agent=" + "\"" + UserAgent + "\"" + " --max-redirect=10 --cookies=on --keep-session-cookies --save-cookies=cookie.txt --post-data " + postdata + " http://www.abc.com/Login1.action;";
            File file = new File("login.bat");
            output = new BufferedWriter(new FileWriter(file));
            output.write(MainStream);
            output.close();
            Runtime.getRuntime().exec("cmd /c start login.bat");
        }
        catch (IOException ex)
        {
            Logger.getLogger(Save_Credentials.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void SendSms(String mobno,String content) throws IOException
    {
                Writer output = null;
                String UserAgent = "Mozilla/5.0 (Windows NT 5.1;KM:10.0.2)";
                String MainStream = "c:\\wget.exe --output-document=quicksms.html --user-agent=" + "\"" + UserAgent + "\"" + " --referer=http://www.abc.com/jsp/SMS.jsp --cookies=on --keep-session-cookies --load-cookies=cookie.txt --save-cookies=cookie.txt --post-data "+"\""+"&HiddenAction=instantsms&catnamedis=Birthday&Action=gstahsbdf5346g&chkall=on&MobNo="+mobno+"&textArea="+content+"\""+" http://www.abc.com/quicksms.action;";
                File file = new File("SendSms.bat");
                output = new BufferedWriter(new FileWriter(file));
                output.write(MainStream);
                output.close();
                Runtime.getRuntime().exec("cmd /c start SendSms.bat");
    }
    public static void main(String[] args) 
    {


    }
}

如您自己所言,如果登錄失敗,將無法發送短信,因此:

可以通過以下方式控制Send_Sms函數的執行權:成功完成login()之后,它將開始發送短信,否則程序將無法工作。

那么這兩個任務是順序耦合的,這意味着在不同線程上並行執行它們是沒有意義的。 只需在單個線程上按順序執行它們即可。

暫無
暫無

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

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