简体   繁体   中英

Developing an SMS Sending Application using Java Swing

I have a 2 functions in a class:

  1. login() Logs into a site

  2. Send_Sms() Sends the sms

Here I am Thinking of putting 2 threads for the functions so that the exceution of the function Send_Sms can be controlled in a manner that it starts to send the sms after the login() has completed successfully otherwise program will not work.

This is what I have done so far :

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) 
    {


    }
}

Since an SMS cannot be sent if the login fails, as you yourself said:

the exceution of the function Send_Sms can be controlled in a manner that it starts to send the sms after the login() has completed successfully otherwise program will not work.

then these two tasks are sequentially coupled, meaning that executing them in parallel on different threads is meaningless. Just execute them in a sequence on a single thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM