Example usage for org.apache.commons.httpclient.methods GetMethod GetMethod

List of usage examples for org.apache.commons.httpclient.methods GetMethod GetMethod

Introduction

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

Prototype

public GetMethod(String uri) 

Source Link

Usage

From source file:com.jivesoftware.os.jive.utils.shell.utils.Curl.java

public String curl(String path) throws IOException {
    GetMethod method = new GetMethod(path);
    byte[] responseBody;
    StatusLine statusLine;/*from   w ww.  ja va  2  s  .  c om*/
    try {
        client.executeMethod(method);

        responseBody = method.getResponseBody();
        statusLine = method.getStatusLine();
        if (statusLine.getStatusCode() == 200) {
            return new String(responseBody);
        } else {
            return null;
        }
    } finally {
        method.releaseConnection();
    }
}

From source file:net.morphbank.webclient.GetRDF.java

public void post(String strURL) throws Exception {
    // Prepare HTTP post
    GetMethod get = new GetMethod(strURL);
    get.setRequestHeader("Accept", "application/rdf+xml");
    // Request content will be retrieved directly
    HttpClient httpclient = new HttpClient();
    // Execute request
    try {/*w  ww .j  av a 2  s.  c o  m*/
        System.out.println("Trying post");
        int result = httpclient.executeMethod(get);
        // Display status code
        System.out.println("Response status code: " + result);
        // Display response
        System.out.println("Response body: ");
        InputStream response = get.getResponseBodyAsStream();
        //int j = response.read(); System.out.write(j);
        for (int i = response.read(); i != -1; i = response.read()) {
            System.out.write(i);
        }
        //System.out.flush();
    } finally {
        // Release current connection to the connection pool once you are
        // done
        get.releaseConnection();
    }
}

From source file:edu.uci.ics.external.connector.asterixdb.ConnectorUtils.java

public static DatasetInfo retrieveDatasetInfo(StorageParameter storageParameter) throws Exception {
    HttpClient client = new HttpClient();
    String requestStr = storageParameter.getServiceURL() + "/connector" + "?dataverseName="
            + storageParameter.getDataverseName() + "&datasetName=" + storageParameter.getDatasetName();
    // Create a method instance.
    GetMethod method = new GetMethod(requestStr);

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

    // Deals with the response.
    // Executes the method.
    try {/* w w w. ja  v a 2 s.co m*/
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
        }

        // Deals with the response.
        JSONTokener tokener = new JSONTokener(new InputStreamReader(method.getResponseBodyAsStream()));
        JSONObject response = new JSONObject(tokener);

        // Checks if there are errors.
        String error = "error";
        if (response.has(error)) {
            throw new IllegalStateException("AsterixDB returned errors: " + response.getString(error));
        }

        // Extracts record type and file partitions.
        boolean temp = extractTempInfo(response);
        ARecordType recordType = extractRecordType(response);
        List<FilePartition> filePartitions = extractFilePartitions(response);
        IFileSplitProvider fileSplitProvider = createFileSplitProvider(storageParameter, filePartitions);
        String[] locations = getScanLocationConstraints(filePartitions, storageParameter.getIpToNcNames());
        String[] primaryKeys = response.getString("keys").split(",");
        DatasetInfo datasetInfo = new DatasetInfo(locations, fileSplitProvider, recordType, primaryKeys, temp);
        return datasetInfo;
    } catch (Exception e) {
        throw e;
    } finally {
        method.releaseConnection();
    }
}

From source file:com.ironiacorp.http.impl.httpclient3.GetRequest.java

public HttpJob call() {
    URI uri = job.getUri();//from w w w  . j  a v a2  s  .c om
    GetMethod getMethod = new GetMethod(uri.toString());
    HttpMethodResult result = new HttpMethodResult();

    try {
        int statusCode = client.executeMethod(getMethod);

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

        InputStream inputStream = getMethod.getResponseBodyAsStream();
        if (inputStream != null) {
            result.setContent(inputStream);
            result.setStatusCode(statusCode);
            job.setResult(result);
        }
    } catch (HttpException e) {
    } catch (IOException e) {
        // In case of an IOException the connection will be released
        // back to the connection manager automatically
    } catch (RuntimeException ex) {
        // In case of an unexpected exception you may want to abort
        // the HTTP request in order to shut down the underlying
        // connection and release it back to the connection manager.
        getMethod.abort();
    }

    return job;
}

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

