|
| 1 | +package com.baeldung.httpclient; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import org.apache.http.HttpResponse; |
| 11 | +import org.apache.http.NameValuePair; |
| 12 | +import org.apache.http.client.ClientProtocolException; |
| 13 | +import org.apache.http.client.HttpClient; |
| 14 | +import org.apache.http.client.entity.UrlEncodedFormEntity; |
| 15 | +import org.apache.http.client.methods.HttpGet; |
| 16 | +import org.apache.http.client.methods.HttpPost; |
| 17 | +import org.apache.http.entity.StringEntity; |
| 18 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 19 | +import org.apache.http.message.BasicNameValuePair; |
| 20 | + |
| 21 | +import com.baeldung.http.HttpResponseWrapper; |
| 22 | +import com.baeldung.http.ParameterStringBuilder; |
| 23 | + |
| 24 | +public class HttpClientRequestBuilder { |
| 25 | + |
| 26 | + public HttpResponseWrapper sendGetRequest(String url, Map<String, String> parameters) { |
| 27 | + HttpClient client = HttpClientBuilder.create() |
| 28 | + .build(); |
| 29 | + if (parameters != null) { |
| 30 | + url += "?" + ParameterStringBuilder.getParamsString(parameters); |
| 31 | + } |
| 32 | + HttpGet request = new HttpGet(url); |
| 33 | + try { |
| 34 | + HttpResponse response = client.execute(request); |
| 35 | + |
| 36 | + HttpResponseWrapper responseWrapper = new HttpResponseWrapper(); |
| 37 | + responseWrapper.setStatus(response.getStatusLine() |
| 38 | + .getStatusCode()); |
| 39 | + BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity() |
| 40 | + .getContent())); |
| 41 | + |
| 42 | + String line = "", content = ""; |
| 43 | + while ((line = in.readLine()) != null) { |
| 44 | + content += line; |
| 45 | + } |
| 46 | + responseWrapper.setContent(content); |
| 47 | + return responseWrapper; |
| 48 | + } catch (ClientProtocolException e) { |
| 49 | + e.printStackTrace(); |
| 50 | + return null; |
| 51 | + } catch (IOException e) { |
| 52 | + e.printStackTrace(); |
| 53 | + return null; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public HttpResponseWrapper sendPostRequestWithParameters(String url, Map<String, String> parameters) { |
| 58 | + HttpClient client = HttpClientBuilder.create() |
| 59 | + .build(); |
| 60 | + HttpPost request = new HttpPost(url); |
| 61 | + |
| 62 | + try { |
| 63 | + if (parameters != null) { |
| 64 | + List<NameValuePair> nameValuePairs = new ArrayList<>(); |
| 65 | + parameters.forEach((key, value) -> nameValuePairs.add(new BasicNameValuePair(key, value))); |
| 66 | + request.setEntity(new UrlEncodedFormEntity(nameValuePairs)); |
| 67 | + } |
| 68 | + |
| 69 | + HttpResponse response = client.execute(request); |
| 70 | + |
| 71 | + HttpResponseWrapper responseWrapper = new HttpResponseWrapper(); |
| 72 | + responseWrapper.setStatus(response.getStatusLine() |
| 73 | + .getStatusCode()); |
| 74 | + BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity() |
| 75 | + .getContent())); |
| 76 | + |
| 77 | + String line = "", content = ""; |
| 78 | + while ((line = in.readLine()) != null) { |
| 79 | + content += line; |
| 80 | + } |
| 81 | + responseWrapper.setContent(content); |
| 82 | + return responseWrapper; |
| 83 | + } catch (ClientProtocolException e) { |
| 84 | + e.printStackTrace(); |
| 85 | + return null; |
| 86 | + } catch (IOException e) { |
| 87 | + e.printStackTrace(); |
| 88 | + return null; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public HttpResponseWrapper sendPostRequestWithJson(String url, String json) { |
| 93 | + HttpClient client = HttpClientBuilder.create() |
| 94 | + .build(); |
| 95 | + HttpPost request = new HttpPost(url); |
| 96 | + |
| 97 | + try { |
| 98 | + request.addHeader("Content-Type", "application/json"); |
| 99 | + request.setEntity(new StringEntity(json)); |
| 100 | + |
| 101 | + HttpResponse response = client.execute(request); |
| 102 | + |
| 103 | + HttpResponseWrapper responseWrapper = new HttpResponseWrapper(); |
| 104 | + responseWrapper.setStatus(response.getStatusLine() |
| 105 | + .getStatusCode()); |
| 106 | + BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity() |
| 107 | + .getContent())); |
| 108 | + |
| 109 | + String line = "", content = ""; |
| 110 | + while ((line = in.readLine()) != null) { |
| 111 | + content += line; |
| 112 | + } |
| 113 | + responseWrapper.setContent(content); |
| 114 | + return responseWrapper; |
| 115 | + } catch (ClientProtocolException e) { |
| 116 | + e.printStackTrace(); |
| 117 | + return null; |
| 118 | + } catch (IOException e) { |
| 119 | + e.printStackTrace(); |
| 120 | + return null; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments