Created
October 9, 2017 19:08
-
-
Save Bresiu/8ab67aad551f8ac8e69443d04302ae66 to your computer and use it in GitHub Desktop.
makeJava HttpPost command and print length of content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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