Example usage for org.apache.commons.httpclient HttpMethod toString

List of usage examples for org.apache.commons.httpclient HttpMethod toString

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:edu.caltech.ipac.firefly.server.network.HttpServices.java

/**
 * Execute the given HTTP method with the given parameters.
 * @param method    the function or method to perform
 * @param cookies   optional, sent with request if present.
 * @return  true is the request was successfully received, understood, and accepted (code 2xx).
 *///from   ww w .  j  a v  a2 s  .  c o  m
public static boolean executeMethod(HttpMethod method, String userId, String password,
        Map<String, String> cookies) {
    try {
        if (!StringUtils.isEmpty(userId)) {
            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userId, password);
            httpClient.getState().setCredentials(AuthScope.ANY, credentials);
        } else {
            // check to see if the userId and password is in the url
            userId = URLDownload.getUserFromUrl(method.toString());
            if (userId != null) {
                password = URLDownload.getPasswordFromUrl(method.toString());
                UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userId, password);
                httpClient.getState().setCredentials(AuthScope.ANY, credentials);
            }
        }

        if (cookies != null) {
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, String> entry : cookies.entrySet()) {
                if (sb.length() > 0)
                    sb.append("; ");
                sb.append(entry.getKey());
                sb.append("=");
                sb.append(entry.getValue());
            }
            if (sb.length() > 0) {
                method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
                method.setRequestHeader("Cookie", sb.toString());
            }
        }

        int status = httpClient.executeMethod(method);
        boolean isSuccess = status >= 200 && status < 300;
        String reqDesc = "URL:" + method.getURI();
        if (isSuccess) {
            LOG.info(reqDesc);
        } else {
            reqDesc = reqDesc + "\nREQUEST HEADERS: "
                    + CollectionUtil.toString(method.getRequestHeaders()).replaceAll("\\r|\\n", "")
                    + "\nPARAMETERS:\n " + getDesc(method.getParams()) + "\nRESPONSE HEADERS: "
                    + CollectionUtil.toString(method.getResponseHeaders()).replaceAll("\\r|\\n", "");

            LOG.error("HTTP request failed with status:" + status + "\n" + reqDesc);
        }
        return isSuccess;
    } catch (Exception e) {
        LOG.error(e, "Unable to connect to:" + method.toString());
    }
    return false;
}

From source file:at.ac.tuwien.auto.sewoa.http.HttpRetrieverImpl.java

private byte[] send(HttpClient httpClient, HttpMethod method) {
    try {//  w ww  . j a  v  a2 s. com
        int httpResult = httpClient.executeMethod(method);
        if (httpResult != HttpURLConnection.HTTP_OK) {
            LOGGER.warn("cannot post data to url {} -> result: {}.", method.getURI().toString(), httpResult);
            ByteStreams.toByteArray(method.getResponseBodyAsStream());
            throw new IllegalStateException("error code");
        } else {
            return ByteStreams.toByteArray(method.getResponseBodyAsStream());
        }
    } catch (Exception e) {
        LOGGER.warn("cannot post http data to url {} due to {}.", method.toString(), e.getMessage());
        throw new IllegalStateException("could not post data");
    }
}