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.oracle.jes.samples.hellostorage.httpcPoster1.java

public String putMessage(int i, String vel, String ser) {

    String out = null;/*from  ww w.j a  v  a 2  s .  c o  m*/

    HttpClient httpclient = HttpClientBuilder.create().build();

    HttpPost httppostreq = new HttpPost("http://192.168.1.5:8080/async-request-war/AjaxCometServlet");
    //HttpPost httppostreq = new HttpPost("http://192.168.1.5:8080/pichrony/AjaxCometServlet");

    //HttpPost httppostreq = new HttpPost("http://192.168.1.5:8080/async-request-war/AjServlet");

    List<NameValuePair> pairs = new ArrayList<NameValuePair>();

    if (i == 1) {
        pairs.add(new BasicNameValuePair("action", "login"));
        pairs.add(new BasicNameValuePair("name", ser));
    } else {
        FindMac fm = new FindMac();

        String m = fm.getAddr();

        pairs.add(new BasicNameValuePair("action", "post"));
        pairs.add(new BasicNameValuePair("name", ser));
        pairs.add(new BasicNameValuePair("message", vel));

    }

    try {
        httppostreq.setEntity(new UrlEncodedFormEntity(pairs));
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        //e1.printStackTrace();
    }

    /*
    StringEntity se=null;
    try {
       se = new StringEntity(jsono.toString());
    } catch (UnsupportedEncodingException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }
            
            
            
    se.setContentType("application/json;charset=UTF-8");
    se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
    httppostreq.setEntity(se);
    */

    //HttpGet httpget = new HttpGet("http://192.168.1.5:8080/async-request-war/AjServlet");

    try {
        HttpResponse response = httpclient.execute(httppostreq);

        if (response != null) {
            out = "";
            InputStream inputstream = response.getEntity().getContent();
            out = convertStreamToString(inputstream);

        } else {

        }
    } catch (ClientProtocolException e) {

    } catch (IOException e) {
    } catch (Exception e) {
    }
    return out;
}

From source file:de.mojadev.ohmmarks.virtuohm.http.VirtuOhmParams.java

public static HttpEntity getSessionRegisterParams(String in_crimmo, String userid)
        throws UnsupportedEncodingException {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair(USER_ID, userid));
    params.add(new BasicNameValuePair(IN_CRIMMO, in_crimmo));
    return new UrlEncodedFormEntity(params);

}

From source file:co.com.soinsoftware.altablero.utils.HttpRequest.java

public Object sendPost(String method, String object) throws IOException {
    String completeUrl = URL + method;
    HttpPost httppost = new HttpPost(completeUrl);
    List<NameValuePair> urlParameters = new ArrayList<>();
    urlParameters.add(new BasicNameValuePair("object", object));
    httppost.setEntity(new UrlEncodedFormEntity(urlParameters));
    return this.getResponse(httppost);
}

From source file:com.mongolduu.android.ng.misc.HttpConnector.java

public static String httpPOST(String url, List<NameValuePair> nameValuePairs)
        throws ClientProtocolException, IOException {
    HttpClient httpClient = createHttpClient();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpClient.execute(httpPost);
    if (response.getStatusLine().getStatusCode() >= 400) {
        throw new IOException("HTTP client error: " + response.getStatusLine().getStatusCode());
    }/*www .java  2 s .c o  m*/
    return processEntity(response.getEntity());
}

From source file:net.yoomai.virgo.spider.Emulator.java

/**
 * //from w  ww.  j  a  va  2 s.c o m
 *
 * @param params
 * @param url
 */
public String login(Map<String, String> params, String url) {
    String cookie = "";

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);

    List<NameValuePair> nvps = generateURLParams(params);
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
    } catch (UnsupportedEncodingException e) {
        log.error(e.getMessage() + " : " + e.getCause());
    }

    CloseableHttpResponse response = null;

    try {
        response = httpClient.execute(httpPost);
    } catch (IOException e) {
        log.error(e.getMessage() + " : " + e.getCause());
    }

    if (response != null) {
        StatusLine statusLine = response.getStatusLine();
        log.info(statusLine);
        if (statusLine.getStatusCode() == 200 || statusLine.getStatusCode() == 302) {
            Header[] headers = response.getHeaders("Set-Cookie");
            for (Header header : headers) {
                cookie += header.getValue() + ";";
            }
        }
    }

    return cookie;
}

From source file:org.apache.clerezza.commons.rdf.impl.sparql.SparqlClient.java

