Search in sources :

Example 31 with WebRequest

use of com.gargoylesoftware.htmlunit.WebRequest in project spring-framework by spring-projects.

the class MockWebResponseBuilderTests method setup.

@BeforeEach
public void setup() throws Exception {
    this.webRequest = new WebRequest(new URL("http://company.example:80/test/this/here"));
    this.responseBuilder = new MockWebResponseBuilder(System.currentTimeMillis(), this.webRequest, this.response);
}
Also used : WebRequest(com.gargoylesoftware.htmlunit.WebRequest) URL(java.net.URL) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 32 with WebRequest

use of com.gargoylesoftware.htmlunit.WebRequest in project spring-framework by spring-projects.

the class HtmlUnitRequestBuilderTests method setup.

@BeforeEach
public void setup() throws Exception {
    webRequest = new WebRequest(new URL("https://example.com/test/this/here"));
    webRequest.setHttpMethod(HttpMethod.GET);
    requestBuilder = new HtmlUnitRequestBuilder(sessions, webClient, webRequest);
}
Also used : WebRequest(com.gargoylesoftware.htmlunit.WebRequest) URL(java.net.URL) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 33 with WebRequest

use of com.gargoylesoftware.htmlunit.WebRequest in project wechat by dllwh.

the class HtmlUnitHandler method crawl.

/**
 * ----------------------------------------------- [私有方法]
 */
@Override
public String crawl(String url, CrawlParameter crawlPara) {
    WebClient webClient = getWebClient(crawlPara);
    String reqtype = crawlPara.getReqmethod();
    Page page = null;
    String resource = "";
    try {
        if (StringUtils.isNotBlank(reqtype)) {
            WebRequest webRequest = new WebRequest(new URL(url));
            if ("post".equals(reqtype)) {
                webRequest.setHttpMethod(HttpMethod.POST);
            } else if ("get".equals(reqtype)) {
                webRequest.setHttpMethod(HttpMethod.GET);
            }
            Map<String, String> reqmap = crawlPara.getReqmap();
            if (MapUtils.isNotEmpty(reqmap)) {
                for (Entry<String, String> param : reqmap.entrySet()) {
                    webRequest.getRequestParameters().add(new NameValuePair(param.getKey(), param.getValue()));
                }
            }
            page = webClient.getPage(webRequest);
            resource = page.getWebResponse().getContentAsString();
        } else {
            page = webClient.getPage(url);
            resource = ((HtmlPage) page).asXml();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return resource;
}
Also used : NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Page(com.gargoylesoftware.htmlunit.Page) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL)

Example 34 with WebRequest

use of com.gargoylesoftware.htmlunit.WebRequest in project saga by timurstrekalov.

the class WebClientFactory method newInstance.

// New version allows for creation of web drivers without the need for a config object
// which may not be readily available if the driver is created by third-party code.
public static WebClient newInstance(final BrowserVersion version) {
    final WebClient client = new WebClient(version) {

        @Override
        public WebResponse loadWebResponse(final WebRequest webRequest) throws IOException {
            return new WebResponseProxy(super.loadWebResponse(webRequest));
        }
    };
    client.setIncorrectnessListener(quietIncorrectnessListener);
    client.setJavaScriptErrorListener(loggingJsErrorListener);
    client.setHTMLParserListener(quietHtmlParserListener);
    client.setCssErrorHandler(quietCssErrorHandler);
    client.getOptions().setJavaScriptEnabled(true);
    client.setAjaxController(new NicelyResynchronizingAjaxController());
    client.getOptions().setThrowExceptionOnScriptError(false);
    client.getOptions().setThrowExceptionOnFailingStatusCode(false);
    client.getOptions().setPrintContentOnFailingStatusCode(false);
    client.setWebConnection(new HttpWebConnection(client) {

        @Override
        protected WebResponse newWebResponseInstance(final WebResponseData responseData, final long loadTime, final WebRequest request) {
            return new WebResponseProxy(super.newWebResponseInstance(responseData, loadTime, request));
        }
    });
    return client;
}
Also used : WebResponse(com.gargoylesoftware.htmlunit.WebResponse) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) NicelyResynchronizingAjaxController(com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController) HttpWebConnection(com.gargoylesoftware.htmlunit.HttpWebConnection) WebResponseData(com.gargoylesoftware.htmlunit.WebResponseData) WebClient(com.gargoylesoftware.htmlunit.WebClient)

Example 35 with WebRequest

use of com.gargoylesoftware.htmlunit.WebRequest in project ats-framework by Axway.

the class HiddenBrowserDriver method fixHtmlUnitBehaviour.

/**
 * Fixing refresh handler to skip Refresh meta tags
 * Allowing connections to any host, regardless of whether they have valid certificates or not
 * Fixing JSESSIONID cookie value
 * Some applications expect double quotes in the beginning and at the end of the JSESSIONID cookie value
 */
