Java Code Examples for com.gargoylesoftware.htmlunit.WebRequest#setCharset()

The following examples show how to use com.gargoylesoftware.htmlunit.WebRequest#setCharset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HtmlEmbed.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Saves this content as the specified file.
 * @param file the file to save to
 * @throws IOException if an IO error occurs
 */
public void saveAs(final File file) throws IOException {
    final HtmlPage page = (HtmlPage) getPage();
    final WebClient webclient = page.getWebClient();

    final URL url = page.getFullyQualifiedUrl(getAttributeDirect(SRC_ATTRIBUTE));
    final WebRequest request = new WebRequest(url);
    request.setCharset(page.getCharset());
    request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm());
    final WebResponse webResponse = webclient.loadWebResponse(request);

    try (OutputStream fos = Files.newOutputStream(file.toPath());
            InputStream content =  webResponse.getContentAsStream()) {
        IOUtils.copy(content, fos);
    }
}
 
Example 2
Source File: HtmlImageInput.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Downloads the image contained by this image element.</p>
 * <p><span style="color:red">POTENTIAL PERFORMANCE KILLER - DOWNLOADS THE IMAGE - USE AT YOUR OWN RISK</span></p>
 * <p>If the image has not already been downloaded, this method triggers a download and caches the image.</p>
 *
 * @throws IOException if an error occurs while downloading the image
 */
private void downloadImageIfNeeded() throws IOException {
    if (!downloaded_) {
        // HTMLIMAGE_BLANK_SRC_AS_EMPTY
        final String src = getSrcAttribute();
        if (!"".equals(src)
                && !(hasFeature(HTMLIMAGE_BLANK_SRC_AS_EMPTY) && StringUtils.isBlank(src))) {
            final HtmlPage page = (HtmlPage) getPage();
            final WebClient webClient = page.getWebClient();

            final URL url = page.getFullyQualifiedUrl(src);
            final BrowserVersion browser = webClient.getBrowserVersion();
            final WebRequest request = new WebRequest(url, browser.getImgAcceptHeader(),
                                                            browser.getAcceptEncodingHeader());
            request.setCharset(page.getCharset());
            request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm());
            imageWebResponse_ = webClient.loadWebResponse(request);
        }

        downloaded_ = hasFeature(JS_IMAGE_COMPLETE_RETURNS_TRUE_FOR_NO_REQUEST)
                || (imageWebResponse_ != null && imageWebResponse_.getContentType().contains("image"));
    }
}
 
Example 3
Source File: YamlReaderTest.java    From configuration-as-code-plugin with MIT License 6 votes vote down vote up
@Test
public void httpDoCheck() throws Exception {
    j.jenkins.setCrumbIssuer(null);

    URL apiURL = new URL(MessageFormat.format(
        "{0}configuration-as-code/check",
        j.getURL().toString()));
    WebRequest request =
        new WebRequest(apiURL, POST);
    request.setCharset(StandardCharsets.UTF_8);
    request.setRequestBody("jenkins:\n"
        + "  systemMessage: \"configuration as code - JenkinsConfigTestHttpRequest\"\n"
        + "  quietPeriod: 10");
    int response = j.createWebClient().getPage(request).getWebResponse().getStatusCode();
    assertThat(response, is(200));
}
 
Example 4
Source File: HtmlImage.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Downloads the image contained by this image element.</p>
 * <p><span style="color:red">POTENTIAL PERFORMANCE KILLER - DOWNLOADS THE IMAGE - USE AT YOUR OWN RISK</span></p>
 * <p>If the image has not already been downloaded, this method triggers a download and caches the image.</p>
 *
 * @throws IOException if an error occurs while downloading the image
 */
