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.topicmapslab.tmcledit.model.psiprovider.internal.Subj3ctPSIProvider.java

public Set<PSIProviderResult> getSubjectIdentifier() {
    if (getName().length() == 0)
        return Collections.emptySet();

    HttpMethod method = null;//from   w w w. java2s  . com
    try {
        String url = "http://api.subj3ct.com/subjects/search";

        HttpClient client = new HttpClient();
        method = new GetMethod(url);

        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(2);
        params.add(new NameValuePair("format", "xml"));
        params.add(new NameValuePair("query", getName()));
        method.setQueryString(params.toArray(new NameValuePair[params.size()]));

        client.getParams().setSoTimeout(5000);
        client.executeMethod(method);

        String result = method.getResponseBodyAsString();

        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        Subj3ctXmlHandler handler = new Subj3ctXmlHandler();
        parser.parse(new InputSource(new StringReader(result)), handler);

        List<Subje3ctResult> resultList = handler.getResultList();
        if (resultList.size() == 0) {
            return Collections.emptySet();
        }

        Set<PSIProviderResult> resultSet = new HashSet<PSIProviderResult>(resultList.size());
        for (Subje3ctResult r : resultList) {
            String description = "";
            if (r.name != null)
                description = "Name: " + r.name + "\n";
            if (r.description != null)
                description += "Description: " + r.description + "\n";

            description += "\n\nThis service is provided by http://www.subj3ct.com";

            resultSet.add(new PSIProviderResult(r.identifier, description));
        }

        return Collections.unmodifiableSet(resultSet);
    } catch (UnknownHostException e) {
        // no http connection -> no results
        TmcleditEditPlugin.logInfo(e);
        return Collections.emptySet();
    } catch (SocketTimeoutException e) {
        // timeout -> no results
        TmcleditEditPlugin.logInfo(e);
        return Collections.emptySet();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (method != null)
            method.releaseConnection();
    }
}

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

public Token GET_Token(String email_or_username, String password) throws IOException, Exception {
    Token token = null;// www  .j a va  2s.c o  m
    Gson gson = new Gson();

    String data = "data=" + email_or_username + "&password=" + password;
    GetMethod method = new GetMethod(api_url + "/api/v1/token?" + data);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    int statusCode = client.executeMethod(method);
    if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + method.getStatusLine());
        throw new Exception("The username or password are invalid");
    }

    byte[] responseBody = method.getResponseBody();
    String response = new String(responseBody, "UTF-8");
    token = gson.fromJson(response, Token.class);
    System.out.println("algo");

    return token;
}

From source file:de.knurt.heinzelmann.util.urlcontent.HttpsUnsecuredContent.java

@SuppressWarnings("deprecation")
@Override//from   ww  w.  j  a  v  a  2 s.c  o m
public String getContent(String urlstring) throws IOException, HTTPException {
    Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
    Protocol.registerProtocol("https", easyhttps);

    HttpClient client = new HttpClient();
    GetMethod httpget = new GetMethod(urlstring);
    client.executeMethod(httpget);
    return httpget.getResponseBodyAsString();
}

From source file:gdv.xport.util.URLReader.java

/**
 * Liest die komplette URL und gibt die gelesen Seite als String zurueck.
 *
 * @return gelesene Seite/*from  ww  w . j a  v  a 2s .  co m*/
 * @throws IOException falls die URL nicht erreichbar ist
 */
public String read() throws IOException {
    try {
        HttpClient httpClient = new HttpClient();
        GetMethod get = new GetMethod(url.toString());
        httpClient.executeMethod(get);
        String content = get.getResponseBodyAsString();
        get.releaseConnection();
        return content;
    } catch (IllegalStateException ise) {
        LOG.info(ise + " - fallback to URLConnection");
        URLConnection connection = url.openConnection();
        return read(connection);
    }
}

From source file:com.rometools.propono.atom.client.ClientCategories.java

public void fetchContents() throws ProponoException {
    final GetMethod method = new GetMethod(getHrefResolved());
    clientCollection.addAuthentication(method);
    try {/*from  w  ww.  ja  v  a  2s.com*/
        clientCollection.getHttpClient().executeMethod(method);
        if (method.getStatusCode() != 200) {
            throw new ProponoException("ERROR HTTP status code=" + method.getStatusCode());
        }
        final SAXBuilder builder = new SAXBuilder();
        final Document catsDoc = builder.build(new InputStreamReader(method.getResponseBodyAsStream()));
        parseCategoriesElement(catsDoc.getRootElement());

    } catch (final IOException ioe) {
        throw new ProponoException("ERROR: reading out-of-line categories", ioe);
    } catch (final JDOMException jde) {
        throw new ProponoException("ERROR: parsing out-of-line categories", jde);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.sun.syndication.propono.atom.client.ClientCategories.java

public void fetchContents() throws ProponoException {
    GetMethod method = new GetMethod(getHrefResolved());
    clientCollection.addAuthentication(method);
    try {// ww w  . ja va 2s  .c  o  m
        clientCollection.getHttpClient().executeMethod(method);
        if (method.getStatusCode() != 200) {
            throw new ProponoException("ERROR HTTP status code=" + method.getStatusCode());
        }
        SAXBuilder builder = new SAXBuilder();
        Document catsDoc = builder.build(new InputStreamReader(method.getResponseBodyAsStream()));
        parseCategoriesElement(catsDoc.getRootElement());

    } catch (IOException ioe) {
        throw new ProponoException("ERROR: reading out-of-line categories", ioe);
    } catch (JDOMException jde) {
        throw new ProponoException("ERROR: parsing out-of-line categories", jde);
    } finally {
        method.releaseConnection();
    }
}

From source file:fr.openwide.talendalfresco.rest.client.RestHttpTest.java

public void testRestAuthentication() {
    // Create an instance of HttpClient.
    HttpClient client = new HttpClient();

    // Create a method instance.
    GetMethod method = new GetMethod("http://www.google.com");

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

    try {/*ww  w . ja  va 2 s.c  o m*/
        // Execute the method.
        int statusCode = client.executeMethod(method);

        assertEquals(statusCode, HttpStatus.SC_OK);

        // Read the response body.
        byte[] responseBody = method.getResponseBody();

        assertTrue(responseBody != null && responseBody.length != 0);

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

    } catch (HttpException e) {
        fail("Fatal protocol violation: " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        fail("Fatal transport error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
}

From source file:colt.nicity.performance.latent.http.ApacheHttpClient.java

public HttpResponse get(String path) {
    GetMethod get = new GetMethod(path);
    try {/*from ww  w  .ja  v a 2  s  . c  o m*/
        return execute(get);
    } catch (Exception e) {
        throw new RuntimeException("Error executing GET request to: "
                + client.getHostConfiguration().getHostURL() + " path: " + path, e);
    }
}

From source file:com.robonobo.common.io.HttpInputStream.java

protected HttpMethod createMethod(String location) {
    return new GetMethod(location);
}

From source file:com.predic8.membrane.core.interceptor.AdjustContentLengthIntegrationTest.java

private GetMethod getDirektRequest() {
    GetMethod get = new GetMethod("http://thomas-bayer.com/samples/sqlrest/CUSTOMER/7/");
    return get;
}