Updated multipart POST example
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1426534 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1db3da376e
commit
d695c0e8b6
|
@ -29,13 +29,14 @@ package org.apache.http.examples.entity.mime;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.HttpClient;
|
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
|
import org.apache.http.entity.ContentType;
|
||||||
import org.apache.http.entity.mime.MultipartEntity;
|
import org.apache.http.entity.mime.MultipartEntity;
|
||||||
import org.apache.http.entity.mime.content.FileBody;
|
import org.apache.http.entity.mime.content.FileBody;
|
||||||
import org.apache.http.entity.mime.content.StringBody;
|
import org.apache.http.entity.mime.content.StringBody;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClients;
|
||||||
import org.apache.http.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -48,13 +49,13 @@ public class ClientMultipartFormPost {
|
||||||
System.out.println("File path not given");
|
System.out.println("File path not given");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
HttpClient httpclient = new DefaultHttpClient();
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||||
try {
|
try {
|
||||||
HttpPost httppost = new HttpPost("http://localhost:8080" +
|
HttpPost httppost = new HttpPost("http://localhost:8080" +
|
||||||
"/servlets-examples/servlet/RequestInfoExample");
|
"/servlets-examples/servlet/RequestInfoExample");
|
||||||
|
|
||||||
FileBody bin = new FileBody(new File(args[0]));
|
FileBody bin = new FileBody(new File(args[0]));
|
||||||
StringBody comment = new StringBody("A binary file of some kind");
|
StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
|
||||||
|
|
||||||
MultipartEntity reqEntity = new MultipartEntity();
|
MultipartEntity reqEntity = new MultipartEntity();
|
||||||
reqEntity.addPart("bin", bin);
|
reqEntity.addPart("bin", bin);
|
||||||
|
@ -63,17 +64,20 @@ public class ClientMultipartFormPost {
|
||||||
httppost.setEntity(reqEntity);
|
httppost.setEntity(reqEntity);
|
||||||
|
|
||||||
System.out.println("executing request " + httppost.getRequestLine());
|
System.out.println("executing request " + httppost.getRequestLine());
|
||||||
HttpResponse response = httpclient.execute(httppost);
|
CloseableHttpResponse response = httpclient.execute(httppost);
|
||||||
HttpEntity resEntity = response.getEntity();
|
try {
|
||||||
|
|
||||||
System.out.println("----------------------------------------");
|
System.out.println("----------------------------------------");
|
||||||
System.out.println(response.getStatusLine());
|
System.out.println(response.getStatusLine());
|
||||||
|
HttpEntity resEntity = response.getEntity();
|
||||||
if (resEntity != null) {
|
if (resEntity != null) {
|
||||||
System.out.println("Response content length: " + resEntity.getContentLength());
|
System.out.println("Response content length: " + resEntity.getContentLength());
|
||||||
}
|
}
|
||||||
EntityUtils.consume(resEntity);
|
EntityUtils.consume(resEntity);
|
||||||
} finally {
|
} finally {
|
||||||
try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
|
response.close();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
httpclient.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue