停止代码执行Java

时间:2014-07-26 02:52:46

标签: java

所以我一直在构建一个基本上调用我的服务器并检查页面源代码的程序。唯一的问题是,因为我有一个无限循环,所以它获取服务器的源代码,然后在循环的底部保存当前页面代码并刷新实际页面代码在另一个变量中。如果这两个变量匹配(没有任何改变)那么它将继续循环并调用页面并反复获取源代码。除了经过一段时间服务器认为它是DoS攻击并且不允许我连接之外,这种方法很有效。我已经尝试过制作一个超长循环的方法,但这似乎是一种混乱的方式。我的问题是,他们无论如何都要告诉Java在一段时间内完全停止代码执行。例如,它会每隔1或2分钟调用一次服务器吗?

package Mailer2;
import java.util.Date;
import java.util.Properties;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.ArrayList;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class mailer2 {
    static Scanner console = new Scanner(System.in);
    public static void main(String[] args) throws AddressException, MessagingException, IOException, InterruptedException{
        System.out.println("Admin console");
        boolean StartServer = true;
        String old = "";

        while(StartServer == true){
            Thread.sleep(10000);
        // Get Info from Page
        String From = "";
        String To = "";
        String Subject = "";
        String Body = "";
        String Provider = "";
        URL GetInfo = new URL("http://smsmessenger.comuf.com/Users.php");

        String GetPage = getPageCode(GetInfo);
        //System.out.println("Got the String:" + GetPage);
        String CutDown = GetPage.substring(73,GetPage.indexOf("<br"));
        //System.out.println(CutDown);
        From = CutDown.substring(0,CutDown.indexOf("TO:")); // From
        //System.out.println(From);
        To = CutDown.substring(CutDown.indexOf("TO:") + 3,CutDown.indexOf("SUBJECT") - 1);
        //System.out.println(To);
        Subject = CutDown.substring(CutDown.indexOf("SUBJECT:") + 8,CutDown.indexOf("BODY"));
        //System.out.println(Subject);
        Body = CutDown.substring(CutDown.indexOf("BODY:") + 5, CutDown.indexOf("Provider"));
        //System.out.println(Body);
        Provider = CutDown.substring(CutDown.indexOf("Provider:") + 9);
        //System.out.println(Provider);

        // Cell provider check
        if(Provider.length() == 0){
            System.out.println("No Provider Detected!");
        }
        else if(Provider.toLowerCase().equals("sprint")){
            To += "@messaging.sprintpcs.com";
            //System.out.println("Sprint HERE:" + To);
        }
        else if(Provider.toLowerCase().equals("verizon")){
            To += "@vtext.com";
        }
        else if(Provider.toLowerCase().equals("att")){
            To += "@txt.att.net";
        }
        else if(Provider.toLowerCase().equals("tmobile") || Provider.toLowerCase().equals("t-mobile")){
            To += "@tmomail.net";
        }
        else{
        System.out.println("Provider NOT FOUND" + Provider);
        }
        if(!old.equals(GetPage)){
            System.out.println("SENDING EMAIL!!");
        SendEmail(From,To,Subject,Body);
        }
        System.out.println("Sleeping");
        Thread.sleep(100);
         old = GetPage;

        }


    }

    public static void SendEmail(String From, String To, String Subject, String Body) throws AddressException, MessagingException{

        Properties prop = new Properties();
        prop.put("mail.smtp.host", "ssrs.reachmail.net");
        prop.put("mail.smtp.port", "587");
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.starttls.enable", "true");


        Session session = Session.getDefaultInstance(prop, new Authenticator() {

            // Override method to Authenticate to mail server
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("MCA28\\admin", "pass");
            }
            });

        session.setDebug(true);

        MimeMessage Msg = new MimeMessage(session);
        //System.out.println("Please enter the From:");
        //String setFrom = console.nextLine();
        Msg.setFrom(new InternetAddress(From));
        //System.out.println("Please enter which email to send to");
        //String setTo = console.nextLine();
        Msg.setRecipients(RecipientType.TO, To);
        //System.out.println("Set the Subject");
        //String subject = console.nextLine();
        Msg.setSubject(Subject);
        //System.out.println("Set the message Body");
        //String MessageBody = console.nextLine();
        // Initiate MimeBodyPart for filling email content
        MimeBodyPart messagePart = new MimeBodyPart();
        messagePart.setText(Body);
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messagePart);
        Msg.setContent(multipart);

        // Email Sending process
        Transport.send(Msg);

    }
    public static String getPageCode(URL SQL) throws IOException // Gets URL, returns InputLine
    {
        URLConnection openLine = SQL.openConnection();
        BufferedReader in = null;
        String inputLine = null;
        String line;
        in = new BufferedReader(new InputStreamReader(openLine.getInputStream()));

        while((line = in.readLine()) != null)
        {
            inputLine += line;
        }

        return inputLine;

    }


}

1 个答案:

答案 0 :(得分:0)

如果没有更改,请将时间加倍,直到下次重新查询。保持加倍,直到达到某个预设的最大值,比如民意调查之间的一小时。一旦检测到更改,您就可以更频繁地返回查询。