private void fixHtmlUnitBehaviour() {
    Field webClientField = null;
    boolean fieldAccessibleState = false;
    try {
        TargetLocator targetLocator = webDriver.switchTo();
        webClientField = targetLocator.getClass().getDeclaringClass().getDeclaredField("webClient");
        fieldAccessibleState = webClientField.isAccessible();
        webClientField.setAccessible(true);
        final WebClient webClient = (WebClient) webClientField.get(targetLocator.defaultContent());
        // Allowing connections to any host, regardless of whether they have valid certificates or not
        webClient.getOptions().setUseInsecureSSL(true);
        // Set Http connection timeout (in milliseconds). The default value is 90 seconds, because in Firefox >= 16
        // the "network.http.connection-timeout" property is 90. But this value is not enough for some cases.
        // NOTE: use 0 for infinite timeout
        webClient.getOptions().setTimeout(5 * 60 * 1000);
        webClient.getOptions().setRedirectEnabled(true);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(true);
        webClient.getOptions().setPrintContentOnFailingStatusCode(true);
        // Hide CSS Warnings
        webClient.setCssErrorHandler(new SilentCssErrorHandler());
        // Suppress warnings like: "Expected content type ... but got ..."
        webClient.setIncorrectnessListener(new IncorrectnessListener() {

            // private final Log log = LogFactory.getLog( this.getClass() );
            @Override
            public void notify(final String message, final Object origin) {
            // log.warn( message );
            }
        });
        if (!Boolean.parseBoolean(System.getProperty(ALLOW_META_REFRESH_TAG))) {
            /*
                 * Fix for refresh meta tags eg. "<meta http-equiv="refresh" content="300">"
                 * The default refresh handler is with Thread.sleep(refreshSecondsFromMetaTag) in the main thread!!!
                     *
                     * Maybe we should check and test this handler: webClient.setRefreshHandler( new ThreadedRefreshHandler() );
                 */
            webClient.setRefreshHandler(new RefreshHandler() {

                @Override
                public void handleRefresh(Page page, URL url, int seconds) throws IOException {
                }
            });
        }
        /*
             * Fix JSessionId
             */
        // WebConnectionWrapper constructs a WebConnection object wrapping the connection of the WebClient
        // and places itself (in the constructor) as connection of the WebClient.
        new WebConnectionWrapper(webClient) {

            public WebResponse getResponse(WebRequest request) throws IOException {
                Cookie jsCookie = webClient.getCookieManager().getCookie("JSESSIONID");
                if (jsCookie != null && (!jsCookie.getValue().startsWith("\"") && !jsCookie.getValue().endsWith("\""))) {
                    Cookie newCookie = new Cookie(jsCookie.getDomain(), jsCookie.getName(), "\"" + jsCookie.getValue() + "\"", jsCookie.getPath(), jsCookie.getExpires(), jsCookie.isSecure());
                    webClient.getCookieManager().removeCookie(jsCookie);
                    webClient.getCookieManager().addCookie(newCookie);
                }
                return super.getResponse(request);
            }
        };
    } catch (Exception e) {
        throw new SeleniumOperationException("Error retrieving internal Selenium web client", e);
    } finally {
        if (webClientField != null) {
            webClientField.setAccessible(fieldAccessibleState);
        }
    }
}
Also used : Cookie(com.gargoylesoftware.htmlunit.util.Cookie) Page(com.gargoylesoftware.htmlunit.Page) IOException(java.io.IOException) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) WebClient(com.gargoylesoftware.htmlunit.WebClient) IncorrectnessListener(com.gargoylesoftware.htmlunit.IncorrectnessListener) URL(java.net.URL) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) IOException(java.io.IOException) Field(java.lang.reflect.Field) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) TargetLocator(org.openqa.selenium.WebDriver.TargetLocator) SilentCssErrorHandler(com.gargoylesoftware.htmlunit.SilentCssErrorHandler) RefreshHandler(com.gargoylesoftware.htmlunit.RefreshHandler) WebConnectionWrapper(com.gargoylesoftware.htmlunit.util.WebConnectionWrapper)

Aggregations

WebRequest (com.gargoylesoftware.htmlunit.WebRequest)84 Test (org.junit.Test)65 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)49 URL (java.net.URL)31 Path (java.nio.file.Path)31 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)12 TextPage (com.gargoylesoftware.htmlunit.TextPage)12 InputStream (java.io.InputStream)7 MkColMethodWebRequest (org.apache.archiva.webdav.httpunit.MkColMethodWebRequest)7 JenkinsRule (org.jvnet.hudson.test.JenkinsRule)7 WebClient (com.gargoylesoftware.htmlunit.WebClient)5 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)5 IOException (java.io.IOException)5 FreeStyleProject (hudson.model.FreeStyleProject)4 Page (com.gargoylesoftware.htmlunit.Page)3 NameValuePair (com.gargoylesoftware.htmlunit.util.NameValuePair)3 HashMap (java.util.HashMap)3 Issue (org.jvnet.hudson.test.Issue)3 SeleniumOperationException (com.axway.ats.uiengine.exceptions.SeleniumOperationException)2 IncorrectnessListener (com.gargoylesoftware.htmlunit.IncorrectnessListener)2