List of usage examples for org.apache.http.client.entity UrlEncodedFormEntity UrlEncodedFormEntity
public UrlEncodedFormEntity(final Iterable<? extends NameValuePair> parameters, final Charset charset)
From source file:org.apache.sling.testing.clients.util.FormEntityBuilder.java
public UrlEncodedFormEntity build() { try {/*from www . ja va 2 s . com*/ return new UrlEncodedFormEntity(params, encoding); } catch (UnsupportedEncodingException ue) { throw new Error("Unexpected UnsupportedEncodingException", ue); } }
From source file:org.apache.streams.monitoring.persist.impl.BroadcastMessagePersister.java
@Override /*//from w ww . ja va 2 s.com * Given a list of messages as Strings, broadcast them to the broadcastUri * (if one is defined) * @param messages * @return int status code from POST response */ public int persistMessages(List<String> messages) { int responseCode = -1; if (broadcastUri != null) { try { HttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost(broadcastUri); post.setHeader("User-Agent", "Streams"); List<NameValuePair> urlParameters = new ArrayList<>(); urlParameters.add(new BasicNameValuePair("messages", serializeMessages(messages))); post.setEntity(new UrlEncodedFormEntity(urlParameters, "UTF-8")); HttpResponse response = client.execute(post); responseCode = response.getStatusLine().getStatusCode(); LOGGER.debug("Broadcast {} messages to URI: {}", messages.size(), broadcastUri); } catch (Exception ex) { LOGGER.error("Failed to broadcast message to URI: {}, exception: {}", broadcastUri, ex); } } return responseCode; }
From source file:org.dataconservancy.ui.it.support.LoginRequest.java
public HttpPost asHttpPost() { HttpPost loginFormPost = new HttpPost(urlConfig.getLoginPostUrl().toString()); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair(form.getPasswordFieldName(), pass)); params.add(new BasicNameValuePair(form.getUsernameFieldName(), user)); UrlEncodedFormEntity entity = null;/* ww w . j av a 2 s .c o m*/ try { entity = new UrlEncodedFormEntity(params, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } loginFormPost.setEntity(entity); return loginFormPost; }
From source file:org.addhen.smssync.net.SmsSyncHttpClient.java
/** * Upload SMS to a web service via HTTP POST * /*from w ww.ja v a 2 s.co m*/ * @param address * @throws MalformedURLException * @throws IOException * @return */ public static boolean postSmsToWebService(String url, HashMap<String, String> params, Context context) { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); try { // Add your data List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); nameValuePairs.add(new BasicNameValuePair("secret", params.get("secret"))); nameValuePairs.add(new BasicNameValuePair("from", params.get("from"))); nameValuePairs.add(new BasicNameValuePair("message", params.get("message"))); nameValuePairs.add(new BasicNameValuePair("sent_timestamp", params.get("sent_timestamp"))); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); if (response.getStatusLine().getStatusCode() == 200) { String resp = getText(response); boolean success = Util.extractPayloadJSON(resp); if (success) { // auto response message is enabled to be received from the // server. if (Prefrences.enableReplyFrmServer) { Util.sendResponseFromServer(context, resp); } return true; } else { return false; } } else { return false; } } catch (ClientProtocolException e) { return false; } catch (IOException e) { return false; } }
From source file:ch.ralscha.extdirectspring_itest.ExceptionFormPostControlerTest.java
@Test public void testPost() throws IOException { List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("extTID", "3")); formparams.add(new BasicNameValuePair("extAction", "exceptionFormPostController")); formparams.add(new BasicNameValuePair("extMethod", "throwAException")); formparams.add(new BasicNameValuePair("extType", "rpc")); formparams.add(new BasicNameValuePair("extUpload", "false")); UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); this.post.setEntity(postEntity); CloseableHttpResponse response = this.client.execute(this.post); HttpEntity entity = response.getEntity(); assertThat(entity).isNotNull();// w ww . j av a 2 s . c o m String responseString = EntityUtils.toString(entity); ObjectMapper mapper = new ObjectMapper(); Map<String, Object> rootAsMap = mapper.readValue(responseString, Map.class); assertThat(rootAsMap).hasSize(6); assertThat(rootAsMap.get("method")).isEqualTo("throwAException"); assertThat(rootAsMap.get("type")).isEqualTo("exception"); assertThat(rootAsMap.get("action")).isEqualTo("exceptionFormPostController"); assertThat(rootAsMap.get("tid")).isEqualTo(3); assertThat(rootAsMap.get("message")).isEqualTo("a null pointer"); assertThat(rootAsMap.get("where")).isNull(); @SuppressWarnings("unchecked") Map<String, Object> result = (Map<String, Object>) rootAsMap.get("result"); assertThat(result).hasSize(1); assertThat((Boolean) result.get("success")).isFalse(); IOUtils.closeQuietly(response); }
From source file:com.vmware.bdd.cli.http.TestSecureHttpClient.java
public void test1() throws IOException { RunWayConfig.autoRun();/*from w w w .j a v a 2s .c o m*/ LOGGER.info("start"); String url = "https://10.117.8.239:443/" + Constants.REST_PATH_LOGIN; HttpPost loginPost = new HttpPost(url); NameValuePair[] loginCredentials = new NameValuePair[] { new BasicNameValuePair("j_username", "userName"), new BasicNameValuePair("j_password", "password") }; //handling non-ascii username and password. Encoding by Apache HTTP Client. HttpEntity requestEntity = new UrlEncodedFormEntity(Arrays.asList(loginCredentials), Charset.forName("UTF-8")); loginPost.setEntity(requestEntity); HttpResponse loginResponse = secureHttpClient.execute(loginPost); LOGGER.debug(loginResponse.getStatusLine()); }
From source file:org.smartloli.kafka.eagle.common.util.HttpClientUtils.java
/** * Send request by post method.// www .j ava 2 s. c o m * * @param uri: * http://ip:port/demo * @param parames: * new BasicNameValuePair("code", "200") * * new BasicNameValuePair("name", "smartloli") */ public static String doPostForm(String uri, List<BasicNameValuePair> parames) { String result = ""; try { CloseableHttpClient client = null; CloseableHttpResponse response = null; try { HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(new UrlEncodedFormEntity(parames, "UTF-8")); client = HttpClients.createDefault(); response = client.execute(httpPost); HttpEntity entity = response.getEntity(); result = EntityUtils.toString(entity); } finally { if (response != null) { response.close(); } if (client != null) { client.close(); } } } catch (Exception e) { e.printStackTrace(); LOG.error("Do post form request has error, msg is " + e.getMessage()); } return result; }
From source file:pingdesktop.Sample.HTTPhandlerSample.java
private void doPost() throws IOException { /* create the HTTP client and POST request */ HttpPost httpPost = new HttpPost("http://xrozz.com/mlogg/api/user/login"); /* add some request parameters for a form request */ List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("username", "pangeranweb")); nvps.add(new BasicNameValuePair("password", "anisnuzulan")); nvps.add(new BasicNameValuePair("Content-Type", "application/x-www-form-urlencoded")); httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); /* execute request */ HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); /* process response */ if (httpResponse.getStatusLine().getStatusCode() == 200) { String responseText = EntityUtils.toString(httpEntity); System.out.println(responseText); } else {//from ww w . j av a 2 s . c om System.err.println("Invalid HTTP response: " + httpResponse.getStatusLine().getStatusCode()); } }
From source file:ch.ralscha.extdirectspring_itest.ExceptionFormPostServiceTest.java
@Test public void testExceptionHandling() throws IOException { List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("extTID", "3")); formparams.add(new BasicNameValuePair("extAction", "exceptionFormPostService")); formparams.add(new BasicNameValuePair("extMethod", "throwAException")); formparams.add(new BasicNameValuePair("extType", "rpc")); formparams.add(new BasicNameValuePair("extUpload", "false")); UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); this.post.setEntity(postEntity); CloseableHttpResponse response = this.client.execute(this.post); HttpEntity entity = response.getEntity(); assertThat(entity).isNotNull();/*from w ww.j a v a 2 s. c o m*/ String responseString = EntityUtils.toString(entity); ObjectMapper mapper = new ObjectMapper(); Map<String, Object> rootAsMap = mapper.readValue(responseString, Map.class); assertThat(rootAsMap).hasSize(6); assertThat(rootAsMap.get("method")).isEqualTo("throwAException"); assertThat(rootAsMap.get("type")).isEqualTo("exception"); assertThat(rootAsMap.get("action")).isEqualTo("exceptionFormPostService"); assertThat(rootAsMap.get("tid")).isEqualTo(3); assertThat(rootAsMap.get("message")).isEqualTo("a null pointer"); @SuppressWarnings("unchecked") Map<String, Object> result = (Map<String, Object>) rootAsMap.get("result"); assertThat(result).hasSize(1); assertThat((Boolean) result.get("success")).isFalse(); IOUtils.closeQuietly(response); }