HTMLUnit proxy?

plajndek

Newbie
Joined
Jun 20, 2012
Messages
44
Reaction score
11
Hi, I'm a bit new to Java and HTMLUnit. I'm trying to use proxies in my program but if one proxy is dead program is shouting down(getting error smtg like timeout or 504 DNS Name Not Found for http:..). How can I force it to continue working?

Code:
 try{  // Open the file that is the first 
  // command line parameter
  FileInputStream fstream = new FileInputStream("textfile.txt");
  // Get the object of DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  //Read File Line By Line
  while ((strLine = br.readLine()) != null)   {
         
       ProxyConfig pc=new ProxyConfig();
            pc.setProxyHost(strLine);
            pc.setProxyPort(3128);
            webClient.setProxyConfig(pc);
            
            System.out.println(strLine);
            HtmlPage currentPage = webClient.getPage("site");
        //   


           // webClient.waitForBackgroundJavaScript(30000);
            //System.out.println(currentPage.asText());
  //System.out.println (strLine);
  }
  //Close the input stream
  in.close();
    }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
 
Java != JavaScript :)

Thread moved to the proper section
 
you need to put a while loop outside of the try catch block
 
You can do nested try catch BTW:

Code:
try{  // Open the file that is the first 
  // command line parameter
  FileInputStream fstream = new FileInputStream("textfile.txt");
  // Get the object of DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  //Read File Line By Line
  while ((strLine = br.readLine()) != null)   {
         
try {
       ProxyConfig pc=new ProxyConfig();
            pc.setProxyHost(strLine);
            pc.setProxyPort(3128);
            webClient.setProxyConfig(pc);
} catch (Exception ex) {
//do something System.out.println("That proxy doesn't work") or
// continue statement to skip to another line in file
}
            
            System.out.println(strLine);
            HtmlPage currentPage = webClient.getPage("site");
        //   


           // webClient.waitForBackgroundJavaScript(30000);
            //System.out.println(currentPage.asText());
  //System.out.println (strLine);
  }
  //Close the input stream
  in.close();
    }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
 
Last edited:
Back
Top