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:org.sonar.plugins.buildstability.ci.jenkins.JenkinsUtils.java

public static void doLogin(HttpClient client, String hostName, String username, String password)
        throws IOException {
    String hudsonLoginEntryUrl = hostName + "/login";
    HttpGet loginLink = new HttpGet(hudsonLoginEntryUrl);
    HttpResponse response = client.execute(loginLink);
    checkResult(response.getStatusLine().getStatusCode(), hudsonLoginEntryUrl);
    EntityUtils.consume(response.getEntity());

    String location = hostName + "/j_acegi_security_check";
    boolean loggedIn = false;
    while (!loggedIn) {
        HttpPost loginMethod = new HttpPost(location);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("j_username", username));
        nvps.add(new BasicNameValuePair("j_password", password));
        nvps.add(new BasicNameValuePair("action", "login"));
        loginMethod.setEntity(new UrlEncodedFormEntity(nvps));
        try {/*from  w w w.  j av a2  s  .  c o  m*/
            HttpResponse response2 = client.execute(loginMethod);
            if (response2.getStatusLine().getStatusCode() / 100 == 3) {
                // Commons HTTP client refuses to handle redirects for POST
                // so we have to do it manually.
                location = response2.getFirstHeader("Location").getValue();
            } else {
                checkResult(response2.getStatusLine().getStatusCode(), location);
                loggedIn = true;
            }
            EntityUtils.consume(response2.getEntity());
        } finally {
            loginMethod.releaseConnection();
        }
    }
}

From source file:org.megam.api.http.TransportMachinery.java

public static TransportResponse post(TransportTools nuts) throws ClientProtocolException, IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(nuts.urlString());
    System.out.println("NUTS" + nuts.toString());
    if (nuts.headers() != null) {
        for (Map.Entry<String, String> headerEntry : nuts.headers().entrySet()) {
            httppost.addHeader(headerEntry.getKey(), headerEntry.getValue());
        }/*from w w  w  .  ja v  a 2s. c  om*/
    }
    if (nuts.fileEntity() != null) {
        httppost.setEntity(nuts.fileEntity());
    }
    if (nuts.pairs() != null && (nuts.contentType() == null)) {
        httppost.setEntity(new UrlEncodedFormEntity(nuts.pairs()));
    }

    if (nuts.contentType() != null) {
        httppost.setEntity(new StringEntity(nuts.contentString(), nuts.contentType()));
    }
    TransportResponse transportResp = null;
    System.out.println(httppost.toString());
    try {
        HttpResponse httpResp = httpclient.execute(httppost);
        transportResp = new TransportResponse(httpResp.getStatusLine(), httpResp.getEntity(),
                httpResp.getLocale());
    } finally {
        httppost.releaseConnection();
    }
    return transportResp;

}

From source file:cardgametrackercs319.DBConnectionManager.java

public static String registerUserInfo(String id, String name, String pwd)
        throws UnsupportedEncodingException, IOException {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://ozymaxx.net/cs319/register_user.php");

    post.setHeader("User Agent", USER_AGENT);
    ArrayList<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("id", id));
    urlParameters.add(new BasicNameValuePair("pname", name));
    urlParameters.add(new BasicNameValuePair("pwd", pwd));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    StringBuffer result = new StringBuffer();
    String res = "";

    while ((res = reader.readLine()) != null) {
        result.append(res);//from w  ww .j a  va2s .com
    }

    return result.toString();
}

From source file:Main.java

