Skip to content

Commit 4600c02

Browse files
authored
Merge pull request eugenp#5990 from faabianr/BAEL-2454
BAEL-2454 Add new section in Java Http Request article
2 parents 21bde94 + 48c0b83 commit 4600c02

2 files changed

Lines changed: 130 additions & 5 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.http;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.io.Reader;
7+
import java.net.HttpURLConnection;
8+
import java.util.Iterator;
9+
import java.util.List;
10+
11+
public class FullResponseBuilder {
12+
public static String getFullResponse(HttpURLConnection con) throws IOException {
13+
StringBuilder fullResponseBuilder = new StringBuilder();
14+
15+
fullResponseBuilder.append(con.getResponseCode())
16+
.append(" ")
17+
.append(con.getResponseMessage())
18+
.append("\n");
19+
20+
con.getHeaderFields()
21+
.entrySet()
22+
.stream()
23+
.filter(entry -> entry.getKey() != null)
24+
.forEach(entry -> {
25+
26+
fullResponseBuilder.append(entry.getKey())
27+
.append(": ");
28+
29+
List<String> headerValues = entry.getValue();
30+
Iterator<String> it = headerValues.iterator();
31+
if (it.hasNext()) {
32+
fullResponseBuilder.append(it.next());
33+
34+
while (it.hasNext()) {
35+
fullResponseBuilder.append(", ")
36+
.append(it.next());
37+
}
38+
}
39+
40+
fullResponseBuilder.append("\n");
41+
});
42+
43+
Reader streamReader = null;
44+
45+
if (con.getResponseCode() > 299) {
46+
streamReader = new InputStreamReader(con.getErrorStream());
47+
} else {
48+
streamReader = new InputStreamReader(con.getInputStream());
49+
}
50+
51+
BufferedReader in = new BufferedReader(streamReader);
52+
String inputLine;
53+
StringBuilder content = new StringBuilder();
54+
while ((inputLine = in.readLine()) != null) {
55+
content.append(inputLine);
56+
}
57+
58+
in.close();
59+
60+
fullResponseBuilder.append("Response: ")
61+
.append(content);
62+
63+
return fullResponseBuilder.toString();
64+
}
65+
}

‎core-java/src/test/java/com/baeldung/http/HttpRequestLiveTest.java‎

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.DataOutputStream;
88
import java.io.IOException;
99
import java.io.InputStreamReader;
10+
import java.io.Reader;
1011
import java.net.CookieManager;
1112
import java.net.HttpCookie;
1213
import java.net.HttpURLConnection;
@@ -48,7 +49,8 @@ public void whenGetRequest_thenOk() throws IOException {
4849
in.close();
4950

5051
assertEquals("status code incorrect", status, 200);
51-
assertTrue("content incorrect", content.toString().contains("Example Domain"));
52+
assertTrue("content incorrect", content.toString()
53+
.contains("Example Domain"));
5254
}
5355

5456
@Test
@@ -89,18 +91,24 @@ public void whenGetCookies_thenOk() throws IOException {
8991
Optional<HttpCookie> usernameCookie = null;
9092
if (cookiesHeader != null) {
9193
List<HttpCookie> cookies = HttpCookie.parse(cookiesHeader);
92-
cookies.forEach(cookie -> cookieManager.getCookieStore().add(null, cookie));
93-
usernameCookie = cookies.stream().findAny().filter(cookie -> cookie.getName().equals("username"));
94+
cookies.forEach(cookie -> cookieManager.getCookieStore()
95+
.add(null, cookie));
96+
usernameCookie = cookies.stream()
97+
.findAny()
98+
.filter(cookie -> cookie.getName()
99+
.equals("username"));
94100
}
95101

96102
if (usernameCookie == null) {
97-
cookieManager.getCookieStore().add(null, new HttpCookie("username", "john"));
103+
cookieManager.getCookieStore()
104+
.add(null, new HttpCookie("username", "john"));
98105
}
99106

100107
con.disconnect();
101108

102109
con = (HttpURLConnection) url.openConnection();
103-
con.setRequestProperty("Cookie", StringUtils.join(cookieManager.getCookieStore().getCookies(), ";"));
110+
con.setRequestProperty("Cookie", StringUtils.join(cookieManager.getCookieStore()
111+
.getCookies(), ";"));
104112

105113
int status = con.getResponseCode();
106114

@@ -125,4 +133,56 @@ public void whenRedirect_thenOk() throws IOException {
125133
assertEquals("status code incorrect", con.getResponseCode(), 200);
126134
}
127135

136+
@Test
137+
public void whenFailedRequest_thenOk() throws IOException {
138+
URL url = new URL("http://example.com");
139+
HttpURLConnection con = (HttpURLConnection) url.openConnection();
140+
con.setRequestMethod("POST");
141+
142+
con.setConnectTimeout(5000);
143+
con.setReadTimeout(5000);
144+
145+
int status = con.getResponseCode();
146+
147+
Reader streamReader = null;
148+
149+
if (status > 299) {
150+
streamReader = new InputStreamReader(con.getErrorStream());
151+
} else {
152+
streamReader = new InputStreamReader(con.getInputStream());
153+
}
154+
155+
BufferedReader in = new BufferedReader(streamReader);
156+
String inputLine;
157+
StringBuilder content = new StringBuilder();
158+
while ((inputLine = in.readLine()) != null) {
159+
content.append(inputLine);
160+
}
161+
in.close();
162+
163+
con.disconnect();
164+
165+
assertEquals("status code incorrect", status, 411);
166+
assertTrue("error content", content.toString()
167+
.contains("411 - Length Required"));
168+
}
169+
170+
@Test
171+
public void whenGetRequestFullResponse_thenOk() throws IOException {
172+
URL url = new URL("http://example.com");
173+
HttpURLConnection con = (HttpURLConnection) url.openConnection();
174+
con.setRequestMethod("GET");
175+
176+
con.setConnectTimeout(5000);
177+
con.setReadTimeout(5000);
178+
179+
String fullResponse = FullResponseBuilder.getFullResponse(con);
180+
181+
con.disconnect();
182+
183+
assertEquals("status code incorrect", con.getResponseCode(), 200);
184+
assertTrue("header incorrect", fullResponse.contains("Content-Type: text/html; charset=UTF-8"));
185+
assertTrue("response incorrect", fullResponse.contains("<!doctype html><html><head>"));
186+
}
187+
128188
}

0 commit comments

Comments
 (0)