Example usage for org.apache.http.client HttpClient getParams

List of usage examples for org.apache.http.client HttpClient getParams

Introduction

In this page you can find the example usage for org.apache.http.client HttpClient getParams.

Prototype

@Deprecated
HttpParams getParams();

Source Link

Document

Obtains the parameters for this client.

Usage

From source file:Main.java

/**
 * Op Http post request , "404error" response if failed
 * /*w  ww.  ja  v a 2s .  c o  m*/
 * @param url
 * @param map
 *            Values to request
 * @return
 */

static public String doHttpPost(String url, HashMap<String, String> map) {

    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), TIMEOUT);
    HttpConnectionParams.setSoTimeout(client.getParams(), TIMEOUT);
    ConnManagerParams.setTimeout(client.getParams(), TIMEOUT);
    HttpPost post = new HttpPost(url);
    post.setHeaders(headers);
    String result = "ERROR";
    ArrayList<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
    if (map != null) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            BasicNameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());
            pairList.add(pair);
        }

    }
    try {
        HttpEntity entity = new UrlEncodedFormEntity(pairList, "UTF-8");
        post.setEntity(entity);
        HttpResponse response = client.execute(post);

        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

            result = EntityUtils.toString(response.getEntity(), "UTF-8");

        } else {
            result = EntityUtils.toString(response.getEntity(), "UTF-8")
                    + response.getStatusLine().getStatusCode() + "ERROR";
        }

    } catch (ConnectTimeoutException e) {
        result = "TIMEOUTERROR";
    }

    catch (Exception e) {
        result = "OTHERERROR";
        e.printStackTrace();

    }
    return result;
}

From source file:com.mothsoft.alexis.util.NetworkingUtil.java

private static HttpClient getClient() {
    final HttpClient client = new DefaultHttpClient();
    client.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
    client.getParams().setParameter("http.socket.timeout", new Integer(300000));
    client.getParams().setParameter("http.protocol.content-charset", "UTF-8");
    return client;
}

From source file:com.sunhz.projectutils.httputils.HttpClientUtils.java

/**
 * get access to the network, you need to use in the sub-thread
 *
 * @param url url// ww  w.  j a  va  2s .  c o  m
 * @return response inputStream
 * @throws IOException In the case of non-200 status code is returned, it would have thrown
 */
public static InputStream getInputStream(String url) throws IOException {
    org.apache.http.client.HttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
            Constant.TimeInApplication.NET_TIMEOUT);
    client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, Constant.TimeInApplication.NET_TIMEOUT);
    HttpGet request = new HttpGet(url);
    HttpResponse response = client.execute(request);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        return response.getEntity().getContent();
    } else {
        throw new IllegalArgumentException("getInputStreamInSubThread response status code is "
                + response.getStatusLine().getStatusCode());
    }
}

From source file:Main.java

static public String doHttpGet(String url, HashMap<String, String> map) {

    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), TIMEOUT);
    HttpConnectionParams.setSoTimeout(client.getParams(), TIMEOUT);
    ConnManagerParams.setTimeout(client.getParams(), TIMEOUT);
    String result = "ERROR";
    if (null != map) {
        int i = 0;
        for (Map.Entry<String, String> entry : map.entrySet()) {

            if (i == 0) {
                url = url + "?" + entry.getKey() + "=" + entry.getValue();
            } else {
                url = url + "&" + entry.getKey() + "=" + entry.getValue();
            }// w  w w  .j  a va  2s .  co m

            i++;

        }
    }
    HttpGet get = new HttpGet(url);
    get.setHeaders(headers);
    try {

        HttpResponse response = client.execute(get);

        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            // setCookie(response);
            result = EntityUtils.toString(response.getEntity(), "UTF-8");

        } else {
            result = EntityUtils.toString(response.getEntity(), "UTF-8")
                    + response.getStatusLine().getStatusCode() + "ERROR";
        }
        if (result != null) {
            if (result.startsWith("\ufeff")) {
                result = result.substring(1);
            }
        }

    } catch (ConnectTimeoutException e) {
        result = "TIMEOUTERROR";
    }

    catch (Exception e) {
        result = "OTHERERROR";
        e.printStackTrace();

    }

    return result;
}

From source file:com.sunhz.projectutils.httputils.HttpClientUtils.java

/**
 * post access to the network, you need to use in the sub-thread
 *
 * @param url    url//w  ww. ja  v  a  2s. com
 * @param params params
 * @return response inputStream
 * @throws IOException In the case of non-200 status code is returned, it would have thrown
 */
public static InputStream postInputStream(String url, Map<String, String> params) throws IOException {
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    if (params != null && !params.isEmpty()) {
        for (Map.Entry<String, String> entry : params.entrySet()) {
            list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
    }
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list);
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(entity);
    org.apache.http.client.HttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
            Constant.TimeInApplication.NET_TIMEOUT);
    client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, Constant.TimeInApplication.NET_TIMEOUT);
    HttpResponse response = client.execute(httpPost);
    if (response.getStatusLine().getStatusCode() == 200) {
        return response.getEntity().getContent();
    } else {
        throw new IllegalArgumentException("postInputStreamInSubThread response status code is "
                + response.getStatusLine().getStatusCode());
    }

}

