Example usage for org.apache.http.client.methods HttpGet HttpGet

List of usage examples for org.apache.http.client.methods HttpGet HttpGet

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpGet HttpGet.

Prototype

public HttpGet(final String uri) 

Source Link

Usage

From source file:org.sonar.plugins.buildstability.ci.jenkins.JenkinsUtils.java

public static void doLogin(HttpClient client, String hostName, String username, String password)
        throws IOException {
    String hudsonLoginEntryUrl = hostName + "/login";
    HttpGet loginLink = new HttpGet(hudsonLoginEntryUrl);
    HttpResponse response = client.execute(loginLink);
    checkResult(response.getStatusLine().getStatusCode(), hudsonLoginEntryUrl);
    EntityUtils.consume(response.getEntity());

    String location = hostName + "/j_acegi_security_check";
    boolean loggedIn = false;
    while (!loggedIn) {
        HttpPost loginMethod = new HttpPost(location);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("j_username", username));
        nvps.add(new BasicNameValuePair("j_password", password));
        nvps.add(new BasicNameValuePair("action", "login"));
        loginMethod.setEntity(new UrlEncodedFormEntity(nvps));
        try {//from  w ww.  j  a  v a2 s  .  c  o m
            HttpResponse response2 = client.execute(loginMethod);
            if (response2.getStatusLine().getStatusCode() / 100 == 3) {
                // Commons HTTP client refuses to handle redirects for POST
                // so we have to do it manually.
                location = response2.getFirstHeader("Location").getValue();
            } else {
                checkResult(response2.getStatusLine().getStatusCode(), location);
                loggedIn = true;
            }
            EntityUtils.consume(response2.getEntity());
        } finally {
            loginMethod.releaseConnection();
        }
    }
}

From source file:com.turn.ttorrent.common.Utils.java

/**
 * Resolves a file from URI and returns its byte array.
 * //from w  w w.j av  a2s.c  om
 * @param uri
 * @return
 * @throws ClientProtocolException
 * @throws IOException
 */
public static byte[] resolveUrlFileToByteArray(URI uri) throws ClientProtocolException, IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();

    try {

        HttpGet get = new HttpGet(uri);
        HttpResponse response = httpclient.execute(get);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            InputStream inputStream = entity.getContent();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            IOUtils.copy(inputStream, outputStream);
            return outputStream.toByteArray();
        }

        throw new IOException("Could not resolve file... [" + uri + "]");

    } finally {
        httpclient.close();
    }

}

From source file:simple.crawler.http.HttpClientUtil.java

public static String fetch(HttpClient httpclient, String uri) throws Exception {
    if (httpclient == null) {
        throw new NullPointerException();
    }// w ww  .j ava 2 s  .com
    if (uri == null) {
        throw new NullPointerException();
    }
    HttpGet get = new HttpGet(uri);
    return getContentBodyAsString(httpclient.execute(get));
}

From source file:JsonParser.java

public JSONObject getJSONFromUrl(String url) {
    // make HTTP request
    try {/*from   ww w .jav  a 2 s.  com*/
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream inputStream = httpEntity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);

        JSONObject jsonObject = createJsonObject(reader);
        inputStream.close();

        return jsonObject;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.apache.hyracks.tests.integration.TestUtil.java

static InputStream httpGetAsInputStream(URI uri) throws URISyntaxException, IOException {
    HttpClient client = HttpClients.createMinimal();
    HttpResponse response = client.execute(new HttpGet(uri));
    return response.getEntity().getContent();
}

From source file:com.intellij.translation.translator.TranslatorUtil.java

public static String fetchInfo(String query, TranslatorEx translator) {
    final CloseableHttpClient client = TranslatorUtil.createClient();
    try {/*from   w  ww  .j  a va  2s .co m*/
        final URI queryUrl = translator.createUrl(query);
        HttpGet httpGet = new HttpGet(queryUrl);
        HttpResponse response = client.execute(httpGet);
        int status = response.getStatusLine().getStatusCode();
        if (status >= 200 && status < 300) {
            HttpEntity resEntity = response.getEntity();
            return translator.generateSuccess(resEntity);
        } else {
            return translator.generateFail(response);
        }
    } catch (Exception ignore) {
    } finally {
        try {
            client.close();
        } catch (IOException ignore) {
        }
    }
    return null;
}

From source file:functional.CookieTest.java

/**
 * Call a handler that will return no cookie
 *
 * @throws Exception/*from w w w .  j av  a  2 s .  c o m*/
 */
@Test
public void testNoCookies() throws Exception {
    HttpGet httpget = new HttpGet("http://localhost:8080/noCookie");
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    httpclient.execute(httpget, responseHandler);

    CookieStore cookies = httpclient.getCookieStore();
    assertEquals(0, cookies.getCookies().size());
}

From source file:language_engine.HttpUtils.java

public static String doHttpGet(String url, Header[] headers) {
    try {/*from w w w  .j av  a  2s.c o  m*/
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeaders(headers);
        HttpResponse resp = httpClient.execute(httpGet);

        return StreamUtil.readText(resp.getEntity().getContent(), "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.opencastproject.remotetest.server.resource.FilesResources.java

public static HttpResponse getFile(TrustedHttpClient client, String mediaPackageID,
        String mediaPackageElementID, String fileName) throws Exception {
    return client.execute(new HttpGet(
            getServiceUrl() + "mediapackage/" + mediaPackageID + '/' + mediaPackageElementID + "/" + fileName));
}

From source file:nl.mineleni.cbsviewer.jsp.AboutJSPIntegrationTest.java

/**
 * testcase voor about.jsp.//from ww  w. ja  v a  2s.  c  o  m
 * 
 * @throws Exception
 */
@Override
@Test
public void testIfValidResponse() throws Exception {
    response = client.execute(new HttpGet(BASE_TEST_URL + "about.jsp"));

    assertThat("Response status is OK.", response.getStatusLine().getStatusCode(), equalTo(SC_OK));

    boilerplateValidationTests(response);
}