private void downloadImageIfNeeded() throws IOException {
    if (!downloaded_) {
        // HTMLIMAGE_BLANK_SRC_AS_EMPTY
        final String src = getSrcAttribute();

        if (!"".equals(src)) {
            final HtmlPage page = (HtmlPage) getPage();
            final WebClient webClient = page.getWebClient();
            final BrowserVersion browser = webClient.getBrowserVersion();

            if (!(browser.hasFeature(HTMLIMAGE_BLANK_SRC_AS_EMPTY)
                    && StringUtils.isBlank(src))) {
                final URL url = page.getFullyQualifiedUrl(src);
                final WebRequest request = new WebRequest(url, browser.getImgAcceptHeader(),
                                                                browser.getAcceptEncodingHeader());
                request.setCharset(page.getCharset());
                request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm());
                imageWebResponse_ = webClient.loadWebResponse(request);
            }
        }

        if (imageData_ != null) {
            imageData_.close();
            imageData_ = null;
        }
        downloaded_ = true;
        isComplete_ = hasFeature(JS_IMAGE_COMPLETE_RETURNS_TRUE_FOR_NO_REQUEST)
                || (imageWebResponse_ != null && imageWebResponse_.getContentType().contains("image"));

        width_ = -1;
        height_ = -1;
    }
}
 
Example 5
Source File: HtmlArea.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected boolean doClickStateUpdate(final boolean shiftKey, final boolean ctrlKey) throws IOException {
    final HtmlPage enclosingPage = (HtmlPage) getPage();
    final WebClient webClient = enclosingPage.getWebClient();

    final String href = getHrefAttribute().trim();
    if (!href.isEmpty()) {
        final HtmlPage page = (HtmlPage) getPage();
        if (StringUtils.startsWithIgnoreCase(href, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
            page.executeJavaScript(
                href, "javascript url", getStartLineNumber());
            return false;
        }
        final URL url;
        try {
            url = enclosingPage.getFullyQualifiedUrl(getHrefAttribute());
        }
        catch (final MalformedURLException e) {
            throw new IllegalStateException(
                    "Not a valid url: " + getHrefAttribute());
        }
        final WebRequest request = new WebRequest(url);
        request.setCharset(page.getCharset());
        request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm());
        final WebWindow webWindow = enclosingPage.getEnclosingWindow();
        webClient.getPage(
                webWindow,
                enclosingPage.getResolvedTarget(getTargetAttribute()),
                request);
    }
    return false;
}
 
Example 6
Source File: HtmlLink.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the request which will allow us to retrieve the content referenced by the {@code href} attribute.
 * @return the request which will allow us to retrieve the content referenced by the {@code href} attribute
 * @throws MalformedURLException in case of problem resolving the URL
 */
public WebRequest getWebRequest() throws MalformedURLException {
    final HtmlPage page = (HtmlPage) getPage();
    final URL url = page.getFullyQualifiedUrl(getHrefAttribute());

    final BrowserVersion browser = page.getWebClient().getBrowserVersion();
    final WebRequest request = new WebRequest(url, browser.getCssAcceptHeader(), browser.getAcceptEncodingHeader());
    // use the page encoding even if this is a GET requests
    request.setCharset(page.getCharset());

    request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm());

    return request;
}
 
Example 7
Source File: YamlReaderTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@Test(expected = FailingHttpStatusCodeException.class)
public void httpDoCheckFailure() throws Exception {
    j.jenkins.setCrumbIssuer(null);

    URL apiURL = new URL(MessageFormat.format(
        "{0}configuration-as-code/check",
        j.getURL().toString()));
    WebRequest request =
        new WebRequest(apiURL, POST);
    request.setCharset(StandardCharsets.UTF_8);
    request.setRequestBody("jenkins:\n"
        + "  systemMessage: {}");
    j.createWebClient().getPage(request);
}
 
Example 8
Source File: HtmlForm.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Gets the request for a submission of this form with the specified SubmittableElement.
 * @param submitElement the element that caused the submit to occur
 * @return the request
 */
