cleanup work for httpclient

This commit is contained in:
eugenp 2014-05-22 20:57:29 +03:00
parent f7bd1ca219
commit f4104354b4
4 changed files with 246 additions and 238 deletions

View File

@ -32,11 +32,5 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="lib" path="/Users/Elena/Downloads/httpcomponents-client-4.3.3 2/lib/httpclient-4.3.3.jar"/>
<classpathentry kind="lib" path="/Users/Elena/Downloads/httpcomponents-client-4.3.3 2/lib/fluent-hc-4.3.3.jar"/>
<classpathentry kind="lib" path="/Users/Elena/Downloads/httpcomponents-client-4.3.3 2/lib/httpmime-4.3.3.jar"/>
<classpathentry kind="lib" path="/Users/Elena/Downloads/httpcomponents-client-4.3.3 2/lib/httpcore-4.3.2.jar"/>
<classpathentry kind="lib" path="/Users/Elena/Downloads/httpcomponents-client-4.3.3 2/lib/commons-logging-1.1.3.jar"/>
<classpathentry kind="lib" path="/Users/Elena/Downloads/httpcomponents-client-4.3.3 2/lib/commons-codec-1.6.jar"/>
<classpathentry kind="output" path="target/classes"/> <classpathentry kind="output" path="target/classes"/>
</classpath> </classpath>

View File

