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

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

Introduction

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

Prototype

public abstract void releaseConnection();

Source Link

Usage

From source file:edu.pitt.dbmi.facebase.hd.HumanDataController.java

/** http web client class used for communicating with Hub 
 * @param action String is one of status, update, event
 * @param signal String is a timeInSeconds or QueueID or eventCode
 * for action=status, signal is "0", number-of-seconds-until-back, 
 * for action=update, signal is qid.hash
 * for action=event, signal "1" means mysql server is unavailable.
 * uses getOTP() method for URL construction
 *///from  w w  w  . ja v  a2  s. co  m
public static void httpGetter(String action, String signal) {
    String oneTimePassword = (new Long(getOTP())).toString();
    String url = hubURL + responseTrigger + oneTimePassword + "/";
    if (action == "update") {
        url += action + "/" + signal;
    } else if (action == "status") {
        long time = System.currentTimeMillis() / 1000L;
        time += Long.parseLong(signal);
        Long timeLong;
        timeLong = new Long(time);
        url += action + "/" + timeLong.toString();
    } else if (action == "event") {
        url += action + "/" + signal;
    } else {
    }
    HttpClient webClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    try {
        webClient.executeMethod(httpMethod);
        String response = httpMethod.getResponseBodyAsString();
    } catch (HttpException httpe) {
    } catch (IOException ioe) {
    } finally {
        httpMethod.releaseConnection();
        httpMethod.recycle();
    }
}

From source file:com.ms.commons.utilities.HttpClientUtils.java

/**
 * byte[]??method????getResponseBodyAsStream
 * //from   w  w w.  jav a 2  s. c o m
 * @param method
 * @return
 */
public static byte[] getResponseBodyAsByte(HttpMethod method) {
    init();
    InputStream inputStream = null;
    try {
        inputStream = getResponseBodyAsStream(method, null, null);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IOUtils.copy(inputStream, baos);
        return baos.toByteArray();
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(inputStream);
        method.releaseConnection();
    }
    return new byte[] {};
}

From source file:com.zimbra.cs.fb.ExchangeFreeBusyProvider.java

public static int checkAuth(ServerInfo info, Account requestor) throws ServiceException, IOException {
    ExchangeFreeBusyProvider prov = new ExchangeFreeBusyProvider();
    ArrayList<Request> req = new ArrayList<Request>();
    req.add(new Request(requestor, requestor.getName(), prov.cachedFreeBusyStartTime(),
            prov.cachedFreeBusyEndTime(), FreeBusyQuery.CALENDAR_FOLDER_ALL));
    String url = prov.constructGetUrl(info, req);

    HttpMethod method = new GetMethod(url);
    try {//from   w  ww.j  a v a 2  s  .  c  o  m
        return prov.sendRequest(method, info);
    } finally {
        method.releaseConnection();
    }
}

From source file:cn.newtouch.util.HttpClientUtil.java

/**
 * ?url??post/*from  ww  w. ja  v a2s .c o  m*/
 * 
 * @param url
 * @param paramsStr
 *            ?
 * @param method
 * @return String ?
 */
public static String post(String url, String paramsStr, String method) {

    HttpClient client = new HttpClient();
    HttpMethod httpMethod = new PostMethod(url);
    httpMethod.setQueryString(paramsStr);
    try {
        int returnCode = client.executeMethod(httpMethod);
        if (returnCode == HttpStatus.SC_OK) {
            return httpMethod.getResponseBodyAsString();
        } else if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
            System.err.println("The Post method is not implemented by this URI");
        }
    } catch (Exception e) {
        System.err.println(e);
    } finally {
        httpMethod.releaseConnection();
    }
    return null;
}

From source file:edu.unc.lib.dl.ui.service.XMLRetrievalService.java

