@Test public void buildResponseHeadersNullDomainDefaulted() throws Exception { Cookie cookie = new Cookie("cookieA", "valueA"); this.response.addCookie(cookie); WebResponse webResponse = this.responseBuilder.build(); List<NameValuePair> responseHeaders = webResponse.getResponseHeaders(); assertThat(responseHeaders.size(), equalTo(1)); NameValuePair header = responseHeaders.get(0); assertThat(header.getName(), equalTo("Set-Cookie")); assertThat(header.getValue(), equalTo("cookieA=valueA")); }
@Test public void buildResponseHeaders() throws Exception { this.response.addHeader("Content-Type", "text/html"); this.response.addHeader("X-Test", "value"); Cookie cookie = new Cookie("cookieA", "valueA"); cookie.setDomain("domain"); cookie.setPath("/path"); cookie.setMaxAge(1800); cookie.setSecure(true); cookie.setHttpOnly(true); this.response.addCookie(cookie); WebResponse webResponse = this.responseBuilder.build(); List<NameValuePair> responseHeaders = webResponse.getResponseHeaders(); assertThat(responseHeaders.size(), equalTo(3)); NameValuePair header = responseHeaders.get(0); assertThat(header.getName(), equalTo("Content-Type")); assertThat(header.getValue(), equalTo("text/html")); header = responseHeaders.get(1); assertThat(header.getName(), equalTo("X-Test")); assertThat(header.getValue(), equalTo("value")); header = responseHeaders.get(2); assertThat(header.getName(), equalTo("Set-Cookie")); assertThat(header.getValue(), startsWith("cookieA=valueA; Path=/path; Domain=domain; Max-Age=1800; Expires=")); assertThat(header.getValue(), endsWith("; Secure; HttpOnly")); }
/** * {@inheritDoc} * The default behavior of this method is to return getResponseHeaders() on the wrapped connection object. */ public List<NameValuePair> getResponseHeaders() { return wrappedWebResponse_.getResponseHeaders(); }
/** * {@inheritDoc} * The default behavior of this method is to return getResponseHeaders() on the wrapped webResponse object. */ @Override public List<NameValuePair> getResponseHeaders() { return wrappedWebResponse_.getResponseHeaders(); }
/** * {@inheritDoc} * The default behavior of this method is to return getResponseHeaders() on the wrapped connection object. */ public List<NameValuePair> getResponseHeaders() { return wrappedWebResponse_.getResponseHeaders(); }
/** * {@inheritDoc} * The default behavior of this method is to return getResponseHeaders() on the wrapped connection object. */ public List<NameValuePair> getResponseHeaders() { return wrappedWebResponse_.getResponseHeaders(); }
/** * {@inheritDoc} * The default behavior of this method is to return getResponseHeaders() on the wrapped webResponse object. */ @Override public List<NameValuePair> getResponseHeaders() { return wrappedWebResponse_.getResponseHeaders(); }
@Override public List<NameValuePair> getResponseHeaders() { return delegate.getResponseHeaders(); }
@Override @Deprecated public Map<String, String> getAllHeaders() { Map<String, String> map = new java.util.HashMap<String, String>(); for (NameValuePair header : getWebResponse().getResponseHeaders()) { map.put(header.getName(), header.getValue()); } return map; }
/** * Returns the content charset specified explicitly in the header or in the content, * or {@code null} if none was specified. * @return the content charset specified explicitly in the header or in the content, * or {@code null} if none was specified */ public Charset getContentCharsetOrNull() { try (InputStream is = getContentAsStream()) { return EncodingSniffer.sniffEncoding(getResponseHeaders(), is); } catch (final IOException e) { LOG.warn("Error trying to sniff encoding.", e); return null; } }
WebClient client = new WebClient(); HtmlPage page = client.getPage("http://www.example.com/"); WebResponse response = page.getWebResponse(); for (NameValuePair header : response.getResponseHeaders()) { System.out.println(header.getName() + " : " + header.getValue()); }
WebClient webClient = new WebClient(); WebClient.setThrowExceptionOnScriptError(false); HtmlPage currentPage = webClient.getPage("http://myapp.com"); WebResponse response = currentPage.getWebResponse(); System.out.println(response.getResponseHeaders()); // And even in subsequent requests currentPage = webClient.getPage("http://myapp.com"); response = currentPage.getWebResponse(); System.out.println(response.getResponseHeaders());
public void print(HtmlPage page) { System.out.println("\n\n\n RAW REQUEST BODY \n" + getRawRequestBody()); System.out.println("\n\n\n RAW RESPONSE \n" + getRawResponse()); System.out.println("Response Headers: " + page.getWebResponse().getResponseHeaders()); System.out.println("\n\n\n ********************************* \n" + page.asXml()); System.out.println("\n\n\n **************************** \n" + webClient.getCookieManager().getCookies()); System.out.println("\n\n\n *********************************"); }
@Override public List<HttpHeader> getResponseHeaders() { List<HttpHeader> result = new LinkedList<HttpHeader>(); for (NameValuePair header : getWebResponse().getResponseHeaders()) { result.add(new HttpHeader(header.getName(), header.getValue())); } return result; }
new WebConnectionWrapper(webClient) { public WebResponse getResponse(WebRequest request) throws IOException { WebResponse response = super.getResponse(request); if (request.getUrl().toExternalForm().contains("Inspection.aspx")) { String content = response.getContentAsString("UTF-8"); WebResponseData data = new WebResponseData(content.getBytes("UTF-8"), response.getStatusCode(), response.getStatusMessage(), response.getResponseHeaders()); response = new WebResponse(data, request, response.getLoadTime()); } return response; } };
new WebConnectionWrapper(webClient) { public WebResponse getResponse(WebRequest request) throws IOException { WebResponse response = super.getResponse(request); if (request.getUrl().toExternalForm().contains("my_url")) { String content = response.getContentAsString("UTF-8"); //change content WebResponseData data = new WebResponseData(content.getBytes("UTF-8"), response.getStatusCode(), response.getStatusMessage(), response.getResponseHeaders()); response = new WebResponse(data, request, response.getLoadTime()); } return response; } };
new WebConnectionWrapper(webClient) { public WebResponse getResponse(WebRequest request) throws IOException { WebResponse response = super.getResponse(request); if (request.getUrl().toExternalForm().contains("THE URL YOU WANT TO CHECK")) { String content = response.getContentAsString("UTF-8"); //CHECK CONTENT HERE WebResponseData data = new WebResponseData(content.getBytes("UTF-8"), response.getStatusCode(), response.getStatusMessage(), response.getResponseHeaders()); response = new WebResponse(data, request, response.getLoadTime()); } return response; } };
/** * Builds a WebResponse with new content, preserving all other information. * @param wr the web response to adapt * @param newContent the new content to place in the response * @return a web response with the new content * @throws IOException if an encoding problem occurred */ protected WebResponse replaceContent(final WebResponse wr, final String newContent) throws IOException { final byte[] body = newContent.getBytes(wr.getContentCharset()); final WebResponseData wrd = new WebResponseData(body, wr.getStatusCode(), wr.getStatusMessage(), wr.getResponseHeaders()); return new WebResponse(wrd, wr.getWebRequest().getUrl(), wr.getWebRequest().getHttpMethod(), wr.getLoadTime()); }
/** * Builds a WebResponse with new content, preserving all other information. * @param wr the web response to adapt * @param newContent the new content to place in the response * @return a web response with the new content * @throws IOException if an encoding problem occurred */ protected WebResponse replaceContent(final WebResponse wr, final String newContent) throws IOException { final byte[] body = newContent.getBytes(wr.getContentCharset()); final WebResponseData wrd = new WebResponseData(body, wr.getStatusCode(), wr.getStatusMessage(), wr.getResponseHeaders()); return new WebResponseImpl(wrd, wr.getRequestSettings().getUrl(), wr.getRequestSettings().getHttpMethod(), wr.getLoadTime()); }
/** * Builds a WebResponse with new content, preserving all other information. * @param wr the web response to adapt * @param newContent the new content to place in the response * @return a web response with the new content * @throws IOException if an encoding problem occurred */ protected WebResponse replaceContent(final WebResponse wr, final String newContent) throws IOException { final byte[] body = newContent.getBytes(wr.getContentCharset()); final WebResponseData wrd = new WebResponseData(body, wr.getStatusCode(), wr.getStatusMessage(), wr.getResponseHeaders()); return new WebResponseImpl(wrd, wr.getRequestSettings().getUrl(), wr.getRequestSettings().getHttpMethod(), wr.getLoadTime()); }