public static void sendData(final String targetUrl, final String idField, final String tempField,
        final String feelingField, final String symptomsField, final String id, final String temp,
        final String feeling, final ArrayList<String> symptoms) {
    new Thread(new Runnable() {
        public void run() {
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(targetUrl);

            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair(idField, id));
                nameValuePairs.add(new BasicNameValuePair(tempField, temp));
                nameValuePairs.add(new BasicNameValuePair(feelingField, feeling));
                for (int i = 0; i < symptoms.size(); i++) {
                    nameValuePairs.add(new BasicNameValuePair(symptomsField, symptoms.get(i)));
                }//from  w  ww .  j  ava2s  . c  om
                // Must put this extra pageHistory field here, otherwise Forms will reject the
                // symptoms.
                nameValuePairs.add(new BasicNameValuePair("pageHistory", "0,1"));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

From source file:br.com.estudogrupo.online.DicionarioOnline04.java

@Override
public void run() {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://www.md5online.org/");
    try {/*from ww  w.  j  av a  2s .c  o m*/
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("md5", getRecebe()));
        nameValuePairs.add(new BasicNameValuePair("search", "0"));
        nameValuePairs.add(new BasicNameValuePair("action", "decrypt"));
        nameValuePairs.add(new BasicNameValuePair("a", "82355607"));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = "";
        while ((line = rd.readLine().trim()) != null) {
            if (line.startsWith("<span class=\"result\"")) {
                String key = line.substring(81, 100);
                System.out.println("Senha  : " + key.trim().replace("</b>", "").replace("</s>", "")
                        .replace("</span><br", "").replace(" /", ""));
            }

        }
    } catch (IOException e) {
        e.printStackTrace();

    } catch (NullPointerException e) {

    }

}

From source file:att.jaxrs.client.Tag.java

public static String addTag(Tag tag) {
    final long tag_id = getExistingRecord(tag.getTag_name());
    if (-1L != tag_id) {
        return "{response:{Tag: 'EXISTING_RECORD'},tag_id:" + tag_id + "}";
    }/*from  w  w  w.  java 2s .  c om*/
    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("tag_name", tag.getTag_name()));

    HttpResponse result;
    String resultStr = "";

    try {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(Constants.INSERT_TAG_RESOURCE);
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        result = httpClient.execute(post);
        System.out.println(Constants.RESPONSE_STATUS_CODE + result);
        resultStr = Util.getStringFromInputStream(result.getEntity().getContent());
        System.out.println(Constants.RESPONSE_BODY + resultStr);

    } catch (Exception e) {
        System.out.println(e.getMessage());

    }
    return resultStr;
}

From source file:att.jaxrs.client.Content_tagCollection.java

public static Content_tagCollection getContentTagsWithID(long content_id) {
    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("content_id", Long.toString(content_id)));

    Content_tagCollection collectionCT = new Content_tagCollection();

    try {/* w  ww .ja  v  a  2  s  .  c om*/
        System.out.println("invoking getTagsWithID: " + content_id);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(Constants.SELECT_WITH_KEY_CONTENT_TAG_RESOURCE);
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        HttpResponse result = httpClient.execute(post);
        System.out.println(Constants.RESPONSE_STATUS_CODE + result);
        String resultStr = Util.getStringFromInputStream(result);

        collectionCT = Marshal.unmarshal(Content_tagCollection.class, resultStr);

    } catch (Exception e) {
        e.printStackTrace();

    }

    return collectionCT;
}

From source file:eu.ebbitsproject.peoplemanager.utils.HttpUtils.java

public static void postData(String url, String action, String errorType, String objectId, String personId,
        String eventId) {/*from  www  .  j  a  va  2 s  . c om*/
    CloseableHttpClient httpClient = HttpClients.createDefault();

    HttpPost httpPost = new HttpPost(url);
    List<NameValuePair> nvps = new ArrayList<>();
    nvps.add(new BasicNameValuePair("action", action));
    nvps.add(new BasicNameValuePair("personId", personId));
    nvps.add(new BasicNameValuePair("errorType", errorType));
    nvps.add(new BasicNameValuePair("objectId", objectId));
    nvps.add(new BasicNameValuePair("eventId", eventId));

    System.out.println("action = " + action);
    System.out.println("personId = " + personId);
    System.out.println("errorType = " + errorType);
    System.out.println("objectId = " + objectId);
    System.out.println("eventId = " + eventId);

    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(HttpUtils.class.getName()).log(Level.SEVERE, null, ex);
    }

    CloseableHttpResponse response = null;
    try {
        response = httpClient.execute(httpPost);
        System.out.println(response.getStatusLine());
        HttpEntity entity = response.getEntity();
        EntityUtils.consume(entity);
    } catch (IOException e) {
        Logger.getLogger(HttpUtils.class.getName()).log(Level.SEVERE, null, e);
    } finally {
        try {
            response.close();
        } catch (IOException e) {
            Logger.getLogger(HttpUtils.class.getName()).log(Level.SEVERE, null, e);
        }
    }
}

From source file:de.avanux.android.livetracker2.HttpUtil.java

public static String post(String url, Map<String, String> httpParameters)
        throws ClientProtocolException, IOException {
    Log.d(TAG, "HTTP POST " + url);
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(httpParameters.size());
    Set<String> httpParameterKeys = httpParameters.keySet();
    for (String httpParameterKey : httpParameterKeys) {
        nameValuePairs.add(new BasicNameValuePair(httpParameterKey, httpParameters.get(httpParameterKey)));
    }// www .  ja  va  2  s  . c o  m

    HttpPost method = new HttpPost(url);
    method.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = executeMethod(method);
    return getResponseAsString(response);
}

From source file:com.nibss.util.Request.java

public void post(String url, List<NameValuePair> list) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {//from  ww w  . j a  v a 2 s  .  c om
        HttpPost httpPost = new HttpPost(url);

        httpPost.setEntity(new UrlEncodedFormEntity(list));
        CloseableHttpResponse response2 = httpclient.execute(httpPost);
        try {
            System.out.println(response2.getStatusLine().getStatusCode());
            HttpEntity entity2 = response2.getEntity();

            EntityUtils.consume(entity2);
        } finally {
            response2.close();
        }
    } finally {
        httpclient.close();
    }
}