List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBody
public abstract byte[] getResponseBody() throws IOException;
From source file:net.sf.ehcache.constructs.web.filter.GzipFilterTest.java
/** * A 0 length body should give a 0 length gzip body and content length * <p/>//from w w w. j a v a2 s . c om * 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:9080/empty_gzip/empty.html */ public void testZeroLengthHTML() throws Exception { String url = "http://localhost:9080/empty_gzip/empty.html"; 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); byte[] responseBody = httpMethod.getResponseBody(); assertEquals(null, responseBody); assertNull(httpMethod.getResponseHeader("Content-Encoding")); checkNullOrZeroContentLength(httpMethod); }
From source file:net.sf.ehcache.constructs.web.filter.GzipFilterTest.java
/** * JSPs and Servlets can send bodies when the response is SC_NOT_MODIFIED. * 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.// w w w. j a v a2s.co m * <p/> * Manual test: wget -d --server-response --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9080/empty_gzip/SC_NOT_MODIFIED.jsp */ public void testNotModifiedJSPGzipFilter() throws Exception { String url = "http://localhost:9080/empty_gzip/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); byte[] responseBody = httpMethod.getResponseBody(); assertEquals(null, responseBody); assertNull(httpMethod.getResponseHeader("Content-Encoding")); assertNotNull(httpMethod.getResponseHeader("Last-Modified").getValue()); checkNullOrZeroContentLength(httpMethod); }
From source file:com.trunghoang.teammedical.utils.FileDownloader.java
public File urlDownloader(String downloadLocation) throws Exception { URL downloadURL = new URL(downloadLocation); HttpClient client = new HttpClient(); client.getParams().setCookiePolicy(CookiePolicy.RFC_2965); client.setHostConfiguration(mimicHostConfiguration(downloadURL.getHost(), downloadURL.getPort())); client.getParams().makeLenient();/*from ww w.j a va2s . c om*/ client.setState(mimicCookieState(driver.manage().getCookies())); HttpMethod getRequest = new GetMethod(downloadLocation); getRequest.setFollowRedirects(true); FileHandler downloadedFile = new FileHandler( downloadPath + downloadURL.getFile().replaceFirst("/|\\\\", ""), true); try { int status = client.executeMethod(getRequest); log.info("HTTP Status {} when getting '{}'", status, downloadURL.toExternalForm()); downloadedFile.getWritableFileOutputStream().write(getRequest.getResponseBody()); downloadedFile.close(); log.info("File downloaded to '{}'", downloadedFile.getAbsoluteFile()); } catch (Exception Ex) { log.error("Download failed: {}", Ex); throw new Exception("Download failed!"); } finally { getRequest.releaseConnection(); } return downloadedFile.getFile(); }
From source file:com.alibaba.intl.bcds.goldroom.remote.DoubanBookInfoFetcher.java
/** * @param isbn//from ww w. ja v a2 s . c o m * @param imageUrl * @return */ private String saveImage(String isbn, String imageUrl) { String largeImageUrl = StringUtils.replaceOnce(imageUrl, "s", "l"); HttpMethod method = new GetMethod(largeImageUrl); HttpClient client = new HttpClient(); try { // ??????api????douban????~~~ client.executeMethod(method); if (method.getStatusCode() != 200) { method = new GetMethod(imageUrl); client.executeMethod(method); } byte[] imgBody = method.getResponseBody(); return imageUtil.save(isbn, getImageSuffix(imageUrl), imgBody); } catch (Exception e) { logger.warn("error occurred while save image from url :" + imageUrl + " for isbn " + isbn, e); } return null; }
From source file:br.com.edu.dbpediaspotlight.AnnotationClient.java
public String request(HttpMethod method) throws AnnotationException { String response = null;//from ww w .j a v a2 s .c o m // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { LOG.error("Method failed: " + method.getStatusLine()); } // Read the response body. byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended. // Deal with the response. // Use caution: ensure correct character encoding and is not binary data response = new String(responseBody); } catch (HttpException e) { LOG.error("Fatal protocol violation: " + e.getMessage()); throw new AnnotationException("Protocol error executing HTTP request.", e); } catch (IOException e) { LOG.error("Fatal transport error: " + e.getMessage()); LOG.error(method.getQueryString()); throw new AnnotationException("Transport error executing HTTP request.", e); } finally { // Release the connection. method.releaseConnection(); } return response; }
From source file:net.sf.ehcache.constructs.web.filter.CachingFilterTest.java
/** * Tests whether the page is gzipped using the rawer HttpClient library. * Lets us check that the responseBody is really gzipped. *//*w w w .j ava 2s. c o m*/ public void testCachedPageIsGzippedWhenEncodingHeaderSet() throws IOException { HttpClient httpClient = new HttpClient(); HttpMethod httpMethod = new GetMethod(buildUrl(cachedPageUrl)); httpMethod.setRequestHeader(new Header("Accept-encoding", "gzip")); httpClient.executeMethod(httpMethod); byte[] responseBody = httpMethod.getResponseBody(); assertTrue(PageInfo.isGzipped(responseBody)); }
From source file:net.sourceforge.jwbf.actions.HttpActionClient.java
/** * Process a GET Message.//from w w w . j a v a 2 s .co m * * @param authgets * a * @param cp * a * @return a returning message, not null * @throws IOException on problems * @throws CookieException on problems * @throws ProcessException on problems */ public byte[] get(HttpMethod authgets) throws IOException, CookieException, ProcessException { showCookies(client); byte[] out = null; authgets.getParams().setParameter("http.protocol.content-charset", MediaWikiBot.CHARSET); // System.err.println(authgets.getParams().getParameter("http.protocol.content-charset")); client.executeMethod(authgets); LOG.debug(authgets.getURI()); LOG.debug("GET: " + authgets.getStatusLine().toString()); out = authgets.getResponseBody(); // release any connection resources used by the method authgets.releaseConnection(); int statuscode = authgets.getStatusCode(); if (statuscode == HttpStatus.SC_NOT_FOUND) { LOG.warn("Not Found: " + authgets.getQueryString()); throw new FileNotFoundException(authgets.getQueryString()); } return out; }
From source file:net.sf.ehcache.constructs.web.filter.CachingFilterTest.java
/** * Tests whether the page is gzipped using the rawer HttpClient library. * Lets us check that the responseBody is really not gzipped. *//*from w w w. j a v a 2 s .c o m*/ public void testCachedPageIsNotGzippedWhenEncodingHeaderNotSet() throws IOException { HttpClient httpClient = new HttpClient(); HttpMethod httpMethod = new GetMethod(buildUrl(cachedPageUrl)); //httpMethod.setRequestHeader(new Header("Accept-encoding", "gzip")); httpClient.executeMethod(httpMethod); byte[] responseBody = httpMethod.getResponseBody(); assertFalse(PageInfo.isGzipped(responseBody)); }
From source file:AnnotationClient.java
public String request(HttpMethod method) throws AnnotationException { String response = null;/* w w w .j a va2 s . co m*/ // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { LOG.error("Method failed: " + method.getStatusLine()); } // Read the response body. byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended. // Deal with the response. // Use caution: ensure correct character encoding and is not binary data response = new String(responseBody); } catch (HttpException e) { LOG.error("Fatal protocol violation: " + e.getMessage()); throw new AnnotationException("Protocol error executing HTTP request.", e); } catch (IOException e) { LOG.error("Fatal transport error: " + e.getMessage()); LOG.error(method.getQueryString()); throw new AnnotationException("Transport error executing HTTP request.", e); } finally { // Release the connection. method.releaseConnection(); } return response; }
From source file:it.infn.ct.aleph_portlet.java
public static int getNumRec(String date, int jrec, int num_rec) { HttpClient client = new HttpClient(); HttpMethod method = callAPIOAR(date, jrec, num_rec); double numRec = 0; int numFor = 0; String responseXML = null;/* ww w .ja v a2s. c om*/ BufferedReader br = null; try { client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { method.getResponseBody(); responseXML = convertStreamToString(method.getResponseBodyAsStream()); numRec = Double.parseDouble(responseXML.split("Results:")[1].split("-->")[0].replace(" ", "")); System.out.println("NUM REC=>" + numRec / 100); numFor = (int) Math.ceil(numRec / 100); System.out.println("NUM REC=>" + numFor); method.releaseConnection(); } } catch (IOException ex) { Logger.getLogger(aleph_portlet.class.getName()).log(Level.SEVERE, null, ex); } return numFor; }