public static Document getXMLDocument(String url) throws HttpException, IOException, JDOMException {
    SAXBuilder builder = new SAXBuilder();

    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(url);
    method.getParams().setParameter("http.socket.timeout", new Integer(2000));
    method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    method.getParams().setParameter("http.useragent", "");

    InputStream responseStream = null;
    Document document = null;// w w  w .j av a 2s  . com

    try {
        client.executeMethod(method);
        responseStream = method.getResponseBodyAsStream();
        document = builder.build(responseStream);
    } finally {
        if (responseStream != null)
            responseStream.close();
        method.releaseConnection();
    }

    return document;
}

From source file:edu.utah.further.core.ws.HttpUtil.java

/**
 * Static method for retrieving the response from an already executed method. This
 * method does not responsible for closing the inputstream unless an exception occurs
 * during retrieval of the response.//  ww  w.  j  av  a 2s. c o m
 * 
 * @param method
 * @return
 */
public static InputStream getHttpResponseStream(final HttpMethod method) {
    isTrue(method.hasBeenUsed());

    Exception exception = null;
    try {
        return method.getResponseBodyAsStream();
    } catch (final IOException e) {
        exception = e;
        throw new ApplicationException("An exception occured while retrieving the response", e);
    } finally {
        if (exception != null) {
            method.releaseConnection();
        }
    }
}

From source file:com.renlg.util.NetAssist.java

public static String delegateGet(String url) {

    StringBuffer response = new StringBuffer();
    HttpClient client = new HttpClient();
    HttpMethod method = null;
    try {/*from w  w  w  .  j a v  a 2s.c o  m*/
        method = new GetMethod(url);
        client.executeMethod(method);
        if (method.getStatusCode() == HttpStatus.SC_OK) {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(method.getResponseBodyAsStream(), "utf8"));
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
        }

    } catch (Exception e) {
        log.error("HTTP Get" + url + "??", e);
    } finally {
        if (method != null)
            method.releaseConnection();
    }
    return response.toString();
}

From source file:davmail.http.DavGatewayHttpClientFacade.java

private static void resetMethod(HttpMethod method) {
    // reset method state
    method.releaseConnection();
    method.getHostAuthState().invalidate();
    method.getProxyAuthState().invalidate();
}

From source file:com.comcast.cats.service.util.HttpClientUtil.java

/**
 * Helper method to clean up resources. This is important as the number of
 * open files/streams are limited in an operating system.
 * //  w  w  w. j av  a 2 s  .  c  o m
 * @param bufferedReader
 * @param inputStreamReader
 * @param responseStream
 * @param httpMethod
 * @param client
 */
private static synchronized void cleanUp(Reader inputStreamReader, InputStream responseStream,
        HttpMethod httpMethod) {
    if (inputStreamReader != null) {
        try {
            inputStreamReader.close();
        } catch (IOException e) {
            logger.warn("Exception caught trying to close inputStreamReader", e);
        }
    }
    if (responseStream != null) {
        try {
            responseStream.close();
        } catch (IOException e) {
            logger.warn("Exception caught trying to close responseStream", e);
        }
    }
    if (httpMethod != null) {
        httpMethod.releaseConnection();
    }
}

From source file:demo.jaxrs.client.Client.java

private static void handleHttpMethod(HttpMethod httpMethod) throws Exception {
    HttpClient client = new HttpClient();

    try {// w w w.ja  va  2s . c  o m
        int statusCode = client.executeMethod(httpMethod);
        System.out.println("Response status : " + statusCode);

        Response.Status status = Response.Status.fromStatusCode(statusCode);

        if (status == Response.Status.OK) {
            System.out.println(httpMethod.getResponseBodyAsString());
        } else if (status == Response.Status.FORBIDDEN) {
            System.out.println("Authorization failure");
        } else if (status == Response.Status.UNAUTHORIZED) {
            System.out.println("Authentication failure");
        }
        System.out.println();

    } finally {
        // release any connection resources used by the method
        httpMethod.releaseConnection();
    }
}