Package org.apache.commons.httpclient

Examples of org.apache.commons.httpclient.HttpMethod


        return receiveNoWait();
    }

    public HttpExchange receiveNoWait() {
        HttpExchange exchange = endpoint.createExchange();
        HttpMethod method = createMethod();

        try {
            int responseCode = httpClient.executeMethod(method);
            // lets store the result in the output message.
            LoadingByteArrayOutputStream bos = new LoadingByteArrayOutputStream();
            InputStream is = method.getResponseBodyAsStream();
            IOUtils.copy(is, bos);
            bos.flush();
            is.close();
            Message message = exchange.getIn();
            message.setBody(bos.createInputStream());

            // lets set the headers
            Header[] headers = method.getResponseHeaders();
            HeaderFilterStrategy strategy = endpoint.getHeaderFilterStrategy();
            for (Header header : headers) {
                String name = header.getName();
                String value = header.getValue();
                if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value)) {
                    message.setHeader(name, value);
                }
            }
       
            message.setHeader("http.responseCode", responseCode);
            return exchange;
        } catch (IOException e) {
            throw new RuntimeCamelException(e);
        } finally {
            method.releaseConnection();
        }
    }
View Full Code Here


        if (creds != null) {
            client.getState().setCredentials(AuthScope.ANY, creds);
        }

        String url = args[0];
        HttpMethod method = null;

        //create a method object
            method = new GetMethod(url);
            method.setFollowRedirects(true);
        //} catch (MalformedURLException murle) {
        //    System.out.println("<url> argument '" + url
        //            + "' is not a valid URL");
        //    System.exit(-2);
        //}

        //execute the method
        String responseBody = null;
        try{
            client.executeMethod(method);
            responseBody = method.getResponseBodyAsString();
        } catch (HttpException he) {
            System.err.println("Http error connecting to '" + url + "'");
            System.err.println(he.getMessage());
            System.exit(-4);
        } catch (IOException ioe){
            System.err.println("Unable to connect to '" + url + "'");
            System.exit(-3);
        }


        //write out the request headers
        System.out.println("*** Request ***");
        System.out.println("Request Path: " + method.getPath());
        System.out.println("Request Query: " + method.getQueryString());
        Header[] requestHeaders = method.getRequestHeaders();
        for (int i=0; i<requestHeaders.length; i++){
            System.out.print(requestHeaders[i]);
        }

        //write out the response headers
        System.out.println("*** Response ***");
        System.out.println("Status Line: " + method.getStatusLine());
        Header[] responseHeaders = method.getResponseHeaders();
        for (int i=0; i<responseHeaders.length; i++){
            System.out.print(responseHeaders[i]);
        }

        //write out the response body
        System.out.println("*** Response Body ***");
        System.out.println(responseBody);

        //clean up the connection resources
        method.releaseConnection();

        System.exit(0);
    }
View Full Code Here

              options.setIfNoneMatch(cached_response.getEntityTag().toString());
            } else {
              disp = CacheDisposition.TRANSPARENT;
            }
          default:                                                               // CACHE MISS
            HttpMethod httpMethod =
              MethodHelper.createMethod(
                method, uri, entity, options);
            client.executeMethod(httpMethod);
            ClientResponse response = new CommonsResponse(abdera,httpMethod);
            return (options.getUseLocalCache()) ?
