Example usage for org.apache.http.params HttpParams setParameter

List of usage examples for org.apache.http.params HttpParams setParameter

Introduction

In this page you can find the example usage for org.apache.http.params HttpParams setParameter.

Prototype

HttpParams setParameter(String str, Object obj);

Source Link

Usage

From source file:com.roamprocess1.roaming4world.syncadapter.SyncAdapter.java

public boolean websericeR4WContacts() {

    String contacts_returned = "", r4wName = "", r4wUserImage = "", r4wstatus = "";
    System.out.println("getAllContacts:" + getAllContacts.length);

    HttpParams p = new BasicHttpParams();
    p.setParameter("user", "1");

    HttpClient httpclient = new DefaultHttpClient(p);

    String url = "http://ip.roaming4world.com/esstel/fetch_contacts_file.php?" + "self_contact=" + selfNumber;

    HttpPost httppost = new HttpPost(url);
    System.out.println("URL being sent is " + url);

    try {/*  w ww.j  a  v  a  2s .co  m*/
        Log.i(getClass().getSimpleName(), "send  task - start");

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("user", "1"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httppost, responseHandler);
        System.out.println("Response Body is " + responseBody);

        // Parse
        JSONObject jsonObjRecv;
        try {
            JSONArray jsonArray = null;
            jsonObjRecv = new JSONObject(responseBody);
            Log.d(TAG, "jsonObjRecv :" + jsonObjRecv);

            String contacts = jsonObjRecv.getString("contacts");
            if (contacts.equals("Error") || contacts.equals("No Contacts")) {
                Log.d(TAG, "No contacts Error");
                prefs.edit().putString(signUpProcess, "R4wContactInserted").commit();
            } else {
                jsonArray = jsonObjRecv.getJSONArray("contacts");
                String path = jsonObjRecv.getString("path");
                Log.d(TAG, "jsonArray :" + jsonArray + "path:" + path);
                if (jsonArray.length() != 0) {
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject position = jsonArray.getJSONObject(i);
                        contacts_returned += position.getString("p") + ",";
                        r4wName += position.getString("un") + ",";
                        r4wstatus += position.getString("s") + ",";
                        String url_image = position.getString("t");

                        if (!url_image.equals("No image")) {
                            r4wUserImage += path + url + ",";
                        } else {
                            r4wUserImage += position.getString("t") + ",";
                        }

                        if (jsonArray.getString(i) == "") {

                        }
                    }
                    System.out.println("data=:contacts_returned :r4wName:r4wstatus :r4wUserImage"
                            + contacts_returned + ":" + r4wName + ":" + r4wstatus + "" + r4wUserImage + "");
                    System.out.println();
                }
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //return true;
    } catch (UnknownHostException e) {
        e.printStackTrace();
        return false;
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    } catch (Exception e) {
        //e.printStackTrace();
        return false;
    }

    if (contacts_returned.length() != 0) {
        contacts_returned = contacts_returned.substring(0, contacts_returned.length() - 1);
        r4wName = r4wName.substring(0, r4wName.length() - 1);
        r4wstatus = r4wstatus.substring(0, r4wstatus.length() - 1);
        r4wUserImage = r4wUserImage.substring(0, r4wUserImage.length() - 1);
        contact_array = contacts_returned.split(",");
        r4wNameArray = r4wName.split(",");
        r4wUserStatus = r4wstatus.split(",");
        r4wUserImageArray = r4wUserImage.split(",");

        RContactlist.r4wCompleteContactList = new String[contact_array.length];

        com.roamprocess1.roaming4world.api.Roaming4WorldCustomApi.r4wContacts = contact_array;
        for (int i = 0; i < com.roamprocess1.roaming4world.api.Roaming4WorldCustomApi.r4wContacts.length; i++) {
            String number = com.roamprocess1.roaming4world.api.Roaming4WorldCustomApi.r4wContacts[i];
            //System.out.println("number :"+number);
            int index = r4wContactListName.indexOf(number.trim());
            //System.out.println("index ="+index);

            try {
                if (index != -1) {
                    System.out.println("index =" + index);
                    String r4wContactName = allContactsPhone[index];
                    String[] parts = r4wContactName.split(",");
                    String r4wNamePhone = parts[0];
                    String r4wNumberName = parts[1];
                    String datacomplte = r4wNamePhone + "," + r4wNumberName + "," + r4wUserStatus[i] + ","
                            + r4wUserImageArray[i] + "," + r4wNameArray[i];
                    System.out.println("datacomplte-" + datacomplte);
                    RContactlist.r4wCompleteContactList[i] = datacomplte;
                }

            } catch (Exception e) {
                // TODO: handle exception
            }

        }
        System.out.println("r4wCompleteContactList.length:" + RContactlist.r4wCompleteContactList.length);

    } else {
        System.out.println("No Contact");
    }

    return true;

}

From source file:org.apache.awf.web.SystemTest.java

@Test
public void wwTest() throws ClientProtocolException, IOException {

    List<Header> headers = new LinkedList<Header>();
    headers.add(new BasicHeader("Connection", "Close"));

    HttpParams params = new BasicHttpParams();
    params.setParameter("http.default-headers", headers);

    HttpClient httpclient = new DefaultHttpClient(params);
    HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/ww");
    HttpResponse response = httpclient.execute(httpget);

    assertNotNull(response);// w  ww  . ja v a  2 s . co m
    assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
    assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
    assertEquals("OK", response.getStatusLine().getReasonPhrase());

    String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
    assertEquals("12", payLoad);
    assertEquals(5, response.getAllHeaders().length);
    assertEquals("2", response.getFirstHeader("Content-Length").getValue());
}

From source file:org.apache.awf.web.SystemTest.java

/**
 * Test a RH that does a single write//from   w  ww  . j a  v  a  2 s.c  o  m
 * 
 * @throws ClientProtocolException
 * @throws IOException
 */
@Test
public void wTest() throws ClientProtocolException, IOException {

    List<Header> headers = new LinkedList<Header>();
    headers.add(new BasicHeader("Connection", "Close"));

    HttpParams params = new BasicHttpParams();
    params.setParameter("http.default-headers", headers);

    HttpClient httpclient = new DefaultHttpClient(params);
    HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/w");
    HttpResponse response = httpclient.execute(httpget);

    assertNotNull(response);
    assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
    assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
    assertEquals("OK", response.getStatusLine().getReasonPhrase());

    String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
    assertEquals("1", payLoad);
    assertEquals(5, response.getAllHeaders().length);
    assertEquals("1", response.getFirstHeader("Content-Length").getValue());
}

From source file:org.apache.awf.web.SystemTest.java

private void doSimpleGetRequest() throws ClientProtocolException, IOException {

    List<Header> headers = new LinkedList<Header>();
    headers.add(new BasicHeader("Connection", "Close"));

    HttpParams params = new BasicHttpParams();
    params.setParameter("http.default-headers", headers);

    HttpClient httpclient = new DefaultHttpClient(params);
    HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/");
    HttpResponse response = httpclient.execute(httpget);
    List<String> expectedHeaders = Arrays
            .asList(new String[] { "Server", "Date", "Content-Length", "Etag", "Connection" });

    assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
    assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
    assertEquals("OK", response.getStatusLine().getReasonPhrase());

    assertEquals(expectedHeaders.size(), response.getAllHeaders().length);

    for (String header : expectedHeaders) {
        assertTrue(response.getFirstHeader(header) != null);
    }//from   w  ww  .ja  v  a  2  s. co m

    assertEquals(expectedPayload, convertStreamToString(response.getEntity().getContent()).trim());
    assertEquals(expectedPayload.length() + "", response.getFirstHeader("Content-Length").getValue());
}

From source file:io.hops.hopsworks.api.kibana.ProxyServlet.java

/**
 * Reads a servlet config parameter by the name {@code hcParamName} of type
 * {@code type}, and//w w w .  j  a v a2 s. c o m
 * set it in {@code hcParams}.
 *
 * @param hcParams
 * @param hcParamName
 * @param type
 */
protected void readConfigParam(HttpParams hcParams, String hcParamName, Class type) {
    String val_str = getConfigParam(hcParamName);
    if (val_str == null) {
        return;
    }
    Object val_obj;
    if (type == String.class) {
        val_obj = val_str;
    } else {
        try {
            //noinspection unchecked
            val_obj = type.getMethod("valueOf", String.class).invoke(type, val_str);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    hcParams.setParameter(hcParamName, val_obj);
}

From source file:org.opendatakit.aggregate.externalservice.AbstractExternalService.java

protected HttpResponse sendHttpRequest(String method, String url, HttpEntity entity,
        List<NameValuePair> qparams, CallingContext cc) throws IOException {

    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, SOCKET_ESTABLISHMENT_TIMEOUT_MILLISECONDS);
    HttpConnectionParams.setSoTimeout(httpParams, SERVICE_TIMEOUT_MILLISECONDS);

    // setup client
    HttpClientFactory factory = (HttpClientFactory) cc.getBean(BeanDefs.HTTP_CLIENT_FACTORY);
    HttpClient client = factory.createHttpClient(httpParams);

    // support redirecting to handle http: => https: transition
    HttpClientParams.setRedirecting(httpParams, true);
    // support authenticating
    HttpClientParams.setAuthenticating(httpParams, true);

    // redirect limit is set to some unreasonably high number
    // resets of the socket cause a retry up to MAX_REDIRECTS times,
    // so we should be careful not to set this too high...
    httpParams.setParameter(ClientPNames.MAX_REDIRECTS, 32);
    httpParams.setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);

    // context holds authentication state machine, so it cannot be
    // shared across independent activities.
    HttpContext localContext = new BasicHttpContext();

    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    localContext.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider);

    HttpUriRequest request = null;//from   w  ww  .  j av a  2s . c  om
    if (entity == null && (POST.equals(method) || PATCH.equals(method) || PUT.equals(method))) {
        throw new IllegalStateException("No body supplied for POST, PATCH or PUT request");
    } else if (entity != null && !(POST.equals(method) || PATCH.equals(method) || PUT.equals(method))) {
        throw new IllegalStateException("Body was supplied for GET or DELETE request");
    }

    URI nakedUri;
    try {
        nakedUri = new URI(url);
    } catch (Exception e) {
        e.printStackTrace();
        throw new IllegalStateException(e);
    }

    if (qparams == null) {
        qparams = new ArrayList<NameValuePair>();
    }
    URI uri;
    try {
        uri = new URI(nakedUri.getScheme(), nakedUri.getUserInfo(), nakedUri.getHost(), nakedUri.getPort(),
                nakedUri.getPath(), URLEncodedUtils.format(qparams, HtmlConsts.UTF8_ENCODE), null);
    } catch (URISyntaxException e1) {
        e1.printStackTrace();
        throw new IllegalStateException(e1);
    }
    System.out.println(uri.toString());

    if (GET.equals(method)) {
        HttpGet get = new HttpGet(uri);
        request = get;
    } else if (DELETE.equals(method)) {
        HttpDelete delete = new HttpDelete(uri);
        request = delete;
    } else if (PATCH.equals(method)) {
        HttpPatch patch = new HttpPatch(uri);
        patch.setEntity(entity);
        request = patch;
    } else if (POST.equals(method)) {
        HttpPost post = new HttpPost(uri);
        post.setEntity(entity);
        request = post;
    } else if (PUT.equals(method)) {
        HttpPut put = new HttpPut(uri);
        put.setEntity(entity);
        request = put;
    } else {
        throw new IllegalStateException("Unexpected request method");
    }

    HttpResponse resp = client.execute(request);
    return resp;
}

