简体   繁体   中英

How do I loop through a java.io exception (502)?

I'm getting this error: java.io.IOException: Server returned HTTP response code: 502 for URL:

and it ends my program if the website has a bad gate, and its inconsistent on working or not. Is there any way I can force it to keep on retrying the website until it gets a response?

this is currently my code if it matters:

    URL webpage = null;
    URLConnection conn = null;
    try{
        webpage = new URL(websiteurl);
        conn=webpage.openConnection();
        InputStreamReader reader = new InputStreamReader(conn.getInputStream(), "UTF8");
        BufferedReader buffer = new BufferedReader(reader);
        String line = "";
        while(true){
            line = buffer.readLine();
            if(line!=null){
               System.out.println(line);
            }
            else
                break;
        }
    }
    catch(Exception e){
        e.printStackTrace();
    }

nevermind solved it by recalling my method in the catch and adding in a pause between each call: this is what it is now

URL webpage = null;
            URLConnection conn = null;
            try{
                webpage = new URL(website);
                
                conn=webpage.openConnection();
                InputStreamReader reader = new InputStreamReader(conn.getInputStream(), "UTF8");
                BufferedReader buffer = new BufferedReader(reader);
                String line = "";
                while(true){
                    line = buffer.readLine();
                    if(line!=null){
                       System.out.println(line);
                    }
                    else
                        break;
                }
            }
            catch(Exception e){
                e.printStackTrace();
                 try
            {
                Thread.sleep(5000);
            }
            catch(InterruptedException ex)
            {
                Thread.currentThread().interrupt();
            }
                findCreationDate(name);
            }

solved it by recalling my method in the catch and adding in a pause between each call: this is what it is now

URL webpage = null;
            URLConnection conn = null;
            try{
                webpage = new URL(website);
                
                conn=webpage.openConnection();
                InputStreamReader reader = new InputStreamReader(conn.getInputStream(), "UTF8");
                BufferedReader buffer = new BufferedReader(reader);
                String line = "";
                while(true){
                    line = buffer.readLine();
                    if(line!=null){
                       System.out.println(line);
                    }
                    else
                        break;
                }
            }
            catch(Exception e){
                e.printStackTrace();
                 try
            {
                Thread.sleep(5000);
            }
            catch(InterruptedException ex)
            {
                Thread.currentThread().interrupt();
            }
                findCreationDate(name);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM