Skip to content

Instantly share code, notes, and snippets.

@Bresiu
Created October 9, 2017 19:08
Show Gist options
  • Save Bresiu/8ab67aad551f8ac8e69443d04302ae66 to your computer and use it in GitHub Desktop.
Save Bresiu/8ab67aad551f8ac8e69443d04302ae66 to your computer and use it in GitHub Desktop.
makeJava HttpPost command and print length of content
HttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://httpbin.org/post");
List<NameValuePair> params = new ArrayList<>(2);
params.add(new BasicNameValuePair("param-1", "12345"));
params.add(new BasicNameValuePair("param-2", "Hello2!"));
params.add(new BasicNameValuePair("param-3", "Hello3!"));
params.add(new BasicNameValuePair("param-4", "Hello4!"));
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
System.out.println("length: " + entity.getContentLength());
String inputLine;
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
try {
while ((inputLine = br.readLine()) != null) {
System.out.println(inputLine);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment