List of usage examples for org.apache.commons.httpclient HttpMethod getResponseHeader
public abstract Header getResponseHeader(String paramString);
From source file:net.sf.ehcache.constructs.web.filter.SimpleCachingHeadersPageCachingFilterTest.java
/** * HEAD methods return an empty response body. If a HEAD request populates * a cache and then a GET follorws, a blank page will result. * This test ensures that the SimplePageCachingFilter implements calculateKey * properly to avoid this problem./*from www . j av a 2 s. c om*/ */ @Test public void testHeadThenGetOnCachedPage() throws Exception { HttpClient httpClient = new HttpClient(); HttpMethod httpMethod = new HeadMethod(buildUrl(cachedPageUrl)); int responseCode = httpClient.executeMethod(httpMethod); //httpclient follows redirects, so gets the home page. assertEquals(HttpURLConnection.HTTP_OK, responseCode); String responseBody = httpMethod.getResponseBodyAsString(); assertNull(responseBody); assertNull(httpMethod.getResponseHeader("Content-Encoding")); httpMethod = new GetMethod(buildUrl(cachedPageUrl)); responseCode = httpClient.executeMethod(httpMethod); responseBody = httpMethod.getResponseBodyAsString(); assertNotNull(responseBody); }
From source file:com.yahoo.flowetl.services.http.BaseHttpCaller.java
/** * Attempts to call the given method using the given client and will attempt * this repeatedly up to the max redirect amount. If no redirect location is * found a http exception will be propagated upwards. Otherwise for * non-redirect codes this method will stop. This function is recursively * called.//from ww w . j a v a2 s. c om * * @throws HttpException * @throws IOException */ private void handleRedirects(final HttpClient client, final HttpMethod method, final int curRedirAm, final int maxRedirAm) throws HttpException, IOException { if (logger.isEnabled(Level.DEBUG)) { logger.log(Level.DEBUG, "Executing " + method + " redir count = " + curRedirAm + " of " + maxRedirAm + " possible redirects "); } // exec and see what happened client.executeMethod(method); int code = method.getStatusCode(); if (logger.isEnabled(Level.DEBUG)) { logger.log(Level.DEBUG, "Executing " + method + " got status code " + code + ""); } // supposed redirect codes // everything else will just stop this function if (REDIR_CODES.contains(code) == false) { return; } // die or continue? if (curRedirAm < maxRedirAm) { // ok to try to find it Header locationHeader = method.getResponseHeader(REDIR_HEADER); String redirLoc = null; if (locationHeader != null) { redirLoc = locationHeader.getValue(); } // cleanup and see if we can use it... redirLoc = StringUtils.trim(redirLoc); if (StringUtils.isEmpty(redirLoc) == false) { // reset uri URI nUri = new URI(redirLoc, false); method.setURI(nUri); if (logger.isEnabled(Level.DEBUG)) { logger.log(Level.DEBUG, "Attempting redirect " + (curRedirAm + 1) + " due to status code " + code + " to location " + nUri); } handleRedirects(client, method, curRedirAm + 1, maxRedirAm); } else { // failure at finding header throw new HttpException("Unable to execute " + method + " - no " + REDIR_HEADER + " header found to redirect to during redirect " + curRedirAm); } } else { // max redirects done throw new HttpException("Unable to execute " + method + " after attempting " + curRedirAm + " redirects of " + maxRedirAm + " attempts"); } }
From source file:net.sf.ehcache.constructs.web.filter.SimpleCachingHeadersPageCachingFilterTest.java
/** * When the servlet container generates a 404 page not found, we want to pass * it through without caching and without adding anything to it. * <p/>/*from ww w. ja va2 s . c om*/ * Manual Test: wget -d --server-response --header='Accept-Encoding: gzip' http://localhost:9090/non_ok/SendRedirect.jsp */ @Test public void testRedirect() throws Exception { String url = buildUrl("/non_ok/SendRedirect.jsp"); HttpClient httpClient = new HttpClient(); HttpMethod httpMethod = new GetMethod(url); httpMethod.addRequestHeader("Accept-Encoding", "gzip"); int responseCode = httpClient.executeMethod(httpMethod); //httpclient follows redirects, so gets the home page. assertEquals(HttpURLConnection.HTTP_OK, responseCode); String responseBody = httpMethod.getResponseBodyAsString(); assertNotNull(responseBody); assertNull(httpMethod.getResponseHeader("Content-Encoding")); }
From source file:net.sf.ehcache.constructs.web.filter.SimpleCachingHeadersPageCachingFilterTest.java
/** * When the servlet container generates a 404 page not found, we want to pass * it through without caching and without adding anything to it. * <p/>//from w ww . ja va2s .com * Manual Test: wget -d --server-response --header='Accept-Encoding: gzip' http://localhost:9090/non_ok/PageNotFound.jsp */ @Test public void testNotFound() throws Exception { String url = buildUrl("/non_ok/PageNotFound.jsp"); HttpClient httpClient = new HttpClient(); HttpMethod httpMethod = new GetMethod(url); httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT"); httpMethod.addRequestHeader("Accept-Encoding", "gzip"); int responseCode = httpClient.executeMethod(httpMethod); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, responseCode); String responseBody = httpMethod.getResponseBodyAsString(); assertNotNull(responseBody); assertNull(httpMethod.getResponseHeader("Content-Encoding")); }
From source file:net.sf.ehcache.constructs.web.filter.SimpleCachingHeadersPageCachingFilterTest.java
/** * Servlets and JSPs can send content even when the response is set to no content. * In this case there should not be a body but there is. Orion seems to kill the body * after is has left the Servlet filter chain. To avoid wget going into an inifinite * retry loop, and presumably some other web clients, the content length should be 0 * and the body 0./*from w w w .j a v a 2s .co m*/ * <p/> * Manual Test: wget -d --server-response --timestamping --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9090/empty_caching_filter/SC_NO_CONTENT.jsp */ @Test public void testNoContentJSPGzipFilter() throws Exception { String url = buildUrl("/empty_caching_filter/SC_NO_CONTENT.jsp"); HttpClient httpClient = new HttpClient(); HttpMethod httpMethod = new GetMethod(url); httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT"); httpMethod.addRequestHeader("Accept-Encoding", "gzip"); int responseCode = httpClient.executeMethod(httpMethod); assertEquals(HttpURLConnection.HTTP_NO_CONTENT, responseCode); String responseBody = httpMethod.getResponseBodyAsString(); assertEquals(null, responseBody); assertNull(httpMethod.getResponseHeader("Content-Encoding")); assertNotNull(httpMethod.getResponseHeader("Last-Modified").getValue()); checkNullOrZeroContentLength(httpMethod); }
From source file:net.sf.ehcache.constructs.web.filter.SimpleCachingHeadersPageCachingFilterTest.java
/** * Servlets and JSPs can send content even when the response is set to no content. * In this case there should not be a body but there is. Orion seems to kill the body * after is has left the Servlet filter chain. To avoid wget going into an inifinite * retry loop, and presumably some other web clients, the content length should be 0 * and the body 0./*from w w w. ja va 2 s.co m*/ * <p/> * wget -d --server-response --timestamping --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9090/empty_caching_filter/SC_NOT_MODIFIED.jsp */ @Test public void testNotModifiedJSPGzipFilter() throws Exception { String url = buildUrl("/empty_caching_filter/SC_NOT_MODIFIED.jsp"); HttpClient httpClient = new HttpClient(); HttpMethod httpMethod = new GetMethod(url); httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT"); httpMethod.addRequestHeader("Accept-Encoding", "gzip"); int responseCode = httpClient.executeMethod(httpMethod); assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, responseCode); String responseBody = httpMethod.getResponseBodyAsString(); assertEquals(null, responseBody); assertNull(httpMethod.getResponseHeader("Content-Encoding")); assertNotNull(httpMethod.getResponseHeader("Last-Modified").getValue()); checkNullOrZeroContentLength(httpMethod); }
From source file:net.sourceforge.jwbf.actions.HttpActionClient.java
/** * Process a POST Message./*from w w w . j av a 2 s . c o m*/ * * @param authpost * a * @param cp * a * @return a returning message, not null * @throws IOException on problems * @throws ProcessException on problems * @throws CookieException on problems */ protected String post(HttpMethod authpost, ContentProcessable cp) throws IOException, ProcessException, CookieException { showCookies(client); authpost.getParams().setParameter("http.protocol.content-charset", MediaWikiBot.CHARSET); String out = ""; client.executeMethod(authpost); // Header locationHeader = authpost.getResponseHeader("location"); // if (locationHeader != null) { // authpost.setRequestHeader(locationHeader) ; // } // Usually a successful form-based login results in a redicrect to // another url int statuscode = authpost.getStatusCode(); if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) || (statuscode == HttpStatus.SC_SEE_OTHER) || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) { Header header = authpost.getResponseHeader("location"); if (header != null) { String newuri = header.getValue(); if ((newuri == null) || (newuri.equals(""))) { newuri = "/"; } LOG.debug("Redirect target: " + newuri); GetMethod redirect = new GetMethod(newuri); client.executeMethod(redirect); LOG.debug("Redirect: " + redirect.getStatusLine().toString()); // release any connection resources used by the method authpost.releaseConnection(); authpost = redirect; } } out = authpost.getResponseBodyAsString(); out = cp.processReturningText(out, authpost); cp.validateReturningCookies(client.getState().getCookies(), authpost); authpost.releaseConnection(); LOG.debug(authpost.getURI() + " || " + "POST: " + authpost.getStatusLine().toString()); return out; }
From source file:cn.leancloud.diamond.client.impl.DefaultDiamondSubscriber.java
/** * ?/*from w w w . ja v a 2 s.c o m*/ * * @param httpMethod * @return */ boolean isZipContent(HttpMethod httpMethod) { if (null != httpMethod.getResponseHeader(Constants.CONTENT_ENCODING)) { String acceptEncoding = httpMethod.getResponseHeader(Constants.CONTENT_ENCODING).getValue(); if (acceptEncoding.toLowerCase().indexOf("gzip") > -1) { return true; } } return false; }
From source file:com.owncloud.android.oc_framework.network.webdav.WebdavClient.java
@Override public int executeMethod(HttpMethod method) throws IOException, HttpException { boolean customRedirectionNeeded = false; try {//from www . ja va 2s . c om method.setFollowRedirects(mFollowRedirects); } catch (Exception e) { //if (mFollowRedirects) Log_OC.d(TAG, "setFollowRedirects failed for " + method.getName() + " method, custom redirection will be used if needed"); customRedirectionNeeded = mFollowRedirects; } if (mSsoSessionCookie != null && mSsoSessionCookie.length() > 0) { method.setRequestHeader("Cookie", mSsoSessionCookie); } int status = super.executeMethod(method); int redirectionsCount = 0; while (customRedirectionNeeded && redirectionsCount < MAX_REDIRECTIONS_COUNT && (status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY || status == HttpStatus.SC_TEMPORARY_REDIRECT)) { Header location = method.getResponseHeader("Location"); if (location != null) { Log.d(TAG, "Location to redirect: " + location.getValue()); method.setURI(new URI(location.getValue(), true)); status = super.executeMethod(method); redirectionsCount++; } else { Log.d(TAG, "No location to redirect!"); status = HttpStatus.SC_NOT_FOUND; } } return status; }
From source file:com.ideabase.repository.webservice.client.impl.HttpWebServiceControllerImpl.java
/** * {@inheritDoc}//from ww w . j a v a2 s.c o m */ public WebServiceResponse sendServiceRequest(final WebServiceRequest pRequest) { if (LOG.isDebugEnabled()) { LOG.debug("Sending request - " + pRequest); } // build query string final NameValuePair[] parameters = buildParameters(pRequest); // Send request to server final HttpMethod httpMethod = prepareMethod(pRequest.getRequestMethod(), pRequest.getServiceUri(), parameters); // set cookie policy httpMethod.getParams().setCookiePolicy(CookiePolicy.RFC_2109); // set cookies if (mCookies != null) { httpMethod.setRequestHeader(HEADER_COOKIE, mCookies); } if (pRequest.getCookies() != null) { httpMethod.setRequestHeader(HEADER_COOKIE, pRequest.getCookies()); mCookies = pRequest.getCookies(); } // execute method final HttpClient httpClient = new HttpClient(); final WebServiceResponse response; try { final int status = httpClient.executeMethod(httpMethod); // build web sercie response response = new WebServiceResponse(); response.setResponseStatus(status); response.setResponseContent(new String(httpMethod.getResponseBody())); response.setContentType(httpMethod.getResponseHeader(HEADER_CONTENT_TYPE).getValue()); response.setServiceUri(httpMethod.getURI().toString()); // set cookies final Header cookieHeader = httpMethod.getResponseHeader(HEADER_SET_COOKIE); if (cookieHeader != null) { mCookies = cookieHeader.getValue(); } // set cookies to the returning response object. response.setCookies(mCookies); if (LOG.isDebugEnabled()) { LOG.debug("Cookies - " + mCookies); LOG.debug("Response - " + response); } } catch (Exception e) { throw ServiceException.aNew(pRequest, "Failed to send web service request.", e); } finally { httpMethod.releaseConnection(); } return response; }