From source file:org.dthume.spring.http.client.httpcomponents.HttpComponentsClientRequestFactory.java

private static HttpClient createDefaultClient() {
    final HttpClient client = new DefaultHttpClient(createConnectionManager());

    final HttpParams params = client.getParams();
    params.setIntParameter(SO_TIMEOUT, DEFAULT_READ_TIMEOUT_MILLISECONDS);

    return client;
}

From source file:com.geeksville.location.LeonardoUpload.java

/**
 * Upload a flight to Leonardo//from  w w  w. j a  v  a  2 s  .c  o m
 * 
 * @param username
 * @param password
 * @param postURL
 * @param shortFilename
 * @param igcFile
 *            we will take care of closing this stram
 * @return null for success, otherwise a string description of the problem
 * @throws IOException
 */
public static String upload(String username, String password, String postURL, int competitionClass,
        String shortFilename, String igcFile, int connectionTimeout, int operationTimeout) throws IOException {

    // Strip off extension (leonado docs say they don't want it
    int i = shortFilename.lastIndexOf('.');
    if (i >= 1)
        shortFilename = shortFilename.substring(0, i);
    String sCompetitionClass = String.valueOf(competitionClass);
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeout);
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    HttpConnectionParams.setSoTimeout(httpParameters, operationTimeout);

    HttpClient httpclient = new DefaultHttpClient(httpParameters);
    httpclient.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
    HttpPost httppost = new HttpPost(postURL);
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("user", username));
    nameValuePairs.add(new BasicNameValuePair("pass", password));
    nameValuePairs.add(new BasicNameValuePair("igcfn", shortFilename));
    nameValuePairs.add(new BasicNameValuePair("Klasse", sCompetitionClass));
    nameValuePairs.add(new BasicNameValuePair("IGCigcIGC", igcFile));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();

    String resp = EntityUtils.toString(entity);

    // An error looks like:
    // <html><body>problem<br>This is not a valid .igc
    // file</body></html>

    // Check for success
    if (resp.contains("flight scored"))
        resp = null;
    else {
        int bodLoc = resp.indexOf("<body>");
        if (bodLoc >= 0)
            resp = resp.substring(bodLoc + 6);
        int probLoc = resp.indexOf("problem");
        if (probLoc >= 0)
            resp = resp.substring(probLoc + 7);
        if (resp.startsWith("<br>"))
            resp = resp.substring(4);
        int markLoc = resp.indexOf('<');
        if (markLoc >= 0)
            resp = resp.substring(0, markLoc);
        resp = resp.trim();
    }

    return resp;
}

From source file:io.personium.core.utils.HttpClientFactory.java

/**
 * HTTPClient?.//from ww w  . ja  va  2s  . com
 * @param type 
 * @return ???HttpClient
 */
public static HttpClient create(final String type) {
    if (TYPE_DEFAULT.equalsIgnoreCase(type)) {
        return new DefaultHttpClient();
    }

    SSLSocketFactory sf = null;
    try {
        if (TYPE_INSECURE.equalsIgnoreCase(type)) {
            sf = createInsecureSSLSocketFactory();
        }
    } catch (Exception e) {
        return null;
    }

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("https", PORTHTTPS, sf));
    schemeRegistry.register(new Scheme("http", PORTHTTP, PlainSocketFactory.getSocketFactory()));
    HttpParams params = new BasicHttpParams();
    ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry);
    // ClientConnectionManager cm = new
    // ThreadSafeClientConnManager(schemeRegistry);
    HttpClient hc = new DefaultHttpClient(cm, params);

    HttpParams params2 = hc.getParams();
    int timeout = TIMEOUT;
    HttpConnectionParams.setConnectionTimeout(params2, timeout); // ?
    HttpConnectionParams.setSoTimeout(params2, timeout); // ??
    return hc;
}

From source file:nl.igorski.lib.utils.network.HTTPTransfer.java

private static DefaultHttpClient getClient() {
    if (_httpClient == null) {
        HttpClient client = new DefaultHttpClient();

        _httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(client.getParams(),
                client.getConnectionManager().getSchemeRegistry()), client.getParams());
    }//  ww w.  ja  v a2s  . c o  m
    return _httpClient;
}

From source file:com.qingzhi.apps.fax.client.NetworkUtilities.java

private static String executeGet(String url, Map<String, String> map) throws IOException {

    HttpClient httpclient = new DefaultHttpClient();
    HttpParams params = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, HTTP_CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, HTTP_SO_TIMEOUT);

    HttpPost post = new HttpPost(url);

    ArrayList<BasicNameValuePair> postDate = new ArrayList<BasicNameValuePair>();
    Set<String> set = map.keySet();
    Iterator<String> iterator = set.iterator();
    while (iterator.hasNext()) {
        String key = iterator.next();
        postDate.add(new BasicNameValuePair(key, map.get(key)));
    }/*w w w  . j a va2s . c o m*/
    post.setEntity(new UrlEncodedFormEntity(postDate, HTTP.UTF_8));
    HttpResponse httpResponse = httpclient.execute(post);
    throwErrors(httpResponse);

    HttpEntity httpEntity = httpResponse.getEntity();
    if (httpEntity != null) {
        String response = EntityUtils.toString(httpEntity);
        LOGD(TAG, response);
        return response;
    }
    return null;
}