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, final Charset charset) 

Source Link

Document

Constructs a new UrlEncodedFormEntity with the list of parameters in the specified encoding.

Usage

From source file:com.github.util.HttpHandler.java

/**
 * Convert a Map to UrlEncodedFormEntity
 *
 * @param parameters A Map of key:value//w w w.j a  va2  s . co m
 * @return UrlEncodedFormEntity using UTF-8 encoding
 * @throws java.io.UnsupportedEncodingException
 *          if UTF-8 encoding is not supported
 */
private static UrlEncodedFormEntity mapToEntity(Map<String, String> parameters)
        throws UnsupportedEncodingException {
    if (parameters == null) {
        throw new IllegalArgumentException("Invalid parameters unable map to Entity");
    }

    if (parameters.isEmpty()) {
        return null;
    }

    ArrayList<NameValuePair> parametersList = new ArrayList<NameValuePair>();

    for (@SuppressWarnings("rawtypes")
    Map.Entry element : parameters.entrySet()) {
        NameValuePair nameValuePair = new BasicNameValuePair((String) element.getKey(),
                (String) element.getValue());
        parametersList.add(nameValuePair);
    }

    return new UrlEncodedFormEntity(parametersList, HTTP.UTF_8);
}

From source file:com.prestomation.android.sospy.monitor.AppEngineClient.java

public HttpResponse makeRequest(String httpMethod, String urlPath, List<NameValuePair> params)
        throws Exception {
    HttpUriRequest request;/*from w  ww  .j  a  v a  2  s.co m*/
    URI uri = new URI(BASE_URL + urlPath);
    Log.w(TAG, uri.toString());

    if (httpMethod == "POST") {
        request = new HttpPost(uri);
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
        ((HttpPost) request).setEntity(entity);
    } else if (httpMethod == "DELETE") {
        request = new HttpDelete(uri);
    } else {
        //This should never happen
        return null;
    }

    HttpResponse res = makeRequestNoRetry(request, params);
    return res;
}

From source file:edu.scripps.fl.pubchem.web.session.PCDepositionSystemSession.java

/**
 * /*  w  ww  .ja  va  2s.  c o  m*/
 * Logs into your PubChem Deposition System account 
 * 
 * @param username
 * @param password
 * @throws Exception
 */
public void login(String username, String password) throws Exception {
    Document doc = getDocument("http://" + SITE + "/deposit/deposit.cgi");

    Node formNode = doc.selectSingleNode("//form[@name='deplogform']");
    FormControlVisitorSupport fcvs = new FormControlVisitorSupport();
    formNode.accept(fcvs);

    Set<String> set = new HashSet<String>(parameters);
    set.add("login");
    set.add("password");
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    for (Map.Entry<String, String> entry : fcvs.getFormParameters().entrySet()) {
        if (!set.contains(entry.getKey()))
            params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }
    addParameters(params, "wcev:src", "logmgr", "wcev:name", "login", "wcev:data", "true", "wc:scrollx", 0,
            "wc:scrolly", 0, "login", username, "password", password);

    String page = postPage("http://" + SITE + "/deposit/deposit.cgi",
            new UrlEncodedFormEntity(params, HTTP.UTF_8));
    if (page.contains("Error while Performing an Action"))
        throw new Exception("PubChem Login Failure: Error while Performing an Action");
}

From source file:com.mmj.app.common.util.PushSMSUtils.java

private void sendPushMsg(String msg, String mobiles) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(PUSH_SERVER_URL);
    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
    params.add(new BasicNameValuePair("apikey", API_KEY));
    params.add(new BasicNameValuePair("text", msg));
    params.add(new BasicNameValuePair("mobile", mobiles));

    try {//www .j  av a2s  .  c  o  m
        httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
        HttpResponse response = client.execute(httpPost);

        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == 200) {
            logger.debug("?{}?{}???", mobiles, msg);
        } else {
            logger.debug("?{}?{}??", mobiles, msg);
        }
    } catch (Exception e) {
        logger.debug("http post error!{}", e.getMessage());
    } finally {
        httpPost.releaseConnection();
    }
}

From source file:securitytools.veracode.http.request.BeginPreScanRequest.java

public BeginPreScanRequest(String applicationId, boolean autoScan, String sandboxId) {
    super("/api/4.0/beginprescan.do");

    if (ValidationUtils.isNullOrEmpty(applicationId)) {
        throw new IllegalArgumentException("\"applicationId\" argument cannot be null or empty.");
    }//from ww w. j a va 2 s . co m

    List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    formparams.add(new BasicNameValuePair("app_id", applicationId));

    if (autoScan) {
        formparams.add(new BasicNameValuePair("auto_scan", "true"));
    }

    if (sandboxId != null) {
        formparams.add(new BasicNameValuePair("sandbox_id", sandboxId));
    }

    setEntity(new UrlEncodedFormEntity(formparams, Consts.UTF_8));
}

