} else if (method instanceof HttpPost) { HttpPost post = new HttpPost(uri); post.setEntity(((HttpPost) method).getEntity()); post.setHeaders(method.getAllHeaders()); method = post;
HttpEntity entity = postMethod.getEntity(); if (entity != null && entity instanceof FileExposingFileEntity)
private String extract(HttpPost post) throws IOException { return EntityUtils.toString(post.getEntity()); }
try { HttpPost postMethod = new HttpPost("your url"); Log.i(TAG,"HTTP Entiry : " + convertStreamToString(postMethod.getEntity().getContent())); }catch (Exception e){ e.printStackTrace(); }
private String getRequestPayload(HttpRequestBase request) throws IOException { if (!(request instanceof HttpPost)) { return EMPTY_PAYLOAD; } HttpPost postRequest = (HttpPost) request; if (postRequest.getEntity() == null) { return EMPTY_PAYLOAD; } if (StringUtils.isNotEmpty(postRequest.getEntity().getContentType().getValue())) { String contentType = postRequest.getEntity().getContentType().getValue(); if (contentType.contains("multipart/form-data")) { return EMPTY_PAYLOAD; } } return IOUtils.toString(postRequest.getEntity().getContent(), StandardCharsets.UTF_8); }
protected HttpResponse sendRequest(HttpUriRequest request, boolean consumeResponse) throws ClientProtocolException, IOException { if (log.isDebugEnabled()) log.debug("Geoscaling request: "+ request.getURI()+ (request instanceof HttpPost ? " "+((HttpPost)request).getEntity() : "")); HttpResponse response = httpClient.execute(request); if (log.isDebugEnabled()) log.debug("Geoscaling response: "+response); if (consumeResponse && response.getEntity() != null) EntityUtils.consume(response.getEntity()); return response; }
protected HttpResponse sendRequest(HttpUriRequest request, boolean consumeResponse) throws ClientProtocolException, IOException { if (log.isDebugEnabled()) log.debug("Geoscaling request: "+ request.getURI()+ (request instanceof HttpPost ? " "+((HttpPost)request).getEntity() : "")); HttpResponse response = httpClient.execute(request); if (log.isDebugEnabled()) log.debug("Geoscaling response: "+response); if (consumeResponse) EntityUtils.consume(response.getEntity()); return response; }
HttpClient client = new DefaultHttpClient(); HttpPost method = new HttpPost("http://localhost:9090/drools-guvnor/KeystoneServlet"); HttpEntity entity = method.getEntity(); List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>(); nvps.add(new BasicNameValuePair("directory", directory)); nvps.add(new BasicNameValuePair("username", username)); nvps.add(new BasicNameValuePair("password", password)); method.setEntity(new UrlEncodedFormEntity(nvps)); HttpResponse httpResponse = client.execute(method); int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { InputStream is = httpResponse.getEntity().getContent(); //convert your stream into a string and convert it into a boolean }
public void trigger() { try (final CloseableHttpClient httpclient = HttpClients.createDefault()) { LOG.debug("Sending '" + event + "' event to IFTTT."); final HttpPost httpPost = new HttpPost(String.format(IFTTT_URL, event, key)); httpPost.setHeader("Content-type", "application/json"); final StringEntity stringEntity = new StringEntity(String.format(IFTTT_JSON, value1, value2, value3)); httpPost.setEntity(stringEntity); final CloseableHttpResponse closeableHttpResponse = httpclient.execute(httpPost); final int statusCode = closeableHttpResponse.getStatusLine().getStatusCode(); if (statusCode != 200) { LOG.warn("Received HTTP Status {} for request to {} with body {}", statusCode, httpPost.getURI(), httpPost.getEntity()); } } catch (IOException e) { LOG.error("Error invoking request: {}", e); } } }
public void trigger() { try (final CloseableHttpClient httpclient = HttpClients.createDefault()) { LOG.debug("Sending '" + event + "' event to IFTTT."); final HttpPost httpPost = new HttpPost(String.format(IFTTT_URL, event, key)); httpPost.setHeader("Content-type", "application/json"); final StringEntity stringEntity = new StringEntity(String.format(IFTTT_JSON, value1, value2, value3)); httpPost.setEntity(stringEntity); final CloseableHttpResponse closeableHttpResponse = httpclient.execute(httpPost); final int statusCode = closeableHttpResponse.getStatusLine().getStatusCode(); if (statusCode != 200) { LOG.warn("Received HTTP Status {} for request to {} with body {}", statusCode, httpPost.getURI(), httpPost.getEntity()); } } catch (IOException e) { LOG.error("Error invoking request: {}", e); } } }
// Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(OAuth2ClientCredentials.ACCESS_TOKEN_URL); try { // Add your data List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5); nameValuePairs.add(new BasicNameValuePair("client_id", OAuth2ClientCredentials.CLIENT_ID)); nameValuePairs.add(new BasicNameValuePair("client_secret", OAuth2ClientCredentials.CLIENT_SECRET)); nameValuePairs.add(new BasicNameValuePair("code", code)); nameValuePairs.add(new BasicNameValuePair("grant_type", OAuth2ClientCredentials.TOKEN_GRANT_TYPE)); nameValuePairs.add(new BasicNameValuePair("redirect_uri", OAuth2ClientCredentials.REDIRECT_URI)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); Log.i(TAG, "httppost= "+inputStreamToString(httppost.getEntity().getContent())); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); Log.i(TAG, "response= "+inputStreamToString(response.getEntity().getContent())); } catch (ClientProtocolException e) { Log.e(TAG, "error= "+e); } catch (IOException e) { Log.e(TAG, "error= "+e); }
String url = "https://yoururl.com"; HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); // add header post.setHeader("User-Agent", USER_AGENT); List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("xml", xmlString)); post.setEntity(new UrlEncodedFormEntity(urlParameters)); HttpResponse response = client.execute(post); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + post.getEntity()); System.out.println("Response Code : " + response.getStatusLine().getStatusCode()); BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } System.out.println(result.toString());
String url = "https://yoururl.com"; HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); // add header post.setHeader("User-Agent", USER_AGENT); List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("xml", xmlString)); post.setEntity(new UrlEncodedFormEntity(urlParameters)); HttpResponse response = client.execute(post); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + post.getEntity()); System.out.println("Response Code : " + response.getStatusLine().getStatusCode()); BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } System.out.println(result.toString());
public void post(ParserSummary parserSummary, List<String> log, String project) throws IOException { HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(ReportingRequest.POST_URL); // add header post.setHeader("User-Agent", USER_AGENT); Report report = new Report(project, parserSummary, log); StringEntity input = new StringEntity(gson.toJson(report)); input.setContentType("application/json"); post.setEntity(input); // send post request HttpResponse response = httpClient.execute(post); System.out.println("Post parameters : " + post.getEntity()); System.out.println("Response Code : " + response.getStatusLine(). getStatusCode()); }
public String postHttp(String url, List<NameValuePair> params, List<NameValuePair> headers) throws IOException { HttpPost post = new HttpPost(url); post.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8)); post.getEntity().toString(); if (headers != null) { for (NameValuePair header : headers) { post.addHeader(header.getName(), header.getValue()); } } HttpClient httpClient = HttpClientBuilder.create().setProxy(proxy).build(); HttpResponse response = httpClient.execute(post); HttpEntity entity = response.getEntity(); if (entity != null) { return EntityUtils.toString(entity); } return null; }
@SuppressWarnings({ "resource", "rawtypes" }) public static Map getResultMap(String url, String xmlParam) { DefaultHttpClient client = new DefaultHttpClient(); client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true); HttpPost httpost = HttpClientConnectionManager.getPostMethod(url); Map map = null; try { httpost.setEntity(new StringEntity(xmlParam, "UTF-8")); HttpResponse response = (HttpResponse) httpclient.execute(httpost); String jsonStr = EntityUtils.toString(((org.apache.http.HttpResponse) response).getEntity(), "UTF-8"); EntityUtils.consume(httpost.getEntity()); log.info(jsonStr); map = doXMLParse(jsonStr); return map; } catch (Exception e) { e.printStackTrace(); } return map; }
@SuppressWarnings({ "resource", "rawtypes" }) public static String getCodeUrl(String url, String xmlParam) { DefaultHttpClient client = new DefaultHttpClient(); client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true); HttpPost httpost = HttpClientConnectionManager.getPostMethod(url); String code_url = ""; try { httpost.setEntity(new StringEntity(xmlParam, "UTF-8")); HttpResponse response = httpclient.execute(httpost); String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8"); EntityUtils.consume(httpost.getEntity()); Map map = doXMLParse(jsonStr); // log.info(map); code_url = map.get("code_url").toString(); } catch (Exception e) { e.printStackTrace(); } return code_url; }
@SuppressWarnings({ "resource", "unchecked" }) public static Map<String, String> getPayNo(String url, String xmlParam) { DefaultHttpClient client = new DefaultHttpClient(); client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true); HttpPost httpost = HttpClientConnectionManager.getPostMethod(url); Map<String, String> map = null; try { httpost.setEntity(new StringEntity(xmlParam, "UTF-8")); HttpResponse response = (HttpResponse) httpclient.execute(httpost); String jsonStr = EntityUtils.toString(((org.apache.http.HttpResponse) response).getEntity(), "UTF-8"); EntityUtils.consume(httpost.getEntity()); log.info(jsonStr); map = doXMLParse(jsonStr); return map; } catch (Exception e) { e.printStackTrace(); } return map; }
HttpPost sentHttpRequest = (HttpPost) Robolectric.getSentHttpRequest(0); StringEntity entity = (StringEntity) sentHttpRequest.getEntity(); String sentPostBody = fromStream(entity.getContent()); assertThat(sentPostBody, equalTo("a post body")); assertThat(entity.getContentType().getValue(), equalTo("text/plain; charset=UTF-8"));
@Test void properHttpPost() throws UnsupportedEncodingException { final int loanId = 1; final RequestId r = new RequestId("user@somewhere.cz", "apitest".toCharArray()); final HttpPost post = ZonkoidConfirmationProvider.getRequest(r, loanId, 200, "https", "somewhere"); SoftAssertions.assertSoftly(softly -> { softly.assertThat(post.getFirstHeader("Accept").getValue()).isEqualTo("text/plain"); softly.assertThat(post.getFirstHeader("Authorization").getValue()) .isNotEmpty() .isEqualTo(ZonkoidConfirmationProvider.getAuthenticationString(r, loanId)); softly.assertThat(post.getFirstHeader("Content-Type").getValue()).isEqualTo( "application/x-www-form-urlencoded"); softly.assertThat(post.getEntity()).isInstanceOf(UrlEncodedFormEntity.class); softly.assertThat(post.getFirstHeader("User-Agent").getValue()).isEqualTo(Defaults.ROBOZONKY_USER_AGENT); }); }