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:com.qihoo.permmgr.util.d.java

public static String a(String paramString, int paramInt) {
    try {/*  www. jav  a  2s.  c o m*/
        HttpGet localHttpGet = new HttpGet(paramString);
        DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient();
        localDefaultHttpClient.getParams().setParameter("http.connection.timeout", Integer.valueOf(paramInt));
        localDefaultHttpClient.getParams().setParameter("http.socket.timeout", Integer.valueOf(paramInt));
        HttpResponse localHttpResponse = localDefaultHttpClient.execute(localHttpGet);
        if (200 == localHttpResponse.getStatusLine().getStatusCode()) {
            String str = EntityUtils.toString(localHttpResponse.getEntity());
            return str;
        }
    } catch (Exception localException) {
        localException.printStackTrace();
    }
    return null;
}

From source file:com.ea.core.bridge.ws.rest.client.GetClient.java

@Override
protected HttpRequestBase getMethod(String url) {
    // TODO Auto-generated method stub
    return new HttpGet(url);
}

From source file:com.quietlycoding.android.reader.util.api.Tags.java

/**
 * This method pulls the tags from Google Reader, its used by the methods in
 * this class to communicate before parsing the specific results.
 * //w w  w  . j av a  2  s  .  c  om
 * @param sid
 *            the Google Reader authentication string.
 * @return arr JSONArray of the items from the server.
 */
private static JSONArray pullTags(String sid) {
    final DefaultHttpClient client = new DefaultHttpClient();
    final HttpGet get = new HttpGet(URL);
    final BasicClientCookie cookie = Authentication.buildCookie(sid);

    try {
        client.getCookieStore().addCookie(cookie);

        final HttpResponse response = client.execute(get);
        final HttpEntity respEntity = response.getEntity();

        Log.d(TAG, "Response from server: " + response.getStatusLine());

        final InputStream in = respEntity.getContent();
        final BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        String line = "";
        String arr = "";
        while ((line = reader.readLine()) != null) {
            arr += line;
        }

        final JSONObject obj = new JSONObject(arr);
        final JSONArray array = obj.getJSONArray("tags");

        return array;
    } catch (final Exception e) {
        Log.d(TAG, "Exception caught:: " + e.toString());
        return null;
    }
}

From source file:wuit.common.crawler.search.Crawler.java

public static String doGetHttp(String url) {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 12000);
    HttpConnectionParams.setSoTimeout(params, 9000);
    HttpClient httpclient = new DefaultHttpClient(params);
    String rs = "";
    try {/*from   w  w  w .ja v a2 s  . c  o m*/
        //            System.out.println(url);
        HttpGet httpget = new HttpGet(url);

        HttpContext httpContext = new BasicHttpContext();
        //            httpget.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
        httpget.addHeader("User-Agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; .NET CLR 1.1.4322; CIBA; .NET CLR 2.0.50727)");

        HttpResponse response = httpclient.execute(httpget, httpContext);
        HttpUriRequest realRequest = (HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST);
        HttpHost targetHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        url = targetHost.toString() + realRequest.getURI();
        int resStatu = response.getStatusLine().getStatusCode();//? 
        if (resStatu == 200) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                rs = EntityUtils.toString(entity, "iso-8859-1");
                String in_code = getEncoding(rs);
                String encode = getHtmlEncode(rs);
                if (encode.isEmpty()) {
                    httpclient.getConnectionManager().shutdown();
                    return "";
                } else {
                    if (!in_code.toLowerCase().equals("utf-8")
                            && !in_code.toLowerCase().equals(encode.toLowerCase())) {
                        if (!in_code.toLowerCase().equals("iso-8859-1"))
                            rs = new String(rs.getBytes("iso-8859-1"), in_code);
                        if (!encode.toLowerCase().equals(in_code.toLowerCase()))
                            rs = new String(rs.getBytes(in_code), encode);
                    }
                }
                try {
                } catch (RuntimeException ex) {
                    httpget.abort();
                    throw ex;
                } finally {
                    // Closing the input stream will trigger connection release
                    //                    try { instream.close(); } catch (Exception ignore) {}
                }
            }
        }
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
        return rs;
    }
}

From source file:br.pub.tradutor.TradutorController.java

/**
 * Mtodo utilizado para traduo utilizando o Google Translate
 *
 * @param text Texto a ser traduzido/* ww  w . ja  v a  2 s. c  o  m*/
 * @param from Idioma de origem
 * @param to Idioma de destino
 * @return Texto traduzido (no idioma destino)
 * @throws IOException
 */