From source file:org.opencastproject.remotetest.server.WorkflowAuthorizationTest.java

@Test
public void testStartAndRetrieveWorkflowInstance() throws Exception {

    // Create a series with attached ACLs
    HttpPost postSeries = new HttpPost(BASE_URL + "/series/");
    List<NameValuePair> seriesParams = new ArrayList<NameValuePair>();
    seriesParams.add(new BasicNameValuePair("series", getSampleSeries()));
    seriesParams.add(new BasicNameValuePair("acl", getSampleAcl()));
    postSeries.setEntity(new UrlEncodedFormEntity(seriesParams, "UTF-8"));
    HttpResponse seriesResponse = client.execute(postSeries);
    Assert.assertEquals(200, seriesResponse.getStatusLine().getStatusCode());
    EntityUtils.toString(seriesResponse.getEntity());

    // Start a workflow instance associated with this series
    HttpPost postWorkflow = new HttpPost(BASE_URL + "/workflow/start");
    List<NameValuePair> workflowParams = new ArrayList<NameValuePair>();
    workflowParams.add(new BasicNameValuePair("definition", getSampleWorkflowDefinition()));
    workflowParams.add(new BasicNameValuePair("mediapackage", getSampleMediaPackage()));
    postWorkflow.setEntity(new UrlEncodedFormEntity(workflowParams, "UTF-8"));
    HttpResponse workflowResponse = client.execute(postWorkflow);
    Assert.assertEquals(200, workflowResponse.getStatusLine().getStatusCode());

    // Grab the new workflow instance from the response
    String postResponse = EntityUtils.toString(workflowResponse.getEntity());

    // Ensure that the mediapackage has a XACML attachment reflecting the series ACLs
    String xacmlAttachmentId = getXACMLAttachmentId(postResponse);
    Assert.assertNotNull(xacmlAttachmentId);
}

From source file:com.alta189.deskbin.services.paste.PastebinService.java

@Override
public String paste(String content, boolean isPrivate) throws PasteException {
    HttpPost post = new HttpPost(URL);

    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("api_dev_key", apiKey));
    nvps.add(new BasicNameValuePair("api_option", "paste"));
    nvps.add(new BasicNameValuePair("api_paste_private", isPrivate ? "2" : "0"));
    nvps.add(new BasicNameValuePair("api_paste_code", content));
    try {//from  w w  w.  ja v a  2  s.  c o m
        post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new PasteException(e);
    }
    try {
        HttpResponse response = client.execute(post);
        return EntityUtils.toString(response.getEntity());
    } catch (Exception e) {
        throw new PasteException(e);
    }
}

From source file:ch.ralscha.extdirectspring_itest.UserServiceTest.java

@Test
public void testPostWithErrors() throws IOException {
    Locale.setDefault(Locale.ENGLISH);
    List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    formparams.add(new BasicNameValuePair("extTID", "2"));
    formparams.add(new BasicNameValuePair("extAction", "userService"));
    formparams.add(new BasicNameValuePair("extMethod", "updateUser"));
    formparams.add(new BasicNameValuePair("extType", "rpc"));
    formparams.add(new BasicNameValuePair("extUpload", "false"));
    formparams.add(new BasicNameValuePair("name", "Joe"));
    formparams.add(new BasicNameValuePair("age", "30"));
    UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(formparams, "UTF-8");

    this.post.setEntity(postEntity);

    CloseableHttpResponse response = this.client.execute(this.post);
    try {/*from   w  w  w.  ja  v a2  s.c o  m*/
        HttpEntity entity = response.getEntity();
        assertThat(entity).isNotNull();
        String responseString = EntityUtils.toString(entity);

        Map<String, Object> rootAsMap = this.mapper.readValue(responseString, Map.class);
        assertThat(rootAsMap).hasSize(5);
        assertThat(rootAsMap.get("method")).isEqualTo("updateUser");
        assertThat(rootAsMap.get("type")).isEqualTo("rpc");
        assertThat(rootAsMap.get("action")).isEqualTo("userService");
        assertThat(rootAsMap.get("tid")).isEqualTo(2);

        Map<String, Object> result = (Map<String, Object>) rootAsMap.get("result");
        assertThat(result).hasSize(4);
        assertThat(result.get("name")).isEqualTo("Joe");
        assertThat(result.get("age")).isEqualTo(30);
        assertThat(result.get("success")).isEqualTo(Boolean.FALSE);

        Map<String, Object> errors = (Map<String, Object>) result.get("errors");
        assertThat(errors).hasSize(1);
        assertThat((List<String>) errors.get("email")).containsOnly("may not be empty");
    } finally {
        IOUtils.closeQuietly(response);
    }
}