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

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

Introduction

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

Prototype

public abstract HttpMethodParams getParams();

Source Link

Usage

From source file:ehu.ned.DBpediaSpotlightClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;//from  w  w w .jav a  2  s.  co m

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        // // Deal with the response.
        // // Use caution: ensure correct character encoding and is not binary data
        InputStream responseBody = method.getResponseBodyAsStream();
        response = IOUtils.toString(responseBody, "UTF-8");

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:AnnotationClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;/*from   ww w. ja  v a  2s . c om*/

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        response = new String(responseBody);

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:com.mythesis.userbehaviouranalysis.AnnotationClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;//from   ww  w.jav  a  2  s . c o m

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        //byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
        InputStream responseBodyAsStream = method.getResponseBodyAsStream();
        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        StringWriter writer = new StringWriter();
        IOUtils.copy(responseBodyAsStream, writer, "utf-8");
        response = writer.toString();

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:ixa.pipe.ned.DBpediaSpotlightClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;//  w ww .j ava2 s  .  c o  m

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        // // Deal with the response.
        // // Use caution: ensure correct character encoding and is not binary data
        InputStream responseBody = method.getResponseBodyAsStream();
        response = StreamUtils.copyToString(responseBody, Charset.forName("UTF-8"));

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:arena.httpclient.commons.JakartaCommonsHttpClient.java

public HttpResponse get() throws IOException {
    NameValuePair params[] = new NameValuePair[0];
    if (this.requestParameters != null) {
        List<NameValuePair> paramList = new ArrayList<NameValuePair>();
        for (String key : this.requestParameters.keySet()) {
            String[] multipleValues = this.requestParameters.get(key);
            for (String value : multipleValues) {
                paramList.add(new NameValuePair(key, value));
            }//from w ww.  ja  v  a  2  s . co m
        }
        params = paramList.toArray(new NameValuePair[paramList.size()]);
    }

    HttpMethod method = null;
    if (this.isPost) {
        method = new PostMethod(this.url);
        if (this.encoding != null) {
            method.getParams().setContentCharset(this.encoding);
        }
        ((PostMethod) method).setRequestBody(params);
    } else {
        String url = this.url;
        for (int n = 0; n < params.length; n++) {
            url = URLUtils.addParamToURL(url, params[n].getName(), params[n].getValue(), this.encoding, false);
        }
        method = new GetMethod(url);
        method.setFollowRedirects(true);
        if (this.encoding != null) {
            method.getParams().setContentCharset(this.encoding);
        }
    }

    HttpClient client = new HttpClient(this.manager);
    if (this.connectTimeout != null) {
        client.getHttpConnectionManager().getParams().setConnectionTimeout(this.connectTimeout.intValue());
    }
    if (this.readTimeout != null) {
        client.getHttpConnectionManager().getParams().setSoTimeout(this.readTimeout.intValue());
    }
    client.setState(this.state);
    try {
        int statusCode = client.executeMethod(method);
        return new HttpResponse(getResponseBytes(method), statusCode,
                buildHeaderMap(method.getResponseHeaders()));
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:com.springsource.hq.plugin.tcserver.serverconfig.web.services.SettingsLoaderImpl.java

private void configureMethod(HttpMethod method, String eid, String sessionId, String csrfNonce) {
    method.setFollowRedirects(false);//from   ww w .j a v  a2  s.  c  om
    method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
    method.setRequestHeader("Cookie", "JSESSIONID=" + sessionId);
    method.setQueryString(new NameValuePair[] { new NameValuePair("eid", eid),
            new NameValuePair(REQUEST_PARAMETER_CSRF_NONCE, csrfNonce) });
}

From source file:com.ideabase.repository.webservice.client.impl.HttpWebServiceControllerImpl.java

/**
 * {@inheritDoc}//from   w w  w .j  a v  a  2 s  .  c o  m
 */
public WebServiceResponse sendServiceRequest(final WebServiceRequest pRequest) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Sending request - " + pRequest);
    }
    // build query string
    final NameValuePair[] parameters = buildParameters(pRequest);

    // Send request to server
    final HttpMethod httpMethod = prepareMethod(pRequest.getRequestMethod(), pRequest.getServiceUri(),
            parameters);

    // set cookie policy
    httpMethod.getParams().setCookiePolicy(CookiePolicy.RFC_2109);

    // set cookies
    if (mCookies != null) {
        httpMethod.setRequestHeader(HEADER_COOKIE, mCookies);
    }
    if (pRequest.getCookies() != null) {
        httpMethod.setRequestHeader(HEADER_COOKIE, pRequest.getCookies());
        mCookies = pRequest.getCookies();
    }

    // execute method
    final HttpClient httpClient = new HttpClient();
    final WebServiceResponse response;
    try {
        final int status = httpClient.executeMethod(httpMethod);

        // build web sercie response
        response = new WebServiceResponse();
        response.setResponseStatus(status);
        response.setResponseContent(new String(httpMethod.getResponseBody()));
        response.setContentType(httpMethod.getResponseHeader(HEADER_CONTENT_TYPE).getValue());
        response.setServiceUri(httpMethod.getURI().toString());

        // set cookies
        final Header cookieHeader = httpMethod.getResponseHeader(HEADER_SET_COOKIE);
        if (cookieHeader != null) {
            mCookies = cookieHeader.getValue();
        }

        // set cookies to the returning response object.
        response.setCookies(mCookies);

        if (LOG.isDebugEnabled()) {
            LOG.debug("Cookies - " + mCookies);
            LOG.debug("Response - " + response);
        }
    } catch (Exception e) {
        throw ServiceException.aNew(pRequest, "Failed to send web service request.", e);
    } finally {
        httpMethod.releaseConnection();
    }

    return response;
}

