2017-08-04 56 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,这会让你在命令行上快速尝试这个。

相关问题