java - Check if a proxy can connect to a website? -


basically i'm building proxy checker check if can connect specific website. problem when proxy doesn't work takes huge amounts of time cancel try , move next.

i've tried "setconnecttimeout();" parameter doesn't seem work properly.

any appreciated, thanks.

  private void jbutton1actionperformed(java.awt.event.actionevent evt) {//gen-first:event_jbutton1actionperformed     try {         executeloop();     } catch (exception ex) {         logger.getlogger(proxycheckergui.class.getname()).log(level.severe, null, ex);     } }//gen-last:event_jbutton1actionperformed  private void executeloop() throws exception{      string getproxies[] = jtextareaproxy.gettext().split("\n");     string proxyip;     int proxyport;     system.out.println("quantityoflines:" + getproxies.length);      (int = 0; < getproxies.length; i++) {          system.out.println("line: " + i);         string proxy[] = getproxies[i].split(":");         if (proxy.length == 2 & proxy[0].trim().length() > 0 && proxy[1].trim().length() > 0) {             proxyip = proxy[0];             proxyport = integer.parseint(proxy[1]);              try {                 testproxy(proxyip, proxyport);             } catch (exception ex) {                 logger.getlogger(proxycheckergui.class.getname()).log(level.severe, null, ex);             }         }     }  }  private void testproxy(string proxyip, int proxyport) throws exception {      string url = "https://www.google.pt";     url obj = new url(url);     proxy proxy = new proxy(proxy.type.http, new inetsocketaddress(proxyip, proxyport));     httpsurlconnection con = (httpsurlconnection) obj.openconnection(proxy);     //con.setrequestmethod("get");     //con.setrequestproperty("user-agent", "mozilla/5.0");     con.connect();     con.setconnecttimeout(3000);      int response = con.getresponsecode();     system.out.println("\nresponse:" + response);      if (response == 200) {         system.out.println("working : " + proxyip + ":" + proxyport);         jtextareaworking.append(proxyip + ":" + proxyport + "\n");     } else {         system.out.println("not working : " + proxyip + ":" + proxyport);     }     con.disconnect(); } 

you may have better luck setting readtimeout

con.setreadtimeout(3000) 

sometimes server happily accept connection doesn't respond.

with exception depends if it's fatal exception or can carry on after it. here looks exception isn't fatal not working can recover , try else. yes, handle exceptions gracefully.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -