Example usage for org.apache.http.client.entity UrlEncodedFormEntity UrlEncodedFormEntity

List of usage examples for org.apache.http.client.entity UrlEncodedFormEntity UrlEncodedFormEntity

Introduction

In this page you can find the example usage for org.apache.http.client.entity UrlEncodedFormEntity UrlEncodedFormEntity.

Prototype

public UrlEncodedFormEntity(final Iterable<? extends NameValuePair> parameters) 

Source Link

Document

Constructs a new UrlEncodedFormEntity with the list of parameters with the default encoding of HTTP#DEFAULT_CONTENT_CHARSET

Usage

From source file:com.grinnellplans.plandroid.LoginTask.java

protected String doInBackground(String... params) {
    Log.i("LoginTask::doInBackground", "entered");
    AndroidHttpClient plansClient = AndroidHttpClient.newInstance("plandroid");
    final HttpPost req = new HttpPost("http://" + _ss.get_serverName() + "/api/1/index.php?task=login");
    List<NameValuePair> postParams = new ArrayList<NameValuePair>(2);
    postParams.add(new BasicNameValuePair("username", params[0]));
    postParams.add(new BasicNameValuePair("password", params[1]));
    Log.i("LoginTask::doInBackground", "setting postParams");
    String resp = null;//  w  w w  . jav  a2  s .  c o m
    try {
        req.setEntity(new UrlEncodedFormEntity(postParams));
        HttpResponse response = null;
        Log.i("LoginTask::doInBackground", "executing request");
        response = plansClient.execute(req, _httpContext);
        Log.i("LoginTask::doInBackground", "reading response");
        resp = new BufferedReader((new InputStreamReader(response.getEntity().getContent()))).readLine();
    } catch (Exception e) {
        resp = constructResponse(e);
    }
    plansClient.close();
    Log.i("LoginTask::doInBackground", "server responded \"" + resp + "\"");
    Log.i("LoginTask::doInBackground", "exit");
    return resp;
}

From source file:net.giovannicapuano.galax.util.Utils.java

/**
 * Perform a HTTP POST request.//  w w w  .j a  v  a  2 s . com
 */
public static HttpData postData(String path, List<NameValuePair> post, Context context) {
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
    HttpConnectionParams.setSoTimeout(httpParameters, 10000);

    int status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
    String body = "";

    HttpClient httpClient = new DefaultHttpClient(httpParameters);
    HttpContext localContext = new BasicHttpContext();

    localContext.setAttribute(ClientContext.COOKIE_STORE, new PersistentCookieStore(context));
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());

    try {
        HttpPost httpPost = new HttpPost(context.getString(R.string.server) + path);
        httpPost.setEntity(new UrlEncodedFormEntity(post));
        HttpResponse response = httpClient.execute(httpPost, localContext);

        status = response.getStatusLine().getStatusCode();
        body = EntityUtils.toString(response.getEntity());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return new HttpData(status, body);
}

From source file:util.servlet.PaypalListenerServlet.java

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(Constants.IPN_SANDBOX_ENDPOINT);
    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("cmd", "_notify-validate")); //You need to add this parameter to tell PayPal to verify
    for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();) {
        String name = e.nextElement();
        String value = request.getParameter(name);
        params.add(new BasicNameValuePair(name, value));
    }//from w  ww.j a v a2 s .c o m
    post.setEntity(new UrlEncodedFormEntity(params));
    String rc = getRC(client.execute(post)).trim();
    if ("VERIFIED".equals(rc)) {
        //Your business code comes here
    }
}

From source file:it.av.fac.driver.APIClient.java