View Full Code Here

    String uri,
    RequestEntity entity,
    RequestOptions options) {
      if (method == null) return null;
      Method m = Method.fromString(method);
      HttpMethod httpMethod = null;
      switch(m) {
        case GET:     httpMethod = new GetMethod(uri); break;
        case POST:    httpMethod = getMethod(new PostMethod(uri), entity); break;
        case PUT:     httpMethod = getMethod(new PutMethod(uri), entity); break;
        case DELETE:  httpMethod = new DeleteMethod(uri); break;
View Full Code Here

    protected String request(String url, String content) throws Exception {
        return requestWithHttpClient(url, content);
    }

    private String requestWithHttpClient(String url, String content) throws Exception {
        HttpMethod method;
        if (content != null) {
            PostMethod post = new PostMethod(url);
            post.setRequestEntity(new StringRequestEntity(content));
            method = post;
        } else {
            GetMethod get = new GetMethod(url);
            method = get;
        }
        new HttpClient().executeMethod(method);
        if (method.getStatusCode() != 200) {
            throw new InvalidStatusResponseException(method.getStatusCode());
        }
        return method.getResponseBodyAsString();
    }
View Full Code Here

    HostConfiguration hc = new HostConfiguration();
    hc.setHost(url);
    client.setHostConfiguration(hc);
   
    //TODO support other methods
    HttpMethod method = new GetMethod(url.getPathQuery());
    Header[] headers = conn.getHeaders();
    for (int i=0; i<headers.length; i++) {
      method.addRequestHeader(headers[i]);
    }
    if (method instanceof EntityEnclosingMethod) {
      EntityEnclosingMethod emethod = (EntityEnclosingMethod) method;
      emethod.setRequestBody(conn.getInputStream());
    }
    client.executeMethod(method);

   
    Header[] rheaders = method.getResponseHeaders();
    InputStream targetIn = method.getResponseBodyAsStream();
    ResponseWriter out = conn.getWriter();
    out.println(method.getStatusLine().toString());
    for (int i=0; i<rheaders.length; i++) {
      out.print(rheaders[i].toExternalForm());
    }
    if (rheaders.length > 0) out.println();
    out.flush();
View Full Code Here

        if (creds != null) {
            client.getState().setCredentials(AuthScope.ANY, creds);
        }

        String url = args[0];
        HttpMethod method = null;

        //create a method object
            method = new GetMethod(url);
            method.setFollowRedirects(true);
        //} catch (MalformedURLException murle) {
        //    System.out.println("<url> argument '" + url
        //            + "' is not a valid URL");
        //    System.exit(-2);
        //}

        //execute the method
        String responseBody = null;
        try{
            client.executeMethod(method);
            responseBody = method.getResponseBodyAsString();
        } catch (HttpException he) {
            System.err.println("Http error connecting to '" + url + "'");
            System.err.println(he.getMessage());
            System.exit(-4);
        } catch (IOException ioe){
            System.err.println("Unable to connect to '" + url + "'");
            System.exit(-3);
        }


        //write out the request headers
        System.out.println("*** Request ***");
        System.out.println("Request Path: " + method.getPath());
        System.out.println("Request Query: " + method.getQueryString());
        Header[] requestHeaders = method.getRequestHeaders();
        for (int i=0; i<requestHeaders.length; i++){
            System.out.print(requestHeaders[i]);
        }

        //write out the response headers
        System.out.println("*** Response ***");
        System.out.println("Status Line: " + method.getStatusLine());
        Header[] responseHeaders = method.getResponseHeaders();
        for (int i=0; i<responseHeaders.length; i++){
            System.out.print(responseHeaders[i]);
        }

        //write out the response body
        System.out.println("*** Response Body ***");
        System.out.println(responseBody);

        //clean up the connection resources
        method.releaseConnection();

        System.exit(0);
    }
View Full Code Here

  }

  private static int httpNotification(String uri) throws IOException {
    URI url = new URI(uri, false);
    HttpClient m_client = new HttpClient();
    HttpMethod method = new GetMethod(url.getEscapedURI());
    method.setRequestHeader("Accept", "*/*");
    return m_client.executeMethod(method);
  }
View Full Code Here

        //start a session with the webserver
        client.setHostConfiguration(hc);

        //create a method object
        HttpMethod method = new GetMethod(url.getPath());

        //turn follow redirects off
        method.setFollowRedirects(false);

        //turn strict mode on
        method.setStrictMode(false);

        //execute the method
        try{
            client.executeMethod(method);
        } catch (HttpException he) {
            System.err.println("Http error connecting to '" + url + "'");
            System.err.println(he.getMessage());
            System.exit(-4);
        } catch (IOException ioe){
            System.err.println("Unable to connect to '" + url + "'");
            System.exit(-3);
        }

        //get the request headers
        Header[] requestHeaders = method.getRequestHeaders();

        //get the response headers
        Header[] responseHeaders = method.getResponseHeaders();

        //get the response body
        byte[] responseBody = method.getResponseBody();

        //write out the request headers
        System.out.println("*** Request Headers ***");
        for (int i=0; i<requestHeaders.length; i++){
            System.out.print(requestHeaders[i]);
        }

        //write out the response headers
        System.out.println("*** Response Headers ***");
        for (int i=0; i<responseHeaders.length; i++){
            System.out.print(responseHeaders[i]);
        }
        method.releaseConnection();

        System.exit(0);
    }
View Full Code Here

        return res;
    }
   
    protected static HttpResponse get(String url, String user, String password) throws IOException {
        HttpClient httpClient = new HttpClient();
        HttpMethod method = new GetMethod(url);
        addAuthHeader(method, user, password);
       
        String contentType = "application/xml; charset=utf-8";
        method.setRequestHeader("Content-type", contentType);
       
        int status = httpClient.executeMethod(method);
        InputStream responseBody = method.getResponseBodyAsStream();
       
        HttpResponse res = new HttpResponse(status, responseBody);
        return res;
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.httpclient.HttpMethod

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.