public static String translate(String text, String from, String to) throws IOException {
    //Faz encode de URL, para fazer escape dos valores que vo na URL
    String encodedText = URLEncoder.encode(text, "UTF-8");

    DefaultHttpClient httpclient = new DefaultHttpClient();

    //Mtodo GET a ser executado
    HttpGet httpget = new HttpGet(String.format(TRANSLATOR, encodedText, from, to));

    //Faz a execuo
    HttpResponse response = httpclient.execute(httpget);

    //Busca a resposta da requisio e armazena em String
    String returnContent = EntityUtils.toString(response.getEntity());

    //Desconsidera tudo depois do primeiro array
    returnContent = returnContent.split("\\],\\[")[0];

    //StringBuilder que sera carregado o retorno
    StringBuilder translatedText = new StringBuilder();

    //Verifica todas as tradues encontradas, e junta todos os trechos
    Matcher m = RESULTS_PATTERN.matcher(returnContent);
    while (m.find()) {
        translatedText.append(m.group(1).trim()).append(' ');
    }

    //Retorna
    return translatedText.toString().trim();

}

From source file:org.lokra.seaweedfs.util.ConnectionUtil.java

/**
 * Check uri link is alive, the basis for judging response status code.
 *
 * @param client httpClient/*from w ww.j  a  v a2 s . com*/
 * @param url    check url
 * @return When the response status code is 200, the result is true.
 */
public static boolean checkUriAlive(CloseableHttpClient client, String url) {
    boolean result = false;
    CloseableHttpResponse response = null;
    HttpGet request = new HttpGet(url);
    try {
        response = client.execute(request, HttpClientContext.create());
        result = response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
    } catch (IOException e) {
        return false;
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException ignored) {
            }
        }
        request.releaseConnection();
    }
    return result;
}

From source file:eu.sisob.uma.NPL.Researchers.Freebase.Utils.java

public static JsonObject doRESTCall(String url) throws IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = httpclient.execute(new HttpGet(url));

    JsonParser parser = new JsonParser();
    JsonObject json_data = (JsonObject) parser.parse(EntityUtils.toString(response.getEntity()));

    return json_data;
}

From source file:WSpatern.DownloadLink.java

public void getLink(String token, String id) {
    try {/* w w w .j  a  va2  s  .  c om*/
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet("http://documenta-dms.com/DMSWS/api/v1/file/" + token + "/link_by_id/" + id);
        HttpResponse response = client.execute(get);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = "";
        while ((line = rd.readLine()) != null) {

            System.out.println(line);
            parseXML(line);

        }
    } catch (IOException ex) {
        Logger.getLogger(ValidTokenWS.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:org.kegbot.app.util.Downloader.java

/**
 * Downloads and returns a URL as a {@link Bitmap}.
 *
 * @param url the image to download//from w w  w .  ja  va2  s.  c o m
 * @return a new {@link Bitmap}, or {@code null} if any error occurred
 */
public static Bitmap downloadBitmap(String url) {
    final HttpClient client = new DefaultHttpClient();
    final HttpGet getRequest = new HttpGet(url);

    try {
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.w(LOG_TAG, "Error " + statusCode + " while retrieving bitmap from " + url);
            return null;
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2;

                inputStream = entity.getContent();
                return BitmapFactory.decodeStream(inputStream, null, options);
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (IOException e) {
        getRequest.abort();
        Log.w(LOG_TAG, "I/O error while retrieving bitmap from " + url, e);
    } catch (IllegalStateException e) {
        getRequest.abort();
        Log.w(LOG_TAG, "Incorrect URL: " + url);
    } catch (Exception e) {
        getRequest.abort();
        Log.w(LOG_TAG, "Error while retrieving bitmap from " + url, e);
    } finally {
        if ((client instanceof AndroidHttpClient)) {
            ((AndroidHttpClient) client).close();
        }
    }
    return null;
}

From source file:com.ppshein.sevendaynews.common.java

public static String getInputStreamFromUrl(String url) {
    String content = null;/*from   www  .  ja  v  a  2  s.c o m*/
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(new HttpGet(url));
        HttpEntity resEntity = response.getEntity();
        content = EntityUtils.toString(resEntity);
        content = content.trim();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }
    return content;
}