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:de.mpg.imeji.presentation.util.SearchAndExportHelper.java

public static String getCitation(Publication publication) {
    URI uri = publication.getUri();
    URI searchAndExportUri = URI.create("http://" + uri.getHost() + "/search/SearchAndExport");
    String itemId = null;//  w  w  w  .  j  a v  a2  s  . c  o  m
    if (uri.getQuery() != null && uri.getQuery().contains("itemId=")) {
        itemId = uri.getQuery().split("itemId=")[1];
    } else if (uri.getPath() != null && uri.getPath().contains("/item/")) {
        itemId = uri.getPath().split("/item/")[1];
    }
    if (UrlHelper.isValidURI(searchAndExportUri)) {
        try {
            HttpClient client = new HttpClient();
            String exportUri = searchAndExportUri.toString() + "?cqlQuery=" + URLEncoder.encode(
                    "escidoc.objid=" + itemId + " or escidoc.property.version.objid=" + itemId, "ISO-8859-1")
                    + "&exportFormat=" + publication.getExportFormat() + "&outputFormat=html_linked";
            GetMethod method = new GetMethod(exportUri);
            client.executeMethod(method);
            return method.getResponseBodyAsString();
        } catch (Exception e) {
            return null;
        }
    }
    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  .  ja v a 2 s.  c om

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

    return document;
}

From source file:att.jaxrs.client.Category.java

public static Category[] getCategories() {
    GetMethod get = new GetMethod(Constants.SELECT_ALL_CATEGORY_OPERATION);
    CategoryCollection collection = new CategoryCollection();

    HttpClient httpClient = new HttpClient();
    try {//  w w w  .ja v  a2s .co m
        int result = httpClient.executeMethod(get);
        System.out.println(Constants.RESPONSE_STATUS_CODE + result);
        collection = Marshal.unmarshal(CategoryCollection.class, get.getResponseBodyAsStream());

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        get.releaseConnection();

    }
    if (null != collection.getCategories() && collection.getCategories().length > 0) {
        return collection.getCategories();
    } else {
        System.out.println("unmarshalling returned empty collection");
    }
    return null;
}

From source file:com.kwoksys.framework.util.HttpUtils.java

/**
 * Gets contents from url//from w w w .  j  a v a  2 s. com
 * @param url
 * @return
 * @throws Exception
 */
public static String getContent(String url) throws Exception {
    // Create an instance of HttpClient.
    HttpClient client = new HttpClient();

    // Create a method instance.
    GetMethod method = new GetMethod(url);

    // 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) {
            throw new Exception("HTTP method error: " + method.getStatusLine());
        }

        // Read the response body.
        return new String(method.getResponseBody());

    } finally {
        // Release the connection.
        method.releaseConnection();
    }
}

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

public static String getResponseBodyAsString(String url) {
    HttpMethod method = new GetMethod(url);
    return getResponseBodyAsString(method);
}

From source file:com.ibm.watson.apis.conversation_enhanced.filters.LookUp.java

private HttpMethod getHttpMethod(String url) {
    HttpMethod method = new GetMethod(url);
    method.setFollowRedirects(true);
    return method;
}

From source file:com.jaspersoft.studio.server.util.HttpUtils31.java

public static HttpMethod get(HttpClient client, String url) throws HttpException, IOException {
    HttpMethod method = new GetMethod(url);
    method.setRequestHeader("Accept", "application/json");

    System.out.println(method.getURI());
    int statusCode = client.executeMethod(method);
    if (statusCode != HttpStatus.SC_OK)
        System.err.println("Method failed: " + method.getStatusLine());
    return method;
}

From source file:com.robonobo.common.media.PlaylistItem.java