From source file:com.google.gwt.jolokia.server.servlet.ProxyServlet.java

/**
 * Reads a servlet config parameter by the name {@code hcParamName} of type
 * {@code type}, and set it in {@code hcParams}.
 *///from w w w  .  j  av a 2 s .c o  m
protected void readConfigParam(HttpParams hcParams, String hcParamName, Class type) {
    String val_str = getConfigParam(hcParamName);
    if (val_str == null)
        return;
    Object val_obj;
    if (type == String.class) {
        val_obj = val_str;
    } else {
        try {
            // noinspection unchecked
            val_obj = type.getMethod("valueOf", String.class).invoke(type, val_str);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    hcParams.setParameter(hcParamName, val_obj);
}

From source file:org.expath.httpclient.impl.ApacheHttpConnection.java

/**
 * Make a new Apache HTTP client, in order to serve this request.
 *//*from  w  w w . j a v  a  2 s.  c  o m*/
private AbstractHttpClient makeClient() {
    AbstractHttpClient client = new DefaultHttpClient();
    HttpParams params = client.getParams();
    // use the default JVM proxy settings (http.proxyHost, etc.)
    HttpRoutePlanner route = new ProxySelectorRoutePlanner(client.getConnectionManager().getSchemeRegistry(),
            ProxySelector.getDefault());
    client.setRoutePlanner(route);
    // do follow redirections?
    params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, myFollowRedirect);
    // set the timeout if any
    if (myTimeout != null) {
        // See http://blog.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/
        HttpConnectionParams.setConnectionTimeout(params, myTimeout * 1000);
        HttpConnectionParams.setSoTimeout(params, myTimeout * 1000);
    }
    // the shared cookie store
    client.setCookieStore(COOKIES);
    // the HTTP version (1.0 or 1.1)
    params.setParameter("http.protocol.version", myVersion);
    // return the just built client
    return client;
}

From source file:groovyx.net.http.HTTPBuilder.java

/**
 * Creates a new instance with a <code>null</code> default URI.
 *///  w  w  w  .  j ava  2 s .  co m
public HTTPBuilder() {
    super();
    HttpParams defaultParams = new BasicHttpParams();
    defaultParams.setParameter(CookieSpecPNames.DATE_PATTERNS,
            Arrays.asList("EEE, dd-MMM-yyyy HH:mm:ss z", "EEE, dd MMM yyyy HH:mm:ss z"));
    this.client = this.createClient(defaultParams);
    this.setContentEncoding(ContentEncoding.Type.GZIP, ContentEncoding.Type.DEFLATE);
}

From source file:httpmultiplexer.httpproxy.ProxyServlet.java

/**
 * Reads a servlet config parameter by the name {@code hcParamName} of type
 * {@code type}, and set it in {@code hcParams}.
 *//*from w w  w  .  j av  a 2s  . c om*/
protected void readConfigParam(HttpParams hcParams, String hcParamName, Class type) {
    //String val_str = getServletConfig().getInitParameter(hcParamName);
    String val_str = Config.getParam("main", hcParamName, "None");
    if ("None".equals(val_str)) {
        return;
    }
    Object val_obj;
    if (type == String.class) {
        val_obj = val_str;
    } else {
        try {
            //noinspection unchecked
            val_obj = type.getMethod("valueOf", String.class).invoke(type, val_str);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    hcParams.setParameter(hcParamName, val_obj);
}