public String queryRequest(String userToken, String resource) {
    try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
        HttpPost httpPost = new HttpPost(endpoint + QUERY_PATH);

        List<NameValuePair> nvps = new ArrayList<>();
        nvps.add(new BasicNameValuePair("request", URLEncoder.encode(resource, "UTF-8")));
        nvps.add(new BasicNameValuePair("token", URLEncoder.encode(userToken, "UTF-8")));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));

        // Create a custom response handler
        ResponseHandler<String> responseHandler = (final HttpResponse response) -> {
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                HttpEntity entity = response.getEntity();
                return entity != null ? EntityUtils.toString(entity) : null;
            } else {
                throw new ClientProtocolException("Unexpected response status: " + status);
            }/*from  ww w  .j av  a2 s .  c  o m*/
        };

        return httpclient.execute(httpPost, responseHandler);
    } catch (IOException ex) {
        Logger.getLogger(APIClient.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

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

public static HttpResponse start(TrustedHttpClient client, String mediapackage, String workflowDefinition,
        String properties) throws Exception {
    HttpPost post = new HttpPost(getServiceUrl() + "start");
    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
    params.add(new BasicNameValuePair("mediapackage", mediapackage));
    params.add(new BasicNameValuePair("definition", workflowDefinition));
    params.add(new BasicNameValuePair("properties", properties));
    post.setEntity(new UrlEncodedFormEntity(params));
    return client.execute(post);
}

From source file:org.androidnerds.reader.util.api.Authentication.java

/**
 * This method returns back to the caller a proper authentication token to use with
 * the other API calls to Google Reader.
 *
 * @param user - the Google username// w ww .  j  av a  2  s. c  o m
 * @param pass - the Google password
 * @return sid - the returned authentication token for use with the API.
 *
 */
public static String getAuthToken(String user, String pass) {
    NameValuePair username = new BasicNameValuePair("Email", user);
    NameValuePair password = new BasicNameValuePair("Passwd", pass);
    NameValuePair service = new BasicNameValuePair("service", "reader");
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(username);
    pairs.add(password);
    pairs.add(service);

    try {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(AUTH_URL);
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs);

        post.setEntity(entity);

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

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

        InputStream in = respEntity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line = null;
        String result = null;

        while ((line = reader.readLine()) != null) {
            if (line.startsWith("SID")) {
                result = line.substring(line.indexOf("=") + 1);
            }
        }

        reader.close();
        client.getConnectionManager().shutdown();

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

From source file:Tools.HttpClientUtil.java

public static String postRequest(List<NameValuePair> paramsList) {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = null;//from  w ww  .  j  a v a 2 s .c  o  m
    String requestedServlet = ServletRequest.getServletRequest();
    StringBuilder sb = new StringBuilder();
    if (requestedServlet.equals(Constant.LOGIN_SERVLET)) {

        try {
            post = new HttpPost(Constant.SERVER_URL + Constant.LOGIN_SERVLET);
            post.setEntity(new UrlEncodedFormEntity(paramsList));
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
    } else if (requestedServlet.equals(Constant.BILLING_SERVLET)) {
        post = new HttpPost(Constant.SERVER_URL + Constant.BILLING_SERVLET);
        try {
            post.setEntity(new UrlEncodedFormEntity(paramsList));
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(HttpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else if (requestedServlet.equals(Constant.FOOD_LIST_SERVLET)) {
        post = new HttpPost(Constant.SERVER_URL + Constant.FOOD_LIST_SERVLET);
        try {
            post.setEntity(new UrlEncodedFormEntity(paramsList));
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(HttpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        post = new HttpPost(Constant.SERVER_URL + Constant.EMPLOYEE_SERVLET);
        try {
            post.setEntity(new UrlEncodedFormEntity(paramsList));
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(HttpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    try {

        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        String line = "";
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
            sb.append(line);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    //}
    return sb.toString();
}

From source file:at.ac.tuwien.swa13.swazam.NetworkConnection.java

public void makeRequest(String taskId, String username, ISong song) {
    System.out.println("Telling server about " + taskId + ": Found " + song.toString());

    try {/* ww  w  .  j ava 2  s .  c o m*/
        HttpClient client = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost("http://" + server + ":8080/swazam-server/result");
        postRequest.addHeader("Content-Type", "application/x-www-form-urlencoded");
        postRequest.addHeader("Cache-Control", "no-cache");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("id", taskId));
        nvps.add(new BasicNameValuePair("user", username));
        nvps.add(new BasicNameValuePair("result", buildMetadataBody(song)));
        UrlEncodedFormEntity songEntity = new UrlEncodedFormEntity(nvps);
        postRequest.setEntity(songEntity);
        HttpResponse response = client.execute(postRequest);

        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = "";
        while ((line = rd.readLine()) != null) {
            Logger.getLogger(NetworkConnection.class.getName()).log(Level.INFO, line);
        }
    } catch (IOException ex) {
        Logger.getLogger(NetworkConnection.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.hyphenated.pokerplayerclient.network.RestObjectRequestBuilder.java

@Override
public JSONObject sendRequest() throws Exception {
    HttpClient httpClient = new DefaultHttpClient(httpParams);
    httpPost.setEntity(new UrlEncodedFormEntity(params));
    HttpResponse httpResponse = httpClient.execute(httpPost);
    String response = EntityUtils.toString(httpResponse.getEntity());
    return new JSONObject(response);
}

From source file:org.eclipse.mylyn.internal.hudson.core.client.HudsonLoginForm.java

public UrlEncodedFormEntity createEntity() throws UnsupportedEncodingException {
    // set form content
    List<NameValuePair> requestParameters = new ArrayList<NameValuePair>();
    requestParameters.add(new BasicNameValuePair("j_username", j_username)); //$NON-NLS-1$
    requestParameters.add(new BasicNameValuePair("j_password", j_password)); //$NON-NLS-1$
    requestParameters.add(new BasicNameValuePair("from", from)); //$NON-NLS-1$

    // set json encoded content
    requestParameters.add(new BasicNameValuePair("json", new Gson().toJson(this))); //$NON-NLS-1$

    // set form parameters
    requestParameters.add(new BasicNameValuePair("Submit", "log in")); //$NON-NLS-1$ //$NON-NLS-2$

    // create entity
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(requestParameters);
    return entity;
}