1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

HTMLUnit proxy?

Discussion in 'Other Languages' started by plajndek, Aug 2, 2012.

  1. plajndek

    plajndek Newbie

    Joined:
    Jun 20, 2012
    Messages:
    44
    Likes Received:
    11
    Location:
    Depend on IP
    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());
      }
     
  2. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,155
    Java != JavaScript :)

    Thread moved to the proper section
     
  3. xpro

    xpro Regular Member

    Joined:
    Jan 21, 2009
    Messages:
    416
    Likes Received:
    16
    you need to put a while loop outside of the try catch block
     
  4. closedCaption

    closedCaption Regular Member

    Joined:
    Oct 3, 2011
    Messages:
    289
    Likes Received:
    111
    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: Aug 7, 2012