List of usage examples for org.apache.commons.httpclient.methods GetMethod GetMethod
public GetMethod()
From source file:GetMethodExample.java
public static void main(String args[]) { HttpClient client = new HttpClient(); client.getParams().setParameter("http.useragent", "Test Client"); client.getParams().setParameter("http.connection.timeout", new Integer(5000)); GetMethod method = new GetMethod(); FileOutputStream fos = null;// w w w . ja v a 2 s.c o m try { method.setURI(new URI("http://www.google.com", true)); int returnCode = client.executeMethod(method); if (returnCode != HttpStatus.SC_OK) { System.err.println("Unable to fetch default page, status code: " + returnCode); } System.err.println(method.getResponseBodyAsString()); method.setURI(new URI("http://www.google.com/images/logo.gif", true)); returnCode = client.executeMethod(method); if (returnCode != HttpStatus.SC_OK) { System.err.println("Unable to fetch image, status code: " + returnCode); } byte[] imageData = method.getResponseBody(); fos = new FileOutputStream(new File("google.gif")); fos.write(imageData); HostConfiguration hostConfig = new HostConfiguration(); hostConfig.setHost("www.yahoo.com", null, 80, Protocol.getProtocol("http")); method.setURI(new URI("/", true)); client.executeMethod(hostConfig, method); System.err.println(method.getResponseBodyAsString()); } catch (HttpException he) { System.err.println(he); } catch (IOException ie) { System.err.println(ie); } finally { method.releaseConnection(); if (fos != null) try { fos.close(); } catch (Exception fe) { } } }
From source file:com.carrotsearch.util.httpclient.HttpUtils.java
/** * Opens a HTTP/1.1 connection to the given URL using the GET method, decompresses * compressed response streams, if supported by the server. * //from w ww .j a v a 2s .co m * @param url The URL to open. The URL must be properly escaped, this method will * <b>not</b> perform any escaping. * @param params Query string parameters to be attached to the url. * @param headers Any extra HTTP headers to add to the request. * @return The {@link HttpUtils.Response} object. Note that entire payload is read and * buffered so that the HTTP connection can be closed when leaving this * method. */ public static Response doGET(String url, Collection<NameValuePair> params, Collection<Header> headers) throws HttpException, IOException { final HttpClient client = HttpClientFactory.getTimeoutingClient(); client.getParams().setVersion(HttpVersion.HTTP_1_1); final GetMethod request = new GetMethod(); final Response response = new Response(); try { request.setURI(new URI(url, true)); if (params != null) { request.setQueryString(params.toArray(new NameValuePair[params.size()])); } request.setRequestHeader(HttpHeaders.URL_ENCODED); request.setRequestHeader(HttpHeaders.GZIP_ENCODING); if (headers != null) { for (Header header : headers) request.setRequestHeader(header); } Logger.getLogger(HttpUtils.class).debug("GET: " + request.getURI()); final int statusCode = client.executeMethod(request); response.status = statusCode; InputStream stream = request.getResponseBodyAsStream(); final Header encoded = request.getResponseHeader("Content-Encoding"); if (encoded != null && "gzip".equalsIgnoreCase(encoded.getValue())) { stream = new GZIPInputStream(stream); response.compression = COMPRESSION_GZIP; } else { response.compression = COMPRESSION_NONE; } final Header[] respHeaders = request.getResponseHeaders(); response.headers = new String[respHeaders.length][]; for (int i = 0; i < respHeaders.length; i++) { response.headers[i] = new String[] { respHeaders[i].getName(), respHeaders[i].getValue() }; } response.payload = StreamUtils.readFullyAndClose(stream); return response; } finally { request.releaseConnection(); } }
From source file:com.basho.riak.client.response.TestDefaultHttpResponse.java
@Test public void status_2xx_300_and_304_success_for_get() { GetMethod get = new GetMethod(); for (int i = 200; i < 300; i++) { impl = new DefaultHttpResponse(null, null, i, null, null, null, get); assertTrue(impl.isSuccess());//from w w w .j ava2 s . c om } impl = new DefaultHttpResponse(null, null, 300, null, null, null, get); assertTrue(impl.isSuccess()); impl = new DefaultHttpResponse(null, null, 304, null, null, null, get); assertTrue(impl.isSuccess()); }
From source file:net.sf.j2ep.test.AllowHeaderTest.java
public void testSetAllowed() { String allow = "OPTIONS,PROPFIND,OP,PUT"; AllowedMethodHandler.setAllowedMethods(allow); assertEquals("Checking that allow is set", allow, AllowedMethodHandler.getAllowHeader()); assertTrue("Checking that OPTIONS is allowed", AllowedMethodHandler.methodAllowed("OPTIONS")); assertTrue("Checking that PROPFIND is allowed", AllowedMethodHandler.methodAllowed("PROPFIND")); assertTrue("Checking that OP is allowed", AllowedMethodHandler.methodAllowed("OP")); assertTrue("Checking that PUT is allowed", AllowedMethodHandler.methodAllowed("PUT")); assertFalse("Checking that PROP isn't allowed", AllowedMethodHandler.methodAllowed("PROP")); assertTrue("Checking OPTIONS method", AllowedMethodHandler.methodAllowed(new OptionsMethod())); assertFalse("Checking GET method", AllowedMethodHandler.methodAllowed(new GetMethod())); }
From source file:com.braindrainpain.docker.httpsupport.HttpClientServiceIntegrationTest.java
@Before public void setUp() { super.setUp(); httpClient = new HttpClient(); getMethod = new GetMethod(); httpClientService = new HttpClientService(httpClient, getMethod); }
From source file:ar.com.zauber.commons.web.proxy.HttpClientRequestProxyTest.java
/** test */ public final void testNoEncoding() { HttpClientRequestProxy p = new HttpClientRequestProxy(new URLRequestMapper() { public final URLResult getProxiedURLFromRequest(final HttpServletRequest request) { return null; }//from w w w . j a v a2s .c o m }, new HttpClient()); HttpMethod m = new GetMethod() { /** @see HttpMethodBase#getResponseHeader(String) */ @Override public Header getResponseHeader(final String headerName) { return null; } }; assertNull(p.getContentType(m)); }
From source file:com.braindrainpain.docker.httpsupport.HttpClientService.java
public HttpClientService() { httpClient = createHttpClient(); getMethod = new GetMethod(); }
From source file:gov.nih.nci.cabio.portal.portlet.RESTProxyServletController.java
@Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { try {//w w w . j a va 2s. com String relativeURL = request.getParameter("relativeURL"); if (relativeURL == null) { log.error("Null relativeURL parameter"); return null; } String qs = request.getQueryString(); if (qs == null) { qs = ""; } qs = qs.replaceFirst("relativeURL=(.*?)&", ""); qs = URLDecoder.decode(qs, "UTF-8"); String url = caBIORestURL + relativeURL + "?" + qs; log.info("Proxying URL: " + url); URI uri = new URI(url, false); HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(); method.setURI(uri); client.executeMethod(method); response.setContentType(method.getResponseHeader("Content-Type").getValue()); CopyUtils.copy(method.getResponseBodyAsStream(), response.getOutputStream()); } catch (Exception e) { throw new ServletException("Unable to connect to caBIO", e); } return null; }
From source file:ar.com.zauber.commons.web.proxy.HttpClientRequestProxyTest.java
/** test */ public final void testContentTypeCharset() { HttpClientRequestProxy p = new HttpClientRequestProxy(new URLRequestMapper() { public final URLResult getProxiedURLFromRequest(final HttpServletRequest request) { return null; }/*from w w w. ja va2 s .com*/ }, new HttpClient()); HttpMethod m = new GetMethod() { /** @see HttpMethodBase#getResponseHeader(String) */ @Override public Header getResponseHeader(final String headerName) { return new Header(headerName, "text/html ; charset = utf-8"); } }; assertEquals("text/html ; charset = utf-8", p.getContentType(m)); }
From source file:com.datos.vfs.provider.http.HttpRandomAccessContent.java
@Override protected DataInputStream getDataInputStream() throws IOException { if (dis != null) { return dis; }/*from www .jav a 2s . c o m*/ final GetMethod getMethod = new GetMethod(); fileObject.setupMethod(getMethod); getMethod.setRequestHeader("Range", "bytes=" + filePointer + "-"); final int status = fileSystem.getClient().executeMethod(getMethod); if (status != HttpURLConnection.HTTP_PARTIAL && status != HttpURLConnection.HTTP_OK) { throw new FileSystemException("vfs.provider.http/get-range.error", fileObject.getName(), Long.valueOf(filePointer), Integer.valueOf(status)); } mis = new HttpFileObject.HttpInputStream(getMethod); // If the range request was ignored if (status == HttpURLConnection.HTTP_OK) { final long skipped = mis.skip(filePointer); if (skipped != filePointer) { throw new FileSystemException("vfs.provider.http/get-range.error", fileObject.getName(), Long.valueOf(filePointer), Integer.valueOf(status)); } } dis = new DataInputStream(new FilterInputStream(mis) { @Override public int read() throws IOException { final int ret = super.read(); if (ret > -1) { filePointer++; } return ret; } @Override public int read(final byte[] b) throws IOException { final int ret = super.read(b); if (ret > -1) { filePointer += ret; } return ret; } @Override public int read(final byte[] b, final int off, final int len) throws IOException { final int ret = super.read(b, off, len); if (ret > -1) { filePointer += ret; } return ret; } }); return dis; }