public WebRequest getWebRequest(final SubmittableElement submitElement) {
    final HtmlPage htmlPage = (HtmlPage) getPage();
    final List<NameValuePair> parameters = getParameterListForSubmit(submitElement);
    final HttpMethod method;
    final String methodAttribute = getMethodAttribute();
    if ("post".equalsIgnoreCase(methodAttribute)) {
        method = HttpMethod.POST;
    }
    else {
        if (!"get".equalsIgnoreCase(methodAttribute) && StringUtils.isNotBlank(methodAttribute)) {
            notifyIncorrectness("Incorrect submit method >" + getMethodAttribute() + "<. Using >GET<.");
        }
        method = HttpMethod.GET;
    }

    final BrowserVersion browser = getPage().getWebClient().getBrowserVersion();
    String actionUrl = getActionAttribute();
    String anchor = null;
    String queryFromFields = "";
    if (HttpMethod.GET == method) {
        if (actionUrl.contains("#")) {
            anchor = StringUtils.substringAfter(actionUrl, "#");
        }
        final Charset enc = getPage().getCharset();
        queryFromFields =
            URLEncodedUtils.format(Arrays.asList(NameValuePair.toHttpClient(parameters)), enc);

        // action may already contain some query parameters: they have to be removed
        actionUrl = StringUtils.substringBefore(actionUrl, "#");
        actionUrl = StringUtils.substringBefore(actionUrl, "?");
        parameters.clear(); // parameters have been added to query
    }
    URL url;
    try {
        if (actionUrl.isEmpty()) {
            url = WebClient.expandUrl(htmlPage.getUrl(), actionUrl);
        }
        else {
            url = htmlPage.getFullyQualifiedUrl(actionUrl);
        }

        if (!queryFromFields.isEmpty()) {
            url = UrlUtils.getUrlWithNewQuery(url, queryFromFields);
        }

        if (HttpMethod.GET == method && browser.hasFeature(FORM_SUBMISSION_URL_WITHOUT_HASH)
                && WebClient.URL_ABOUT_BLANK != url) {
            url = UrlUtils.getUrlWithNewRef(url, null);
        }
        else if (HttpMethod.POST == method
                && browser.hasFeature(FORM_SUBMISSION_URL_WITHOUT_HASH)
                && WebClient.URL_ABOUT_BLANK != url
                && StringUtils.isEmpty(actionUrl)) {
            url = UrlUtils.getUrlWithNewRef(url, null);
        }
        else if (anchor != null
                && WebClient.URL_ABOUT_BLANK != url) {
            url = UrlUtils.getUrlWithNewRef(url, anchor);
        }
    }
    catch (final MalformedURLException e) {
        throw new IllegalArgumentException("Not a valid url: " + actionUrl);
    }

    final WebRequest request = new WebRequest(url, method);
    request.setAdditionalHeader(HttpHeader.ACCEPT, browser.getHtmlAcceptHeader());
    request.setAdditionalHeader(HttpHeader.ACCEPT_ENCODING, "gzip, deflate");
    request.setRequestParameters(parameters);
    if (HttpMethod.POST == method) {
        request.setEncodingType(FormEncodingType.getInstance(getEnctypeAttribute()));
    }
    request.setCharset(getSubmitCharset());

    String referer = htmlPage.getUrl().toExternalForm();
    request.setAdditionalHeader(HttpHeader.REFERER, referer);

    if (HttpMethod.POST == method
            && browser.hasFeature(FORM_SUBMISSION_HEADER_ORIGIN)) {
        referer = StringUtils.stripEnd(referer, "/");
        request.setAdditionalHeader(HttpHeader.ORIGIN, referer);
    }
    if (HttpMethod.POST == method
            && browser.hasFeature(FORM_SUBMISSION_HEADER_CACHE_CONTROL_MAX_AGE)) {
        request.setAdditionalHeader(HttpHeader.CACHE_CONTROL, "max-age=0");
    }
    if (browser.hasFeature(FORM_SUBMISSION_HEADER_CACHE_CONTROL_NO_CACHE)) {
        request.setAdditionalHeader(HttpHeader.CACHE_CONTROL, "no-cache");
    }

    return request;
}