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