本文整理匯總了Java中org.apache.commons.httpclient.methods.HeadMethod類的典型用法代碼示例。如果您正苦於以下問題:Java HeadMethod類的具體用法?Java HeadMethod怎麽用?Java HeadMethod使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
HeadMethod類屬於org.apache.commons.httpclient.methods包,在下文中一共展示了HeadMethod類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: exists
import org.apache.commons.httpclient.methods.HeadMethod; //導入依賴的package包/類
/** {@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();
}
}
示例2: pageExists
import org.apache.commons.httpclient.methods.HeadMethod; //導入依賴的package包/類
private static boolean pageExists(@NotNull String url) {
if (new File(url).exists()) {
return true;
}
final HttpClient client = new HttpClient();
final HttpConnectionManagerParams params = client.getHttpConnectionManager().getParams();
params.setSoTimeout(5 * 1000);
params.setConnectionTimeout(5 * 1000);
try {
final HeadMethod method = new HeadMethod(url);
final int rc = client.executeMethod(method);
if (rc == 404) {
return false;
}
}
catch (IllegalArgumentException e) {
return false;
}
catch (IOException ignored) {
}
return true;
}
示例3: testExecuteMethod
import org.apache.commons.httpclient.methods.HeadMethod; //導入依賴的package包/類
public void testExecuteMethod() {
OwnCloudClient client =
new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager());
HeadMethod head = new HeadMethod(client.getWebdavUri() + "/");
int status = -1;
try {
status = client.executeMethod(head);
assertTrue("Wrong status code returned: " + status,
status > 99 && status < 600);
} catch (IOException e) {
Log.e(TAG, "Exception in HEAD method execution", e);
// TODO - make it fail? ; try several times, and make it fail if none
// is right?
} finally {
head.releaseConnection();
}
}
示例4: main
import org.apache.commons.httpclient.methods.HeadMethod; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
String message = "Hello Ivy!";
System.out.println("standard message : " + message);
System.out.println("capitalized by " + WordUtils.class.getName()
+ " : " + WordUtils.capitalizeFully(message));
HttpClient client = new HttpClient();
HeadMethod head = new HeadMethod("http://www.ibiblio.org/");
client.executeMethod(head);
int status = head.getStatusCode();
System.out.println("head status code with httpclient: " + status);
head.releaseConnection();
System.out.println(
"now check if httpclient dependency on commons-logging has been realized");
Class<?> clss = Class.forName("org.apache.commons.logging.Log");
System.out.println("found logging class in classpath: " + clss);
}
示例5: exists
import org.apache.commons.httpclient.methods.HeadMethod; //導入依賴的package包/類
public boolean exists(String filename) throws FileResourceException {
HeadMethod m = new HeadMethod(contact + '/' + filename);
try {
int code = client.executeMethod(m);
try {
if (code != HttpStatus.SC_OK) {
return false;
}
else {
return true;
}
}
finally {
m.releaseConnection();
}
}
catch (Exception e) {
throw new FileResourceException(e);
}
}
示例6: doGetType
import org.apache.commons.httpclient.methods.HeadMethod; //導入依賴的package包/類
/**
* Determines the type of this file. Must not return null. The return
* value of this method is cached, so the implementation can be expensive.
*/
@Override
protected FileType doGetType() throws Exception
{
// Use the HEAD method to probe the file.
method = new HeadMethod();
setupMethod(method);
final HttpClient client = fileSystem.getClient();
final int status = client.executeMethod(method);
method.releaseConnection();
if (status == HttpURLConnection.HTTP_OK)
{
return FileType.FILE;
}
else if (status == HttpURLConnection.HTTP_NOT_FOUND
|| status == HttpURLConnection.HTTP_GONE)
{
return FileType.IMAGINARY;
}
else
{
throw new FileSystemException("vfs.provider.http/head.error", getName());
}
}
示例7: createCommonsHttpMethod
import org.apache.commons.httpclient.methods.HeadMethod; //導入依賴的package包/類
/**
* Create a Commons HttpMethodBase object for the given HTTP method
* and URI specification.
* @param httpMethod the HTTP method
* @param uri the URI
* @return the Commons HttpMethodBase object
*/
protected HttpMethodBase createCommonsHttpMethod(HttpMethod httpMethod, String uri) {
switch (httpMethod) {
case GET:
return new GetMethod(uri);
case DELETE:
return new DeleteMethod(uri);
case HEAD:
return new HeadMethod(uri);
case OPTIONS:
return new OptionsMethod(uri);
case POST:
return new PostMethod(uri);
case PUT:
return new PutMethod(uri);
case TRACE:
return new TraceMethod(uri);
case PATCH:
throw new IllegalArgumentException(
"HTTP method PATCH not available before Apache HttpComponents HttpClient 4.2");
default:
throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
}
}
示例8: checkUrlExistence
import org.apache.commons.httpclient.methods.HeadMethod; //導入依賴的package包/類
public static void checkUrlExistence(String url) {
if (url.toLowerCase().startsWith("http") || url.toLowerCase().startsWith("https")) {
HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
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);
} catch (IOException ioe) {
throw new IllegalArgumentException("Cannot reach URL: " + url);
}
}
}
示例9: testHeadObject
import org.apache.commons.httpclient.methods.HeadMethod; //導入依賴的package包/類
@Test
public void testHeadObject() throws Exception {
// create bucket and store object
assertEquals("OK", conn.createBucket(bucketName, null, null).connection.getResponseMessage());
S3Object object = new S3Object(testData.getBytes(), null);
assertEquals("OK", conn.put(bucketName, objectName, object, null).connection.getResponseMessage());
// grant public read access to the bucket
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("x-amz-acl", Arrays.asList(new String[] { "public-read" }));
assertEquals("OK", conn.createBucket(bucketName, null, headers).connection.getResponseMessage());
HttpClient httpClient = new HttpClient();
HeadMethod headMethod = new HeadMethod(objectUri);
int rc = httpClient.executeMethod(headMethod);
assertEquals(200, rc);
System.out.println(FileUtils.readFileToString(new File(String.format("%s/%s/%s%s", BUCKET_ROOT, bucketName, objectName, ObjectMetaData.FILE_SUFFIX))));
assertEquals(Integer.toString(testData.length()), headMethod.getResponseHeaders(HttpHeaders.CONTENT_LENGTH)[0].getValue());
assertEquals(ObjectMetaData.DEFAULT_OBJECT_CONTENT_TYPE, headMethod.getResponseHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue());
}
示例10: acquireLength
import org.apache.commons.httpclient.methods.HeadMethod; //導入依賴的package包/類
private void acquireLength() throws URISyntaxException, HttpException, IOException {
HttpMethod head = new HeadMethod(url);
int code = http.executeMethod(head);
if(code != 200) {
throw new IOException("Unable to retrieve from " + url);
}
Header lengthHeader = head.getResponseHeader(CONTENT_LENGTH);
if(lengthHeader == null) {
throw new IOException("No Content-Length header for " + url);
}
String val = lengthHeader.getValue();
try {
length = Long.parseLong(val);
} catch(NumberFormatException e) {
throw new IOException("Bad Content-Length value " +url+ ": " + val);
}
}
示例11: doGetType
import org.apache.commons.httpclient.methods.HeadMethod; //導入依賴的package包/類
/**
* Determines the type of this file. Must not return null. The return
* value of this method is cached, so the implementation can be expensive.
*/
protected FileType doGetType()
throws Exception
{
// Use the HEAD method to probe the file.
method = new HeadMethod();
setupMethod(method);
final HttpClient client = fileSystem.getClient();
final int status = client.executeMethod(method);
method.releaseConnection();
if (status == HttpURLConnection.HTTP_OK)
{
return FileType.FILE;
}
else if (status == HttpURLConnection.HTTP_NOT_FOUND
|| status == HttpURLConnection.HTTP_GONE)
{
return FileType.IMAGINARY;
}
else
{
throw new FileSystemException("vfs.provider.http/head.error", getName());
}
}
示例12: resourceExists
import org.apache.commons.httpclient.methods.HeadMethod; //導入依賴的package包/類
/**
* Returns <code>true</code> if the resource given as URL does exist.
* @param client
* @param httpURL
* @return <code>true</code>if the resource exists
* @throws IOException
* @throws HttpException
*/
public static boolean resourceExists(HttpClient client, HttpURL httpURL)
throws IOException, HttpException
{
HeadMethod head = new HeadMethod(httpURL.getURI());
head.setFollowRedirects(true);
int status = client.executeMethod(head);
switch (status) {
case WebdavStatus.SC_OK:
return true;
case WebdavStatus.SC_NOT_FOUND:
return false;
default:
HttpException ex = new HttpException();
ex.setReasonCode(status);
ex.setReason(head.getStatusText());
throw ex;
}
}
示例13: createResponseHandler
import org.apache.commons.httpclient.methods.HeadMethod; //導入依賴的package包/類
/**
* Checks the method being received and created a
* suitable ResponseHandler for this method.
*
* @param method Method to handle
* @return The handler for this response
* @throws MethodNotAllowedException If no method could be choose this exception is thrown
*/
public static ResponseHandler createResponseHandler(HttpMethod method) throws MethodNotAllowedException {
if (!AllowedMethodHandler.methodAllowed(method)) {
throw new MethodNotAllowedException("The method " + method.getName() + " is not in the AllowedHeaderHandler's list of allowed methods.", AllowedMethodHandler.getAllowHeader());
}
ResponseHandler handler = null;
if (method.getName().equals("OPTIONS")) {
handler = new OptionsResponseHandler((OptionsMethod) method);
} else if (method.getName().equals("GET")) {
handler = new GetResponseHandler((GetMethod) method);
} else if (method.getName().equals("HEAD")) {
handler = new HeadResponseHandler((HeadMethod) method);
} else if (method.getName().equals("POST")) {
handler = new PostResponseHandler((PostMethod) method);
} else if (method.getName().equals("PUT")) {
handler = new PutResponseHandler((PutMethod) method);
} else if (method.getName().equals("DELETE")) {
handler = new DeleteResponseHandler((DeleteMethod) method);
} else if (method.getName().equals("TRACE")) {
handler = new TraceResponseHandler((TraceMethod) method);
} else {
throw new MethodNotAllowedException("The method " + method.getName() + " was allowed by the AllowedMethodHandler, not by the factory.", handledMethods);
}
return handler;
}
示例14: htmlHead
import org.apache.commons.httpclient.methods.HeadMethod; //導入依賴的package包/類
@Test
public void htmlHead() throws IOException {
final HeadMethod head = new HeadMethod(HTML_URL);
final int status = H.getHttpClient().executeMethod(head);
assertEquals(200, status);
assertNull("Expecting null body", head.getResponseBody());
assertCommonHeaders(head, "text/html");
}
開發者ID:apache,項目名稱:sling-org-apache-sling-launchpad-integration-tests,代碼行數:9,代碼來源:HeadServletTest.java
示例15: pngHead
import org.apache.commons.httpclient.methods.HeadMethod; //導入依賴的package包/類
@Test
public void pngHead() throws IOException {
final HeadMethod head = new HeadMethod(PNG_URL);
final int status = H.getHttpClient().executeMethod(head);
assertEquals(200, status);
assertNull("Expecting null body", head.getResponseBody());
assertCommonHeaders(head, "image/png");
}
開發者ID:apache,項目名稱:sling-org-apache-sling-launchpad-integration-tests,代碼行數:9,代碼來源:HeadServletTest.java