Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
package com.baeldung.httpclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
package com.baeldung.http;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -18,12 +10,17 @@
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

import com.baeldung.http.HttpResponseWrapper;
import com.baeldung.http.ParameterStringBuilder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class HttpClientRequestBuilder {

public HttpResponseWrapper sendGetRequest(String url, Map<String, String> parameters) {
public HttpResponseWrapper sendGetRequest(String url, Map<String, String> parameters) throws UnsupportedEncodingException {
HttpClient client = HttpClientBuilder.create()
.build();
if (parameters != null) {
Expand All @@ -39,19 +36,16 @@ public HttpResponseWrapper sendGetRequest(String url, Map<String, String> parame
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent()));

String line = "", content = "";
String line, content = "";
while ((line = in.readLine()) != null) {
content += line;
}
responseWrapper.setContent(content);
return responseWrapper;
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
return null;
}

public HttpResponseWrapper sendPostRequestWithParameters(String url, Map<String, String> parameters) {
Expand All @@ -61,8 +55,9 @@ public HttpResponseWrapper sendPostRequestWithParameters(String url, Map<String,

try {
if (parameters != null) {
List<NameValuePair> nameValuePairs = new ArrayList<>();
parameters.forEach((key, value) -> nameValuePairs.add(new BasicNameValuePair(key, value)));
List<NameValuePair> nameValuePairs = parameters.entrySet().stream()
.map(e -> new BasicNameValuePair(e.getKey(), e.getValue()))
.collect(Collectors.toList());
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}

Expand All @@ -74,19 +69,17 @@ public HttpResponseWrapper sendPostRequestWithParameters(String url, Map<String,
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent()));

String line = "", content = "";
String line, content = "";
while ((line = in.readLine()) != null) {
content += line;
}
responseWrapper.setContent(content);
return responseWrapper;
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}

return null;
}

public HttpResponseWrapper sendPostRequestWithJson(String url, String json) {
Expand All @@ -106,19 +99,16 @@ public HttpResponseWrapper sendPostRequestWithJson(String url, String json) {
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent()));

String line = "", content = "";
String line, content = "";
while ((line = in.readLine()) != null) {
content += line;
}
responseWrapper.setContent(content);
return responseWrapper;
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@
import java.util.Map;

public class ParameterStringBuilder {
public static String getParamsString(Map<String, String> params) {
public static String getParamsString(Map<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();

params.forEach((key, value) -> {
try {
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value, "UTF-8"));
result.append("&");
} catch (UnsupportedEncodingException exc) {
}
});
for (Map.Entry<String, String> entry : params.entrySet()) {
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
result.append("&");
}

String resultString = result.toString();
if (resultString.length() > 0) {
resultString = resultString.substring(0, resultString.length() - 1);
}
return resultString;
return resultString.length() > 0
? resultString.substring(0, resultString.length() - 1)
: resultString;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.baeldung.httpclient;
package com.baeldung.http;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

import com.baeldung.http.HttpResponseWrapper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class HttpClientRequestBuilderTest {
private HttpClientRequestBuilder requestBuilder;
Expand All @@ -20,7 +19,7 @@ public void setup() {
}

@Test
public void whenGetRequest_thenOk() {
public void whenGetRequest_thenOk() throws UnsupportedEncodingException {
Map<String, String> parameters = new HashMap<>();
parameters.put("param1", "val");
HttpResponseWrapper response = requestBuilder.sendGetRequest("http://www.example.com",parameters);
Expand Down