Unable to navigate through HTTP Proxy using HtmlUnitDriver

21 views
Skip to first unread message

FcoSandoval

unread,
Jun 1, 2017, 12:29:36 AM6/1/17
to Selenium Users
Hi all,

I'm using Selenuim with HtmlUnitDriver for navigating and downloading a file from a site. It works fine when I hit directly ti the site from my machine, but when I do through a Http proxy, I cannot get connected to the site... If I look at log file it hangs making the connection to timeout:


2017-05-31 13:06:09,752 [[ACTIVE] ExecuteThread: '19' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG c.b.c.c.sii.SiiNavigator - obtenerArchivoSii -> A ingresar a URL: https://200.10.252.80/sismunInternet/?caller=ABIF
2017-05-31 13:06:09,760 [Thread-26234] DEBUG c.g.htmlunit.html.DomElement - Firing Event beforeunload (Current Target: HTMLElement for HtmlHtml[<html>]);
2017-05-31 13:06:09,763 [Thread-26234] DEBUG c.g.htmlunit.WebClient - Get page for window named '', using WebRequest[<url="https://200.10.252.80/sismunInternet/?caller=ABIF", GET, EncodingType[name=application/x-www-form-urlencoded], [], {Accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8, Accept-Encoding=gzip, deflate}, null>]
2017-05-31 13:06:09,765 [Thread-26234] DEBUG c.g.htmlunit.WebClient - Load response for GET https://200.10.252.80/sismunInternet/?caller=ABIF
2017-05-31 13:06:09,954 [Thread-26234] DEBUG c.g.h.h.HtmlUnitSSLConnectionSocketFactory - Enabled protocols: [TLSv1, TLSv1.1, TLSv1.2]
2017-05-31 13:06:09,954 [Thread-26234] DEBUG c.g.h.h.HtmlUnitSSLConnectionSocketFactory - Enabled cipher suites:[TLS_RSA_WITH_AES_256_CBC_SHA256, TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, TLS_DHE_DSS_WITH_AES_256_CBC_SHA256, TLS_RSA_WITH_AES_256_CBC_SHA, TLS_DHE_RSA_WITH_AES_256_CBC_SHA, TLS_DHE_DSS_WITH_AES_256_CBC_SHA, TLS_RSA_WITH_AES_128_CBC_SHA256, TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, TLS_DHE_DSS_WITH_AES_128_CBC_SHA256, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_DSS_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_AES_256_GCM_SHA384, TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, TLS_DHE_DSS_WITH_AES_256_GCM_SHA384, TLS_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_DSS_WITH_AES_128_GCM_SHA256, SSL_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA, TLS_EMPTY_RENEGOTIATION_INFO_SCSV]
2017-05-31 13:06:09,957 [Thread-26234] DEBUG c.g.h.h.HtmlUnitSSLConnectionSocketFactory - Starting handshake


It seems that the proxy is not set.
I've tried everything found in the net to configure the proxy, but no success. My code for creating the WebDriver is:

    private WebDriver createWebDriver() {

        

        boolean useProxy = true;

        Proxy proxy = useProxy ? new Proxy() : null;


        DesiredCapabilities capability = DesiredCapabilities.htmlUnit();

        capability.setJavascriptEnabled(true);


        // Chequea si debe configurar un proxy de salida a internet, si es que está configurado

        if ( useProxy ) {

            String proxySettings = PROXY_HOST + ":" + PROXY_PORT;

            proxy.setProxyType(ProxyType.MANUAL);            

            proxy.setHttpProxy(proxySettings).setSslProxy(proxySettings);

            capability.setCapability(CapabilityType.PROXY, proxy);

            capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

            capability.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);

        }


        HtmlUnitDriver driver = new HtmlUnitDriver(capability) {

            

            @Override

            protected WebClient modifyWebClient(WebClient client) {

                

                // Configura que no se aborte la ejecución de la página por errores de JavaScript

                client.getOptions().setThrowExceptionOnScriptError(false);

                LOGGER.debug("modifyWebClient -> isThrowExceptionOnScriptError: {}", client.getOptions().isThrowExceptionOnScriptError());


                // Chequea si debe configurar un proxy de salida a internet, si es que está configurado

                if ( useProxy ) {

                    ProxyConfig proxyConfig = new ProxyConfig(PROXY_HOSTPROXY_PORT);

                    proxyConfig.setSocksProxy(false);

                    client.getOptions().setUseInsecureSSL(true);

                    client.getOptions().setProxyConfig(proxyConfig);               

                    

                    // Agrega el provider a la instancia webClient 

                    DefaultCredentialsProvider credentialsProvider = new DefaultCredentialsProvider();

                    credentialsProvider.addCredentials("username", "password");

                    client.setCredentialsProvider(credentialsProvider);

                }

                

                // Configura handler para ventanas de confirmación que siempre acepta (retorna true)

                ConfirmHandler okHandler = new ConfirmHandler() {

                    public boolean handleConfirm(Page page, String message) {

                        return true;

                    }

                };

                client.setConfirmHandler(okHandler);


                // Configura handler para procesar respuesta de una página

                client.addWebWindowListener(new WebWindowListener() {


                    public void webWindowOpened(WebWindowEvent event) {

                    }


                    @SuppressWarnings("unchecked")

                    public void webWindowContentChanged(WebWindowEvent event) {


                        WebResponse response = event.getWebWindow().getEnclosedPage().getWebResponse();

                        try {

                            Map<String, String> headersMap = ObjectUtils.toMap(response.getResponseHeaders(), "name", "value");

                            // Si es de tipo application/octet-strem, es un archivo

                            if (response.getContentType().equals("application/octet-strem")) {                        

                                // Genera el path donde quedarán los archivos

                                String actualPath = "";

                                String downloadPath = SII_DIRECTORY;

                                if ( StringUtils.isNotNullOrEmpty(downloadPath) ) {

                                    File downloadDir = new File(downloadPath);

                                    if (!downloadDir.exists())

                                        downloadDir.mkdirs();

                                    actualPath = downloadPath + File.separator;

                                }

                                // Obtiene el nombre del archivo a bajar y lo copia

                                String contentDisposition = headersMap.get("Content-Disposition");

                                String downloadedFileName = actualPath + contentDisposition.substring(contentDisposition.indexOf('=') + 1);

                                LOGGER.debug("webWindowContentChanged -> Downloading file: {}", downloadedFileName);

                                IOUtils.copy(response.getContentAsStream(), new FileOutputStream(downloadedFileName));

                            }

                        } catch (Exception ex) {

                            LOGGER.error("webWindowContentChanged -> Exception: {}", ex.getMessage(), ex);

                        }

                    }


                    public void webWindowClosed(WebWindowEvent event) {

                    }

                });

                

                return client;

            }

          };


          // Chequea si debe configurar un proxy de salida a internet, si es que está configurado

          if ( useProxy ) {

              driver.setProxySettings(proxy);

              driver.setAcceptSslCertificates(true);

          }

          

          driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);


          driver.setJavascriptEnabled(true);

          return driver;

    }


I'll appreciate any help. Thanks.

Reply all
Reply to author
Forward
0 new messages