Why i get an Http 503 Error by query a API

时间:2017-08-04 12:48:08

标签: java api http

I wrote an Java Program that query a API from haveibeenpwned.com. And nearly every time i get the output:

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

That's looks like an DDOS Protection but in my Browser all works fine so i don't can explain that and i don't find any other errors.

My Code:

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 = "cool-man25@web.de";

    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;    
    }

}

Thanks for your help in advance

Sandro

//Some text that i reach the requirements to ask my Question because i think that i explain all important stuff

1 个答案:

答案 0 :(得分:0)

HTTP 503 means that the service is temporarily unavailable.

However, since you say it works just fine in a browser, it is likely that the web server is configured to respond this way to scripts such as yours.

You could try spoofing the User Agent string, which means you could pretend to be a modern web browser, and see if the server responds differently. You might want to look into curl, which will let you try this out quickly on the command line.