@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId> <groupId>org.baeldung</groupId>
<artifactId>httpclient</artifactId> <artifactId>httpclient</artifactId>
@ -41,6 +42,24 @@
<version>${httpcore.version}</version> <version>${httpcore.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<!-- logging --> <!-- logging -->
<dependency> <dependency>

View File

@ -0,0 +1 @@
some content

View File

@ -2,11 +2,15 @@ package org.baeldung.httpclient;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import org.apache.http.Header;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.fluent.Request; import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response; import org.apache.http.client.fluent.Response;
@ -14,9 +18,11 @@ import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
public class HttpClientMultipartTest { public class HttpClientMultipartTest {
@ -40,18 +46,11 @@ public class HttpClientMultipartTest {
textFileName = ".\temp.txt"; textFileName = ".\temp.txt";
imageFileName = "image.jpg"; imageFileName = "image.jpg";
zipFileName = "zipFile.zip"; zipFileName = "zipFile.zip";
} }
@Before @Test
public void setUp() throws Exception { @Ignore
}
/* @Test
public final void whenUploadWithAddPart_thenNoExceptions() throws IOException { public final void whenUploadWithAddPart_thenNoExceptions() throws IOException {
final File file = new File(textFileName); final File file = new File(textFileName);
final FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY); final FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
final StringBody stringBody1 = new StringBody("This is message 1", ContentType.MULTIPART_FORM_DATA); final StringBody stringBody1 = new StringBody("This is message 1", ContentType.MULTIPART_FORM_DATA);
@ -65,39 +64,39 @@ public class HttpClientMultipartTest {
post.setEntity(entity); post.setEntity(entity);
final HttpResponse response = client.execute(post); final HttpResponse response = client.execute(post);
System.out.println(getContent(response)); System.out.println(getContent(response));
Header[] headers = response.getAllHeaders(); final Header[] headers = response.getAllHeaders();
for (Header thisHeader : headers) { for (final Header thisHeader : headers) {
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
} }
} */ }
/*@Test
public final void whenUploadWithAddBinaryBodyandAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException {
@Test
@Ignore
public final void whenUploadWithAddBinaryBodyandAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException {
final File file = new File(textFileName); final File file = new File(textFileName);
String message = "This is a multipart post"; final String message = "This is a multipart post";
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("submitted", file, ContentType.DEFAULT_BINARY, textFileName); builder.addBinaryBody("submitted", file, ContentType.DEFAULT_BINARY, textFileName);
builder.addTextBody("note", message, ContentType.TEXT_PLAIN); builder.addTextBody("note", message, ContentType.TEXT_PLAIN);
final HttpEntity entity = builder.build(); final HttpEntity entity = builder.build();
post.setEntity(entity); post.setEntity(entity);
HttpResponse response = client.execute(post); final HttpResponse response = client.execute(post);
System.out.println(getContent(response)); System.out.println(getContent(response));
Header[] headers = response.getAllHeaders(); final Header[] headers = response.getAllHeaders();
for (Header thisHeader : headers) { for (final Header thisHeader : headers) {
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
} }
}
}*/ @Test
@Ignore
/* @Test
public final void whenUploadWithAddBinaryBody_NoType_andAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException { public final void whenUploadWithAddBinaryBody_NoType_andAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException {
final File file = new File(imageFileName); final File file = new File(imageFileName);
final String message = "This is a multipart post"; final String message = "This is a multipart post";
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("submitted", file, ContentType.DEFAULT_BINARY, textFileName); builder.addBinaryBody("submitted", file, ContentType.DEFAULT_BINARY, textFileName);
// builder.addBinaryBody("upfile", fileBin); // builder.addBinaryBody("upfile", fileBin);
@ -106,15 +105,15 @@ public class HttpClientMultipartTest {
post.setEntity(entity); post.setEntity(entity);
final HttpResponse response = client.execute(post); final HttpResponse response = client.execute(post);
System.out.println(getContent(response)); System.out.println(getContent(response));
Header[] headers = response.getAllHeaders(); final Header[] headers = response.getAllHeaders();
for (Header thisHeader : headers) { for (final Header thisHeader : headers) {
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
} }
}
}*/ @Test
@Ignore
/* @Test
public final void whenUploadWithAddBinaryBody_InputStream_andTextBody_ThenNoException() throws ClientProtocolException, IOException { public final void whenUploadWithAddBinaryBody_InputStream_andTextBody_ThenNoException() throws ClientProtocolException, IOException {
final InputStream inputStream = new FileInputStream(zipFileName); final InputStream inputStream = new FileInputStream(zipFileName);
final String message = "This is a multipart post"; final String message = "This is a multipart post";
@ -128,19 +127,17 @@ public class HttpClientMultipartTest {
final HttpResponse response = client.execute(post); final HttpResponse response = client.execute(post);
System.out.println(getContent(response)); System.out.println(getContent(response));
Header[] headers = response.getAllHeaders(); final Header[] headers = response.getAllHeaders();
for (Header thisHeader : headers) { for (final Header thisHeader : headers) {
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
} }
}
}*/
// BUG // BUG
@Test @Test
public final void whenFluentRequestWithBody_ThenNoException() throws IOException { public final void whenFluentRequestWithBody_ThenNoException() throws IOException {
final String fileName = ".\temp.txt"; final String fileName = ".\temp.txt";
final File fileBin = new File(fileName); final File fileBin = new File(fileName);
final String message = "This is a multipart post"; final String message = "This is a multipart post";
@ -149,12 +146,10 @@ public class HttpClientMultipartTest {
builder.addBinaryBody("upfile", fileBin, ContentType.DEFAULT_BINARY, fileName); builder.addBinaryBody("upfile", fileBin, ContentType.DEFAULT_BINARY, fileName);
builder.addTextBody("note", message, ContentType.TEXT_PLAIN); builder.addTextBody("note", message, ContentType.TEXT_PLAIN);
final HttpEntity entity = builder.build(); final HttpEntity entity = builder.build();
final Response response = Request.Post(SERVER) final Response response = Request.Post(SERVER).body(entity).execute();
.body(entity).execute();
} }
public static String getContent(final HttpResponse response) throws IOException { public static String getContent(final HttpResponse response) throws IOException {
final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String body = ""; String body = "";
String content = ""; String content = "";
@ -165,5 +160,4 @@ public class HttpClientMultipartTest {
return content.trim(); return content.trim();
} }
} }