From source file:ensen.controler.AnnotationClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;/*ww  w  .  j a  va  2  s .com*/

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        client.setHttpConnectionManager(new MultiThreadedHttpConnectionManager());
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            System.out.println("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        InputStream responseBodyStream = method.getResponseBodyAsStream(); //Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

        int b = responseBodyStream.read();
        ArrayList<Integer> bytes = new ArrayList<Integer>();
        while (b != -1) {
            bytes.add(b);
            b = responseBodyStream.read();
        }
        byte[] responseBody = new byte[bytes.size()];

        for (int i = 0; i < bytes.size(); i++) {
            responseBody[i] = bytes.get(i).byteValue();
        }
        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        response = new String(responseBody);

    } catch (HttpException e) {
        System.out.println("Fatal protocol violation: " + e.getMessage());
        try {
            System.err.println(method.getURI());
        } catch (URIException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        System.out.println("Fatal transport error: " + e.getMessage());
        System.out.println(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:net.sourceforge.jwbf.actions.HttpActionClient.java

/**
 * Process a GET Message.//from   w w w .  ja v  a 2  s .c  o m
 * 
 * @param authgets
 *            a
 * @param cp
 *            a
 * @return a returning message, not null
 * @throws IOException on problems
 * @throws CookieException on problems
 * @throws ProcessException on problems
 */
public byte[] get(HttpMethod authgets) throws IOException, CookieException, ProcessException {
    showCookies(client);
    byte[] out = null;
    authgets.getParams().setParameter("http.protocol.content-charset", MediaWikiBot.CHARSET);
    //      System.err.println(authgets.getParams().getParameter("http.protocol.content-charset"));

    client.executeMethod(authgets);
    LOG.debug(authgets.getURI());
    LOG.debug("GET: " + authgets.getStatusLine().toString());

    out = authgets.getResponseBody();

    // release any connection resources used by the method
    authgets.releaseConnection();
    int statuscode = authgets.getStatusCode();

    if (statuscode == HttpStatus.SC_NOT_FOUND) {
        LOG.warn("Not Found: " + authgets.getQueryString());

        throw new FileNotFoundException(authgets.getQueryString());
    }

    return out;
}

From source file:com.zimbra.cs.dav.client.WebDavClient.java

protected HttpMethod executeMethod(HttpMethod m, Depth d, String bodyForLogging) throws IOException {
    HttpMethodParams p = m.getParams();
    if (p != null)
        p.setCredentialCharset("UTF-8");

    m.setDoAuthentication(true);//w  w  w  . j a v  a 2 s . c  om
    m.setRequestHeader("User-Agent", mUserAgent);
    String depth = "0";
    switch (d) {
    case one:
        depth = "1";
        break;
    case infinity:
        depth = "infinity";
        break;
    case zero:
        break;
    default:
        break;
    }
    m.setRequestHeader("Depth", depth);
    logRequestInfo(m, bodyForLogging);
    HttpClientUtil.executeMethod(mClient, m);
    logResponseInfo(m);

    return m;
}