public static List getPlaylist(String uri) throws IOException, PlaylistFormatException {
    Log log = LogFactory.getLog(PlaylistItem.class);
    Vector list = new Vector();
    HttpClient client = new HttpClient();
    GetMethod get = new GetMethod(uri);
    int status = client.executeMethod(get);
    switch (status) {
    case 200://from w ww  .  j  av a  2  s  . c o m
        BufferedReader reader = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream()));
        String line = reader.readLine();
        if (!line.equals("[playlist]"))
            throw new PlaylistFormatException("The provided URL does not describe a playlist");
        String[] kvp;
        int currentEntryNumber = 1;
        int version = 2;
        int supposedNumberOfEntries = 1;
        PlaylistItem currentEntry = new PlaylistItem();
        while ((line = reader.readLine()) != null) {
            kvp = line.split("=");
            if (kvp[0].equals("NumberOfEntries")) {
                supposedNumberOfEntries = Integer.parseInt(kvp[1]);
            } else if (kvp[0].equals("Version")) {
                version = Integer.parseInt(kvp[1]);
                if (version != 2)
                    throw new PlaylistFormatException(
                            "This parser currently only supports version 2 .pls files");
            } else {
                if (!kvp[0].endsWith(String.valueOf(currentEntryNumber))) {
                    list.add(currentEntry);
                    currentEntryNumber++;
                    currentEntry = new PlaylistItem();
                }
                if (kvp[0].startsWith("File")) {
                    currentEntry.setFile(kvp[1]);
                } else if (kvp[0].startsWith("Title")) {
                    currentEntry.setTitle(kvp[1]);
                } else if (kvp[0].startsWith("Length")) {
                    currentEntry.setLength(Integer.parseInt(kvp[1]));
                }
            }
        }
        if (currentEntry != null)
            list.add(currentEntry);
        if (supposedNumberOfEntries != list.size())
            throw new PlaylistFormatException("The server said there were " + supposedNumberOfEntries
                    + " but we actually got " + list.size());
        return list;
    default:
        throw new IOException(
                "The remote server responded with a status " + status + " and not 200 as expected");
    }
}

From source file:apm.common.utils.HttpTookit.java

/**
 * HTTP GET?HTML//from w w  w .  j ava2  s  .  c  o  m
 * 
 * @param url
 *            URL?
 * @param queryString
 *            ?,?null
 * @param charset
 *            
 * @param pretty
 *            ?
 * @return ?HTML
 */
public static String doGet(String url, String queryString, String charset, boolean pretty) {
    StringBuffer response = new StringBuffer();
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(url);
    try {
        if (StringUtils.isNotBlank(queryString))
            // get??http?????%?
            method.setQueryString(URIUtil.encodeQuery(queryString));
        client.executeMethod(method);
        if (method.getStatusCode() == HttpStatus.SC_OK) {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(method.getResponseBodyAsStream(), charset));
            String line;
            while ((line = reader.readLine()) != null) {
                if (pretty) {
                    response.append(line).append(System.getProperty("line.separator"));
                } else {
                    response.append(line);
                }
            }
            reader.close();
        }
    } catch (URIException e) {
        log.error("HTTP Get?" + queryString + "???", e);
    } catch (IOException e) {
        log.error("HTTP Get" + url + "??", e);
    } finally {
        method.releaseConnection();
    }
    return response.toString();
}

From source file:com.intellij.diagnostic.DevelopersLoader.java

public static Collection<Developer> fetchDevelopers(ProgressIndicator indicator) throws IOException {
    List<Developer> developers = new LinkedList<Developer>();
    developers.add(Developer.NULL);/*from   ww  w  .  j a va 2  s .c  o m*/

    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setConnectionTimeout(TIMEOUT);
    HttpMethod method = new GetMethod(DEVELOPERS_LIST_URL);

    try {
        client.executeMethod(method);

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(method.getResponseBodyAsStream(), DATA_CHARSET));

        try {
            while (true) {
                String line = reader.readLine();
                if (line == null)
                    break;
                int i = line.indexOf('\t');
                if (i == -1)
                    throw new IOException("Protocol error");
                int id = Integer.parseInt(line.substring(0, i));
                String name = line.substring(i + 1);
                developers.add(new Developer(id, name));
                indicator.checkCanceled();
            }
            return developers;
        } finally {
            reader.close();
        }
    } finally {
        method.releaseConnection();
    }
}