In this tutorial – we’ll POST with the HttpClient 4 – using first authorization, then the fluent HttpClient API. Finally – we will discuss how to upload File using HttpClient.
First – let’s go over a simple example and send a POST request using HttpClient.
In the following example – we will do a POST with two parameters – “username” and “password“:
@Test public void whenPostRequestUsingHttpClient_thenCorrect() throws ClientProtocolException, IOException { CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://www.example.com"); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("username", "John")); params.add(new BasicNameValuePair("password", "pass")); httpPost.setEntity(new UrlEncodedFormEntity(params)); CloseableHttpResponse response = client.execute(httpPost); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); client.close(); }
Note how we used a List of NameValuePair to include parameters in the POST request.
Next – let’s see how to do a POST with Authentication credentials using the HttpClient.
In the following example – we send a post request to a URL secured with Basic Authentication:
@Test public void whenPostRequestWithAuthorizationUsingHttpClient_thenCorrect() throws ClientProtocolException, IOException, AuthenticationException { CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://www.example.com"); httpPost.setEntity(new StringEntity("test post")); UsernamePasswordCredentials creds = new UsernamePasswordCredentials("John", "pass"); httpPost.addHeader(new BasicScheme().authenticate(creds, httpPost, null)); CloseableHttpResponse response = client.execute(httpPost); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); client.close(); }
Now – let’s see how to send POST request with a JSON body using the HttpClient.
In the following example – we’re sending some person information (id, name) as JSON:
@Test public void whenPostJsonUsingHttpClient_thenCorrect() throws ClientProtocolException, IOException { CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://www.example.com"); String json = "{"id":1,"name":"John"}"; StringEntity entity = new StringEntity(json); httpPost.setEntity(entity); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); CloseableHttpResponse response = client.execute(httpPost); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); client.close(); }
Note how we’re using the StringEntity to set the body of the request.
We are also setting the ContentType header to application/json to give the server the necessary information about the representation of the content we’re sending.
Next – let’s POST with the HttpClient Fluent API; we’re going to send a request with two parameters “username” and “password“:
@Test public void whenPostFormUsingHttpClientFluentAPI_thenCorrect() throws ClientProtocolException, IOException { HttpResponse response = Request.Post("http://www.example.com").bodyForm( Form.form().add("username", "John").add("password", "pass").build()) .execute().returnResponse(); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); }
Now – let’s POST a Multipart Request – in the following example, we’ll post a File, username and password using MultipartEntityBuilder:
@Test public void whenSendMultipartRequestUsingHttpClient_thenCorrect() throws ClientProtocolException, IOException { CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://www.example.com"); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("username", "John"); builder.addTextBody("password", "pass"); builder.addBinaryBody("file", new File("test.txt"), ContentType.APPLICATION_OCTET_STREAM, "file.ext"); HttpEntity multipart = builder.build(); httpPost.setEntity(multipart); CloseableHttpResponse response = client.execute(httpPost); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); client.close(); }
Next – let’s see how to upload a File using the HttpClient – we’ll upload the “test.txt” file using MultipartEntityBuilder:
@Test public void whenUploadFileUsingHttpClient_thenCorrect() throws ClientProtocolException, IOException { CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://www.example.com"); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addBinaryBody("file", new File("test.txt"), ContentType.APPLICATION_OCTET_STREAM, "file.ext"); HttpEntity multipart = builder.build(); httpPost.setEntity(multipart); CloseableHttpResponse response = client.execute(httpPost); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); client.close(); }
Finally – let’s see how to get the progress of File upload using HttpClient. In the following example – we will extend HttpEntityWrapper to gain visibility into the upload process:
First – here’s the upload method:
@Test public void whenGetUploadFileProgressUsingHttpClient_thenCorrect() throws ClientProtocolException, IOException { CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://www.example.com"); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addBinaryBody("file", new File("test.txt"), ContentType.APPLICATION_OCTET_STREAM, "file.ext"); HttpEntity multipart = builder.build(); ProgressEntityWrapper.ProgressListener pListener = new ProgressEntityWrapper.ProgressListener() { @Override public void progress(float percentage) { assertFalse(Float.compare(percentage, 100) > 0); } }; httpPost.setEntity(new ProgressEntityWrapper(multipart, pListener)); CloseableHttpResponse response = client.execute(httpPost); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); client.close(); }
And here is the interface ProgressListener that enables us to observe the upload progress:
public static interface ProgressListener { void progress(float percentage); }
And here our extended version of HttpEntityWrapper “ProgressEntityWrapper“:
public class ProgressEntityWrapper extends HttpEntityWrapper { private ProgressListener listener; public ProgressEntityWrapper(HttpEntity entity, ProgressListener listener) { super(entity); this.listener = listener; } @Override public void writeTo(OutputStream outstream) throws IOException { super.writeTo(new CountingOutputStream(outstream, listener, getContentLength())); } }
And the extended version of FilterOutputStream “CountingOutputStream“:
public static class CountingOutputStream extends FilterOutputStream { private ProgressListener listener; private long transferred; private long totalBytes; public CountingOutputStream( OutputStream out, ProgressListener listener, long totalBytes) { super(out); this.listener = listener; transferred = 0; this.totalBytes = totalBytes; } @Override public void write(byte[] b, int off, int len) throws IOException { out.write(b, off, len); transferred += len; listener.progress(getCurrentProgress()); } @Override public void write(int b) throws IOException { out.write(b); transferred++; listener.progress(getCurrentProgress()); } private float getCurrentProgress() { return ((float) transferred / totalBytes) * 100; } }
Note that:
In this tutorial we illustrated the most common ways to send POST HTTP Requests with the Apache HttpClient 4.
We learned how to send post request with Authorization, how to post using HttpClient fluent API and how to upload a file and track its progress.
原文:http://www.cnblogs.com/danielchang/p/5141747.html