public Object queryResult(final String query) throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(endpoint);
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("query", query));
    httpPost.setEntity(new UrlEncodedFormEntity(nvps));
    CloseableHttpResponse response2 = httpclient.execute(httpPost);
    HttpEntity entity2 = response2.getEntity();
    try {//from w  w  w.  j a  v  a  2s.  c  o m
        InputStream in = entity2.getContent();
        final String mediaType = entity2.getContentType().getValue();
        if ("application/sparql-results+xml".equals(mediaType)) {
            return SparqlResultParser.parse(in);
        } else {
            //assuming RDF response
            //FIXME clerezza-core-rdf to clerezza dependency
            Parser parser = Parser.getInstance();
            return parser.parse(in, mediaType);
        }
    } finally {
        EntityUtils.consume(entity2);
        response2.close();
    }

}

From source file:org.opencastproject.videosegmenter.remote.VideoSegmenterRemoteImpl.java

@Override
public Job segment(Track track) throws VideoSegmenterException {
    HttpPost post = new HttpPost();
    try {//from ww w  .  ja  v  a  2 s  .c  om
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("track", MediaPackageElementParser.getAsXml(track)));
        post.setEntity(new UrlEncodedFormEntity(params));
    } catch (Exception e) {
        throw new VideoSegmenterException(e);
    }
    HttpResponse response = null;
    try {
        response = getResponse(post);
        if (response != null) {
            try {
                Job receipt = JobParser.parseJob(response.getEntity().getContent());
                logger.info("Analyzing {} on a remote analysis server", track);
                return receipt;
            } catch (Exception e) {
                throw new VideoSegmenterException(
                        "Unable to analyze element '" + track + "' using a remote analysis service", e);
            }
        }
    } finally {
        closeConnection(response);
    }
    throw new VideoSegmenterException(
            "Unable to analyze element '" + track + "' using a remote analysis service");
}

From source file:org.utils.HttpDriver.java

/**
 *
 * @param mainvars// w  w  w .  jav a  2  s . c o m
 * @return 
 */
public String Start() {
    Log.d(LOG_TAG, "Destination Url," + Url);
    Log.d(LOG_TAG, "Post Send Json," + Json);
    String responseBody = null;
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(Url.toString());
    CookieHandler.setDefault(new CookieManager());
    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("Json", this.Json));
        // nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        httppost.setHeader("Host", "78.46.251.139");
        httppost.setHeader("User-Agent", USER_AGENT);
        httppost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httppost.setHeader("Accept-Language", "en-US,en;q=0.5");
        httppost.setHeader("Cookie", cookies + ";");
        httppost.setHeader("Connection", "keep-alive");
        httppost.setHeader("Referer", "https://accounts.google.com/ServiceLoginAuth");
        httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");

        Log.d(LOG_TAG, "Post Send Cookies," + cookies + ";");

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        int responseCode = response.getStatusLine().getStatusCode();
        Log.d(LOG_TAG, "Response code," + responseCode);
        switch (responseCode) {
        case 200:
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                responseBody = EntityUtils.toString(entity);
            }
            break;
        }

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

    return (responseBody);

}

From source file:com.ibm.watson.developer_cloud.professor_languo.it.endpoints.IT_AskAQuestion.java

/**
 * Tests if the ask a question endpoint can be accessed and checks if the return is in JSON
 * format.//ww w . j  ava2 s  .co m
 * 
 * @throws Exception
 */
@Test
public void askAQuestionTest() throws Exception {
    String postUrl = System.getProperty("app.url");
    postUrl = postUrl + "/api/ask_question";

    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost postRequest = new HttpPost(postUrl);
    postRequest.addHeader("accept", "application/json");
    postRequest.addHeader("Content-Type", "application/x-www-form-urlencoded");

    List<NameValuePair> formParams = new ArrayList<NameValuePair>();
    formParams.add(new BasicNameValuePair("questionText", "question text"));

    postRequest.setEntity(new UrlEncodedFormEntity(formParams));

    HttpResponse response = httpClient.execute(postRequest);

    String jsonString = EntityUtils.toString(response.getEntity());

    Assert.assertEquals("response was: " + jsonString, 200, response.getStatusLine().getStatusCode());

    try {
        new JSONArray(jsonString);
    } catch (JSONException e) {
        Assert.fail("Response is not in JSON format: " + jsonString + e.getMessage());
    }
}