Example usage for org.apache.commons.httpclient.methods HeadMethod setURI

List of usage examples for org.apache.commons.httpclient.methods HeadMethod setURI

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods HeadMethod setURI.

Prototype

@Override
public void setURI(URI uri) throws URIException 

Source Link

Document

Sets the URI for this method.

Usage

From source file:org.apache.wink.itest.methodannotations.HttpMethodTest.java

/**
 * Tests that a HEAD request can be sent to resource containing only a GET
 * method.//  w w w . j a v  a2 s. c  o  m
 */
public void testHEADRequest() {
    try {
        HeadMethod httpMethod = new HeadMethod();
        httpMethod.setURI(new URI(BASE_URI, false));
        httpclient = new HttpClient();

        try {
            int result = httpclient.executeMethod(httpMethod);
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            String responseBody = httpMethod.getResponseBodyAsString();
            System.out.println(responseBody);
            assertEquals(200, result);
            assertEquals(null, responseBody);
            Header[] headers = httpMethod.getResponseHeaders();
            assertNotNull(headers);
            assertTrue("Response for HEAD request contained no headers", headers.length > 0);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}

From source file:org.apache.wink.itest.methodannotations.HttpMethodTest.java

/**
 * Tests that a HEAD request can be sent to resource annotated with a custom
 * HEAD annotation.//w  w  w.ja va 2 s.  co m
 */
public void testCustomHEADRequest() {
    try {
        HeadMethod httpMethod = new HeadMethod();
        httpMethod.setURI(new URI(ALT_URI, false));
        httpclient = new HttpClient();

        try {
            int result = httpclient.executeMethod(httpMethod);
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            String responseBody = httpMethod.getResponseBodyAsString();
            System.out.println(responseBody);
            assertEquals(200, result);
            assertEquals(null, responseBody);
            Header header = httpMethod.getResponseHeader("HEAD");
            assertNotNull(header);
            assertEquals("TRUE", header.getValue());
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}