break; case HEAD: http = new HeadMethod(); break; case GET:
public static void checkUrlExistence(String url) { if (url.toLowerCase().startsWith("http") || url.toLowerCase().startsWith("https")) { HttpClient httpClient = getHttpClient(); HeadMethod httphead = new HeadMethod(url); try { if (httpClient.executeMethod(httphead) != HttpStatus.SC_OK) { throw new IllegalArgumentException("Invalid URL: " + url); } if (url.endsWith("metalink") && !checkUrlExistenceMetalink(url)) { throw new IllegalArgumentException("Invalid URLs defined on metalink: " + url); } } catch (HttpException hte) { throw new IllegalArgumentException("Cannot reach URL: " + url + " due to: " + hte.getMessage()); } catch (IOException ioe) { throw new IllegalArgumentException("Cannot reach URL: " + url + " due to: " + ioe.getMessage()); } finally { httphead.releaseConnection(); } } }
/** * No-arg constructor. * * @since 1.0 */ public HeadMethod() { setFollowRedirects(true); }
public String getCharset() throws DereferenceException { if (!url.getProtocol().equals("http")) { return null; // Don't know } HeadMethod method = new HeadMethod(url.toExternalForm()); HttpClient httpClient = new HttpClient(); try { httpClient.executeMethod(method); return method.getResponseCharSet(); } catch (HttpException e) { throw new DereferenceException(e); } catch (IOException e) { throw new DereferenceException(e); } finally { method.releaseConnection(); } } }
/** {@inheritDoc} */ public boolean exists() throws ResourceException { HeadMethod headMethod = new HeadMethod(resourceUrl); headMethod.addRequestHeader("Connection", "close"); try { httpClient.executeMethod(headMethod); if (headMethod.getStatusCode() != HttpStatus.SC_OK) { return false; } return true; } catch (IOException e) { throw new ResourceException("Unable to contact resource URL: " + resourceUrl, e); } finally { headMethod.releaseConnection(); } }
/** Determine the value for the Content-Length header using a HEAD call * * @param url * @return * @throws HttpException */ public static int getContentLength(String url) throws HttpException, IOException { int result = 0; //Execute a GET method on the URL HeadMethod head = new HeadMethod(url); head.setFollowRedirects(true); head.addRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 4.0)"); try { _httpClient.executeMethod(head); // Retrieve just the Content-Length header value. String length = head.getResponseHeader("Content-Length").getValue(); result = Integer.parseInt(length); } catch (NumberFormatException nfe) { //header not found } finally { head.releaseConnection(); } return result; }
HeadMethod head = new HeadMethod("http://jakarta.apache.org"); // execute the method and handle any error responses. ... // Retrieve all the headers. Header[] headers = head.getResponseHeaders(); // Retrieve just the last modified header value. String lastModified = head.getResponseHeader("last-modified").getValue();
HeadMethod head = new HeadMethod("http://www.myfootestsite.com"); head.setFollowRedirects(true); // Header stuff Header[] headers = head.getResponseHeaders();
/** * Send a HEAD request * @param cluster the cluster definition * @param path the path or URI * @param headers the HTTP headers to include in the request * @return a Response object with response detail * @throws IOException */ public Response head(Cluster cluster, String path, Header[] headers) throws IOException { HeadMethod method = new HeadMethod(); try { int code = execute(cluster, method, null, path); headers = method.getResponseHeaders(); return new Response(code, headers, null); } finally { method.releaseConnection(); } }
HeadMethod head = new HeadMethod("http://jakarta.apache.org"); // execute the method and handle any error responses. ... // Retrieve all the headers. Header[] headers = head.getResponseHeaders();
HeadMethod headMethod = new HeadMethod(URIUtil.encodePath(getHref())); try { httpClient.executeMethod(headMethod); if (headMethod.getResponseHeader("ETag") != null) { itemResult.etag = headMethod.getResponseHeader("ETag").getValue(); headMethod.releaseConnection();
/** * Check if a file exists in the OC server * * @deprecated Use ExistenceCheckOperation instead * * @return 'true' if the file exists; 'false' it doesn't exist * @throws Exception When the existence could not be determined */ @Deprecated public boolean existsFile(String path) throws IOException { HeadMethod head = new HeadMethod(getWebdavUri() + WebdavUtils.encodePath(path)); try { int status = executeMethod(head); Log_OC.d(TAG, "HEAD to " + path + " finished with HTTP status " + status + ((status != HttpStatus.SC_OK)?"(FAIL)":"")); exhaustResponse(head.getResponseBodyAsStream()); return (status == HttpStatus.SC_OK); } finally { head.releaseConnection(); // let the connection available for other methods } }
boolean previousFollowRedirects = client.getFollowRedirects(); try { head = new HeadMethod(client.getWebdavUri() + WebdavUtils.encodePath(mPath)); client.setFollowRedirects(false); int status = client.executeMethod(head, TIMEOUT, TIMEOUT); status = mRedirectionPath.getLastStatus(); client.exhaustResponse(head.getResponseBodyAsStream()); boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent) || (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent); success, status, head.getStatusText(), head.getResponseHeaders() ); Log_OC.d(TAG, "Existence check for " + client.getWebdavUri() + head.releaseConnection(); client.setFollowRedirects(previousFollowRedirects);
private HttpMethod getHeadMethod(Protocol protocol) throws Exception { HeadMethod head = new HeadMethod(protocol.getTarget()); head.setPath(target.toString()); for(String name : header) { head.addRequestHeader(name, header.get(name)); } return head; }
HeadMethod headMethod = new HeadMethod(absoluteAttachmentUrl); int status = httpClient.executeMethod(headMethod); if (status == 200) { Header attachmentSizeHeader = headMethod.getResponseHeader("Content-Length"); if (attachmentSizeHeader != null) { attachmentSize = attachmentSizeHeader.getValue(); Header attachmentTypeHeader = headMethod.getResponseHeader("Content-Type"); if (attachmentTypeHeader != null) { attachmentType = attachmentTypeHeader.getValue();
private HeadMethod doHead(URL url, int timeout) throws IOException { HttpClient client = getClient(); client.setTimeout(timeout); HeadMethod head = new HeadMethod(normalizeToString(url)); head.setDoAuthentication(useAuthentication(url) || useProxyAuthentication()); client.executeMethod(head); return head; }
@Override public FileContentInfo create(final FileContent fileContent) throws FileSystemException { final HttpFileObject httpFile = (HttpFileObject) FileObjectUtils.getAbstractFileObject(fileContent.getFile()); String contentType = null; String contentEncoding = null; HeadMethod headMethod; try { headMethod = httpFile.getHeadMethod(); } catch (final IOException e) { throw new FileSystemException(e); } final Header header = headMethod.getResponseHeader("content-type"); if (header != null) { final HeaderElement[] element = header.getElements(); if (element != null && element.length > 0) { contentType = element[0].getName(); } } contentEncoding = headMethod.getResponseCharSet(); return new DefaultFileContentInfo(contentType, contentEncoding); } }
/** * Returns the size of the file content (in bytes). */ @Override protected long doGetContentSize() throws Exception { final Header header = method.getResponseHeader("content-length"); if (header == null) { // Assume 0 content-length return 0; } return Long.parseLong(header.getValue()); }
@Override public String getCharset() throws DereferenceException { if (charsetFetched) return charsetName; charsetFetched = true; if (!httpUrl.getProtocol().equals("http") && !httpUrl.getProtocol().equals("https")) { charsetName = null; return null; } HeadMethod method = new HeadMethod(httpUrl.toExternalForm()); HttpClient httpClient = new HttpClient(); try { httpClient.executeMethod(method); charsetName = method.getResponseCharSet(); return charsetName; } catch (HttpException e) { // throw new DereferenceException(e); } catch (IOException e) { // throw new DereferenceException(e); } finally { method.releaseConnection(); } charsetName = null; return null; }
/** {@inheritDoc} */ public DateTime getLastModifiedTime() throws ResourceException { HeadMethod headMethod = new HeadMethod(resourceUrl); headMethod.addRequestHeader("Connection", "close"); try { httpClient.executeMethod(headMethod); if (headMethod.getStatusCode() != HttpStatus.SC_OK) { throw new ResourceException("Unable to retrieve resource URL " + resourceUrl + ", received HTTP status code " + headMethod.getStatusCode()); } Header lastModifiedHeader = headMethod.getResponseHeader("Last-Modified"); if (lastModifiedHeader != null && !DatatypeHelper.isEmpty(lastModifiedHeader.getValue())) { long lastModifiedTime = DateUtil.parseDate(lastModifiedHeader.getValue()).getTime(); return new DateTime(lastModifiedTime, ISOChronology.getInstanceUTC()); } return new DateTime(); } catch (IOException e) { throw new ResourceException("Unable to contact resource URL: " + resourceUrl, e); } catch (DateParseException e) { throw new ResourceException("Unable to parse last modified date for resource:" + resourceUrl, e); } finally { headMethod.releaseConnection(); } }