2014-07-26 240 views
0

所以我一直在構建一個基本上調用我的服務器並檢查頁面源代碼的程序。唯一的問題是,因爲我有一個無限循環,所以它獲取服務器的源代碼,然後在循環的底部它保存當前頁面代碼並刷新實際頁面代碼在一個不同的變量。如果這兩個變量匹配(沒有任何變化),然後它將繼續循環並調用頁面並一遍又一遍地獲取源代碼。這很有效,除了一段時間後服務器認爲它是DoS攻擊並且不會讓我連接。我已經嘗試了一種方法,它會是一個超長的循環,但是這似乎是一個混亂的方式。我的問題是,他們無論如何要告訴Java完全停止代碼執行一段時間。例如,它會每隔1分鐘或2分鐘打電話給服務器?停止代碼執行Java

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; 

    } 


} 
+0

張貼一些代碼。 –

+0

使用一個計時器,也許? – user2864740

+0

呃,你的代碼已經有'Thread.sleep(10000);'和'Thread.sleep(100);'在裏面。你認爲這些做什麼? –

回答

0

如果沒有更改,請將時間加倍直到下一次重新查詢。保持一倍,直到達到預設的最大值,例如在民意測驗之間一小時。只要您檢測到更改,您就可以更頻繁地返回查詢。