/**
 * 
 * @param u like "/index.php?title=Special:Recentchanges&feed=rss"
 */
public GetPage(String u) {
    msgs.add(new GetMethod(u));

}

From source file:com.unicauca.braim.http.HttpBraimClient.java

public int getConnectionStatus() throws IOException {
    GetMethod method = new GetMethod(socket_url);
    int statusCode = client.executeMethod(method);
    return (statusCode);
}

From source file:it.pronetics.madstore.crawler.downloader.impl.DownloaderImpl.java

public Page download(Link link) {
    String url = link.getLink();//from  w  w  w . j  a  v  a 2 s .  c o m
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(url);
    try {
        LOG.info("Downloading page from: {}", url);
        int status = client.executeMethod(method);
        if (status == HttpStatus.SC_OK) {
            String data = method.getResponseBodyAsString();
            return new Page(link, data);
        } else {
            return new Page(link);
        }
    } catch (Exception e) {
        LOG.warn("Download failed: {}", url);
        LOG.warn(e.getMessage());
        LOG.debug(e.getMessage(), e);
        return new Page(link);
    } finally {
        method.releaseConnection();
    }
}

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

private static void list(final String url, final HttpClient httpClient) throws IOException, HttpException {

    System.out.println("Sent HTTP GET request to query all books in catalog");

    final GetMethod get = new GetMethod(url);
    try {//from  ww w  .  j a va  2 s  .com
        int status = httpClient.executeMethod(get);
        if (status == 200) {
            System.out.println(get.getResponseBodyAsString());
        }
    } finally {
        get.releaseConnection();
    }
}

From source file:name.chengchao.myhttpclient.version3_1.HttpClient3UtilUseManager.java

/**
 * ?url?ResponseBody,method=get//from   www .java2s. c o m
 * 
 * @param url exp:http://192.168.1.1:8080/dir/target.html
 * @return byte[]?
 */
public static byte[] getDataFromUrl(String url, int timeout) {
    if (StringUtils.isBlank(url)) {
        logger.error("url is blank!");
        return null;
    }
    HttpClient httpClient = new HttpClient(connectionManager);
    // 
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(8000);
    // ?
    httpClient.getParams().setSoTimeout(timeout);
    GetMethod method = new GetMethod(url);

    // fix???
    // method.setRequestHeader("Connection", "close");
    // ??1
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(1, false));
    try {
        int statusCode = httpClient.executeMethod(method);
        if (statusCode == HttpStatus.SC_OK) {
            return method.getResponseBody();
        } else {
            throw new RuntimeException("http request error,return code:" + statusCode + ",msg:"
                    + new String(method.getResponseBody()));
        }
    } catch (HttpException e) {
        method.abort();
        logger.error(e.getMessage());
    } catch (IOException e) {
        method.abort();
        logger.error(e.getMessage());
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return null;
}

From source file:com.javector.soaj.deploy.invocation.TestInvocation.java

public void testInvocation() throws Exception {
    HttpClient client = new HttpClient();

    log.info("Entered TestInvocation.testInvocation.");
    //TODO factor out the hardcoded http address
    HttpMethod method = new GetMethod("http://localhost:8080/tester/test");
    System.out.println("HTTP GET response code: " + client.executeMethod(method));
    Header hdr = method.getResponseHeader("IntegrationTest_SoajEJB30Method");
    String response = "";
    if (hdr == null) {
        // check for exception
        hdr = method.getResponseHeader("IntegrationTest_SoajEJB30Method_Exception");
        throw new Exception(hdr.getValue());
    } else {//  w  ww  .  j  av  a2s .  c o m
        response = hdr.getValue();
    }
    assertEquals("Hello World", response);

}