Example usage for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient

List of usage examples for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient.

Prototype

public DefaultHttpClient(final HttpParams params) 

Source Link

Usage

From source file:Main.java

public static String getHttpClientString(String path) {
    BasicHttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
    HttpClient client = new DefaultHttpClient(httpParams);
    DefaultHttpClient httpClient = (DefaultHttpClient) client;
    HttpResponse httpResponse = null;//w  ww  .  j  av  a 2  s.  c  o m
    String result = "";
    try {
        httpResponse = httpClient.execute(new HttpGet(path));
        int res = httpResponse.getStatusLine().getStatusCode();
        if (res == 200) {
            InputStream in = httpResponse.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in, "GBK"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line.trim());
            }
            reader.close();
            in.close();
            result = sb.toString();
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static String getResultPost(String uri, List<NameValuePair> params) {
    String Result = null;//from   w ww  .j  a va 2 s  .co m
    try {
        if (uri == null) {
            return "";
        }
        HttpPost httpRequest = new HttpPost(uri);
        BasicHttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
        DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
        httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        HttpResponse httpResponse = httpClient.execute(httpRequest);
        int res = httpResponse.getStatusLine().getStatusCode();
        if (res == 200) {

            StringBuilder builder = new StringBuilder();
            BufferedReader bufferedReader2 = new BufferedReader(
                    new InputStreamReader(httpResponse.getEntity().getContent()));
            for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2.readLine()) {
                builder.append(s);
            }
            Result = builder.toString();

        }

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block

        e.printStackTrace();
        return "";
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "";
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return Result;
}

From source file:fr.lemet.application.util.HttpUtils.java

public static HttpClient getHttpClient() {
    HttpParams myHttpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myHttpParams, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(myHttpParams, CONNECTION_TIMEOUT);
    return new DefaultHttpClient(myHttpParams);
}

From source file:kr.pe.javarss.mybus.task.GBusPageParser.java

public static InputStream getPageInputStream(String url) throws IOException {

    //    :   30   ?
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, CONNECTION_TIMEOUT);

    HttpClient hc = new DefaultHttpClient(params);
    HttpResponse res = hc.execute(new HttpGet(url));
    if (res.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new UnknownServiceException();
    }/*  w  w w .  j ava2  s.com*/

    return res.getEntity().getContent();
}

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

public static String doGetHttp(DSCrawlerUrl pageUrl) {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 12000);
    HttpConnectionParams.setSoTimeout(params, 9000);
    HttpClient httpclient = new DefaultHttpClient(params);
    String rs = "";
    try {//from   w  w w .  j a  v  a2  s  .com
        HttpGet httpget = new HttpGet(pageUrl.url);
        //            System.out.println("executing request " + pageUrl.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);
        pageUrl.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:neembuu.release1.settings.OnlineSettingImpl.java

public static String getRaw(String... p) {
    //Get the version.xml and read the version value.
    HttpParams params = new BasicHttpParams();
    params.setParameter("http.useragent",
            "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6");
    DefaultHttpClient httpclient = new DefaultHttpClient(params);
    String pth = "";
    for (String p_ele : p) {
        pth = pth + "/" + p_ele;
    }/*from ww w.  ja  v a 2s  . c o m*/
    HttpGet httpget = new HttpGet("http://neembuu.sourceforge.net" + pth);
    LoggerUtil.L().log(Level.INFO, "Getting online setting ...{0}", p);
    try {
        HttpResponse response = httpclient.execute(httpget);
        String respxml = EntityUtils.toString(response.getEntity());
        return respxml;
    } catch (Exception ex) {
        LoggerUtil.L().log(Level.INFO, "Exception while getting resource " + p, ex);
    }

    return null;
}

From source file:hu.javaforum.android.soap.ssl.HttpsClientFactory.java

/**
 * Creates a DefaultHttpClient instance.
 * //  w  w w . j  a v  a  2 s . c  o m
 * @param params
 *            The HttpParams
 * @return The DefaultHttpClient implementation
 */
public static HttpClient createDefaultInstance(final HttpParams params) {
    try {
        return new DefaultHttpClient(params);
    } finally {
    }
}

From source file:artsmedia.ebikes.network.EBikesHttpClientFactory.java

public static HttpClient create() {
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used. 
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 5000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    return new DefaultHttpClient(httpParameters);
}

From source file:com.terremark.AbstractCloudApiAuthTestBase.java

/**
 * TODO/*from   w  w w .  j a  va 2  s.c  om*/
 *
 * @throws Exception If there is a problem authenticating using Cloud API auth.
 */
@BeforeClass
public static void cloudApiAuth() throws Exception {
    ThreadSafeClientConnManager httpConnectionManager = new ThreadSafeClientConnManager(
            SchemeRegistryFactory.createDefault());
    httpConnectionManager.setMaxTotal(10);
    httpConnectionManager.setDefaultMaxPerRoute(10);

    final PropertiesBuilder props = new PropertiesBuilder().setEndPoint(ENDPOINT_URL).setAccessKey(ACCESS_KEY)
            .setAPIVersion(VERSION).setContentType(CONTENT_TYPE).setPrivateKey(PRIVATE_KEY)
            .setHttpClient(new DefaultHttpClient(httpConnectionManager));
    client = TerremarkFactory.getClient(props);
}

From source file:dk.kk.ibikecphlib.search.HTTPAutocompleteHandler.java

@SuppressLint("NewApi")

public static JsonNode performGET(String urlString) {
    JsonNode ret = null;/*from  w ww  . j a  v a  2 s. c om*/

    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, 20000);
    HttpConnectionParams.setSoTimeout(myParams, 20000);
    HttpClient httpclient = new DefaultHttpClient(myParams);
    HttpGet httpget = null;

    URL url = null;

    try {

        url = new URL(urlString);
        httpget = new HttpGet(url.toString());
        LOG.d("Request " + url.toString());
        httpget.setHeader("Content-type", "application/json");
        HttpResponse response = httpclient.execute(httpget);
        String serverResponse = EntityUtils.toString(response.getEntity());
        LOG.d("Response " + serverResponse);
        ret = Util.stringToJsonNode(serverResponse);

    } catch (Exception e) {
        if (e != null && e.getLocalizedMessage() != null)
            LOG.e(e.getLocalizedMessage());
    }
    return ret;
}