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.piraso.client.net.HttpPirasoTestHandler.java
private void doExecute() throws IOException, SAXException, ParserConfigurationException { Validate.notNull(uri, "uri should not be null."); HttpPost post = new HttpPost(uri.getPath()); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair(SERVICE_PARAMETER, SERVICE_TEST_PARAMETER_VALUE)); post.setEntity(new UrlEncodedFormEntity(params, ENCODING_UTF_8)); HttpResponse response = client.execute(targetHost, post, context); StatusLine status = response.getStatusLine(); if (status.getStatusCode() != HttpStatus.SC_OK) { throw new HttpPirasoException(status.toString()); }//w w w . ja v a2 s . c o m responseEntity = response.getEntity(); String contentType = responseEntity.getContentType().getValue().toLowerCase(); if (!contentType.contains(JSON_CONTENT_TYPE)) { throw new HttpPirasoException("Invalid response content type: " + responseEntity.getContentType()); } this.status = mapper.readValue(responseEntity.getContent(), Status.class); }
From source file:fr.ippon.wip.http.request.PostRequestBuilder.java
public HttpRequestBase buildHttpRequest() throws URISyntaxException { URI uri = new URI(getRequestedURL()); HttpPost postRequest = new HttpPost(uri); if (parameterMap == null) return postRequest; List<NameValuePair> httpParams = new LinkedList<NameValuePair>(); for (Map.Entry<String, String> entry : parameterMap.entries()) httpParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); HttpEntity formEntity = new UrlEncodedFormEntity(httpParams, ContentType.APPLICATION_FORM_URLENCODED.getCharset()); postRequest.setEntity(formEntity);/* w ww .ja va2s .c o m*/ return postRequest; }
From source file:cc.alessandro.jpocket.session.RestUtility.java
private void addParameters(HttpPost post, Request request) throws IOException { List<NameValuePair> params = new ArrayList<NameValuePair>(); for (Type param : request.getParameters()) { params.add(new BasicNameValuePair(param.getName(), param.getValue())); }//from w w w . java 2s. co m post.setEntity(new UrlEncodedFormEntity(params, DEFAULT_CHARACTER_SET)); }
From source file:com.phoneToPc.Http.java
/** * Connects to the server,// ww w. j a va 2s .c o m * @return String request response (or null) */ //CommandE 0 URL public static String httpReq(CommandE command) { final HttpResponse resp; String ResString = null; final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); assert (command.GetPropertyNum() >= 2); String url = command.GetProperty(1).GetPropertyContext(); Log.d("HTTP", "httpReq : "); for (int i = 0; i < command.GetPropertyNum(); i++) { Log.d("HTTP", command.GetProperty(i).GetPropertyName() + " " + command.GetProperty(i).GetPropertyContext()); } for (int i = 2; i < command.GetPropertyNum(); i++) { params.add(new BasicNameValuePair(command.GetProperty(i).GetPropertyName(), command.GetProperty(i).GetPropertyContext())); } final HttpEntity entity; try { entity = new UrlEncodedFormEntity(params, HTTP.UTF_8); } catch (final UnsupportedEncodingException e) { // this should never happen. throw new IllegalStateException(e); } Log.i(TAG, "connect url = " + url); final HttpPost post = new HttpPost(url); post.addHeader(entity.getContentType()); post.setEntity(entity); try { resp = getHttpClient().execute(post); if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { InputStream istream = (resp.getEntity() != null) ? resp.getEntity().getContent() : null; if (istream != null) { BufferedReader ireader = new BufferedReader(new InputStreamReader(istream)); ResString = ireader.readLine().trim(); Log.e(TAG, "Http Resp = " + ResString); } } else { Log.e(TAG, "getStatusCode = " + resp.getStatusLine().getStatusCode()); } } catch (final IOException e) { Log.e(TAG, "getHttpClient().execute(post)", e); return null; } finally { Log.v(TAG, "getAuthtoken completing"); } return ResString; }
From source file:securitytools.veracode.http.request.GetPreScanResultsRequest.java
public GetPreScanResultsRequest(String applicationId, String buildId, String sandboxId) { super("/api/4.0/getprescanresults.do"); if (applicationId == null) { throw new IllegalArgumentException("Application id cannot be null."); }/* w ww . ja va 2 s . com*/ List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("app_id", applicationId)); if (buildId != null) { formparams.add(new BasicNameValuePair("build_id", buildId)); } if (sandboxId != null) { formparams.add(new BasicNameValuePair("sandbox_id", sandboxId)); } setEntity(new UrlEncodedFormEntity(formparams, Consts.UTF_8)); }
From source file:com.lexicalintelligence.action.extract.ExtractRequest.java
@SuppressWarnings("unchecked") public ExtractResponse execute() { ExtractResponse extractResponse = new ExtractResponse(); HttpPost post = new HttpPost(client.getUrl() + PATH); Reader reader = null;// w w w .j a va 2s. co m try { List<NameValuePair> params = new ArrayList<>(); for (List<NameValuePair> val : fields.values()) { params.addAll(val); } post.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8)); HttpResponse response = client.getHttpClient().execute(post); reader = new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8); Map<String, Object> result = client.getObjectMapper().readValue(reader, new TypeReference<Map<String, Object>>() { }); extractResponse.setId((String) result.get("id")); extractResponse.setEntries((Map<String, List<LexicalEntry>>) result.get("entries")); // if (projection == null) { // // extractResponse.setEntries(result.get("entries").stream().collect(Collectors.toList())); // } /*else { System.out.println(result); extractResponse.setEntries(result.get("text").stream().filter(x -> { for (String type : x.getType()) { boolean contains = projection.contains(type); if (contains) { return include; } } return !include; }).collect(Collectors.toList())); }*/ } catch (Exception e) { e.printStackTrace(); log.error(e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { log.error(e); } } } return extractResponse; }
From source file:securitytools.veracode.http.request.CreateBuildRequest.java
public CreateBuildRequest(String applicationId, String versionName) { super("/api/4.0/createbuild.do"); if (applicationId == null) { throw new IllegalArgumentException("Application id cannot be null."); }//w ww . j a va 2 s . co m List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("app_id", applicationId)); if (versionName == null) { DateFormat formatter = new SimpleDateFormat(VERSION_DATE_FORMAT); versionName = formatter.format(new Date()); } formparams.add(new BasicNameValuePair("version", versionName)); setEntity(new UrlEncodedFormEntity(formparams, Consts.UTF_8)); }
From source file:com.vmware.bdd.cli.auth.LoginClientImpl.java
/** * * attempt login by posting credentials to serengeti server * * @param serengetiURL https://host:8443/serengeti/api/ * @param userName vc user name/* ww w . ja v a2s .c o m*/ * @param password vc password * @throws IOException connection exception */ public LoginResponse login(final String serengetiURL, String userName, String password) throws IOException { String url = serengetiURL + 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 response; try { response = client1.execute(loginPost); LOGGER.debug("resp code is: " + response.getStatusLine()); int responseCode = response.getStatusLine().getStatusCode(); LoginResponse loginResponse; if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { //normal response String cookieValue = null; Header[] setCookieHeaders = response.getHeaders(SET_COOKIE_HEADER); if (ArrayUtils.isNotEmpty(setCookieHeaders)) { cookieValue = setCookieHeaders[0].getValue(); if (StringUtils.isNotBlank(cookieValue) && cookieValue.contains(";")) { cookieValue = cookieValue.split(";")[0]; } } loginResponse = new LoginResponse(responseCode, cookieValue); } else { loginResponse = new LoginResponse(responseCode, null); } return loginResponse; } finally { loginPost.releaseConnection(); } }
From source file:co.tuzza.clicksend4j.http.HttpClientUtilsTest.java
/** * Test of getHttpClient method, of class HttpClientUtils. *//*from ww w . ja v a2s.c o m*/ @Test public void testGetHttpClient() throws ProtocolException, UnsupportedEncodingException, IOException { System.out.println("getHttpClient"); HttpClientUtils instance = new HttpClientUtils(10000, 10000); HttpClient expResult = null; HttpClient httpClient = instance.getHttpClient(); // https://api.clicksend.com/rest/v2/send.json?method=rest&message=This%20is%20the%20message&to=%2B8611111111111%2C%2B61411111111%20 String loginPassword = "tuzzmaniandevil:5C148EA8-D97B-B5F8-1A3F-1BE0658F4DF5"; String auth = Base64.encodeBase64String(loginPassword.getBytes()); HttpUriRequest method; List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("method", "rest")); params.add(new BasicNameValuePair("to", "+61411111111")); params.add(new BasicNameValuePair("message", "This is a test message")); HttpPost httpPost = new HttpPost("https://api.clicksend.com/rest/v2/send.json"); httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); httpPost.setHeader("Authorization", "Basic " + auth); method = httpPost; String url = "https://api.clicksend.com/rest/v2/send.json" + "?" + URLEncodedUtils.format(params, "utf-8"); HttpResponse httpResponse = httpClient.execute(method); int status = httpResponse.getStatusLine().getStatusCode(); String response = new BasicResponseHandler().handleResponse(httpResponse); }
From source file:org.dataconservancy.ui.it.support.ViewCollectionDetailsRequest.java
public HttpPost asHttpPost() { if (collectionIdToView == null) { throw new RuntimeException( "Id of the collection to be viewed has not been set. Call setCollectionIdToView() first"); }//w w w . j a v a2 s. c o m HttpPost post = null; try { post = new HttpPost(urlConfig.getViewCollectionUrl(collectionIdToView).toURI()); } catch (URISyntaxException e) { throw new RuntimeException(e.getMessage(), e); } List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("selectedCollectionId", "collectionIdToView")); params.add(new BasicNameValuePair(STRIPES_EVENT, "View Collections Details")); HttpEntity entity = null; try { entity = new UrlEncodedFormEntity(params, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e.getMessage(), e); } post.setEntity(entity); return post; }