2017-08-04 50 views
1

我編寫了一個從hasibeenpwned.com查詢API的Java程序。而幾乎每一次我得到的輸出:爲什麼我通過查詢API來獲得Http 503錯誤

run: 
Fehler 
java.lang.RuntimeException: Failed : HTTP error code : 503 
BUILD SUCCESSFUL (total time: 0 seconds) 

這看起來像一個DDoS保護,但在我的瀏覽器都工作正常,所以我不能夠解釋,我沒有發現任何其他錯誤。

我的代碼:

package checkpassword; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.util.ArrayList; 



public class CheckPassword 
{ 

    //Thats an old Email from Me ^^ 
    final static String testMail = "[email protected]"; 

    public static void main(String[] args) 
    { 

//  ArrayList<String> mailList = createEmailList(); 
//   
//  for(int i=0; i<mailList.size(); i++) 
//  { 
      System.out.println(getOutput(testMail)); 
//  } 

    } 



    public static String getOutput(String Mail) 
    { 

     String output; 

     try 
     { 

      URL url = new URL("https://haveibeenpwned.com/api/breachedaccount/" + Mail); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"); 
      conn.setRequestMethod("GET"); 
      conn.setRequestProperty("Accept", "application/json"); 

      if (conn.getResponseCode() != 200) 
      { 
       throw new RuntimeException("Failed : HTTP error code : " 
         + conn.getResponseCode()); 
      } 

      BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); 

      output = br.readLine(); 

      conn.disconnect(); 

     }catch (Exception ex) 
     { 
      output = "Fehler"; 
      System.err.println(ex); 
     } 
     return output; 
    } 

    public static ArrayList createEmailList() 
    { 

     ArrayList<String> mailList = new ArrayList<>(); 

     try 
     { 

     FileReader fr = new FileReader("C:/Users/s.kobe/Desktop/test.txt"); 
     BufferedReader br = new BufferedReader(fr); 

     while(true) 
     { 
      String Zeile = br.readLine(); 
      if(Zeile != null) 
      { 
       mailList.add(Zeile); 
      }else{ 
       break; 
      } 
     } 

     br.close(); 

     }catch(Exception ex) 
     { 
      System.err.println(ex); 
     } 

    return mailList;  
    } 

} 

感謝您的幫助提前

桑德羅

//一些文字,我達到要求,問我的問題,因爲我認爲我解釋所有重要東西

回答

0

HTTP 503表示服務暫時不可用。

但是,既然你說它在瀏覽器中工作得很好,那麼很可能Web服務器被配置爲以這種方式對諸如你的腳本進行響應。

您可以嘗試欺騙User Agent字符串,這意味着您可以假裝爲現代Web瀏覽器,並查看服務器是否響應不同。你可能想看看curl,這會讓你在命令行上快速嘗試這個。

相關問題