Java Code Examples for com.gargoylesoftware.htmlunit.WebClient#expandUrl()
The following examples show how to use
com.gargoylesoftware.htmlunit.WebClient#expandUrl() .
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: HtmlPage.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Given a relative URL (ie <tt>/foo</tt>), returns a fully-qualified URL based on * the URL that was used to load this page. * * @param relativeUrl the relative URL * @return the fully-qualified URL for the specified relative URL * @exception MalformedURLException if an error occurred when creating a URL object */ public URL getFullyQualifiedUrl(String relativeUrl) throws MalformedURLException { // to handle http: and http:/ in FF (Bug #474) if (hasFeature(URL_MISSING_SLASHES)) { boolean incorrectnessNotified = false; while (relativeUrl.startsWith("http:") && !relativeUrl.startsWith("http://")) { if (!incorrectnessNotified) { notifyIncorrectness("Incorrect URL \"" + relativeUrl + "\" has been corrected"); incorrectnessNotified = true; } relativeUrl = "http:/" + relativeUrl.substring(5); } } return WebClient.expandUrl(getBaseURL(), relativeUrl); }
Example 2
Source File: HtmlPage.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Given a relative URL (ie <tt>/foo</tt>), returns a fully-qualified URL based on * the URL that was used to load this page. * * @param relativeUrl the relative URL * @return the fully-qualified URL for the specified relative URL * @exception MalformedURLException if an error occurred when creating a URL object */ public URL getFullyQualifiedUrl(String relativeUrl) throws MalformedURLException { // to handle http: and http:/ in FF (Bug #474) if (hasFeature(URL_MISSING_SLASHES)) { boolean incorrectnessNotified = false; while (relativeUrl.startsWith("http:") && !relativeUrl.startsWith("http://")) { if (!incorrectnessNotified) { notifyIncorrectness("Incorrect URL \"" + relativeUrl + "\" has been corrected"); incorrectnessNotified = true; } relativeUrl = "http:/" + relativeUrl.substring(5); } } return WebClient.expandUrl(getBaseURL(), relativeUrl); }
Example 3
Source File: HtmlForm.java From HtmlUnit-Android with Apache License 2.0 | 4 votes |
/** * <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; }