Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf5b1a95

Browse files
committed
iluwatar#5 Add simple http get snippet
1 parentbb6ee6d commitf5b1a95

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

‎README.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ Update the sample application with the snippet and add a test for it. After prov
3131
###Media
3232
*[Capture screen](#capture-screen)
3333

34+
###Networking
35+
*[HTTP GET](#http-get)
36+
3437
###String
3538
*[Palindrome check](#palindrome-check)
3639
*[Reverse string](#reverse-string)
@@ -239,6 +242,19 @@ Update the sample application with the snippet and add a test for it. After prov
239242

240243
[⬆ back to top](#table-of-contents)
241244

245+
##Networking
246+
247+
###HTTP GET
248+
249+
```java
250+
publicstaticint httpGet(URL address) throwsIOException {
251+
HttpURLConnection con= (HttpURLConnection) address.openConnection();
252+
return con.getResponseCode();
253+
}
254+
```
255+
256+
[⬆ back to top](#table-of-contents)
257+
242258
##String
243259

244260
###Palindrome check

‎src/main/java/Library.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
importjava.awt.*;
2626
importjava.awt.image.BufferedImage;
2727
importjava.io.*;
28+
importjava.net.HttpURLConnection;
29+
importjava.net.URL;
30+
importjava.net.URLConnection;
2831
importjava.nio.file.Files;
2932
importjava.text.ParseException;
3033
importjava.text.SimpleDateFormat;
@@ -272,4 +275,15 @@ public static void quickSort(int[] arr, int left, int right) {
272275
}
273276
}
274277
}
278+
279+
/**
280+
* Performs HTTP GET request
281+
* @param address the URL of the connection
282+
* @return HTTP status code
283+
* @throws IOException
284+
*/
285+
publicstaticinthttpGet(URLaddress)throwsIOException {
286+
HttpURLConnectioncon = (HttpURLConnection)address.openConnection();
287+
returncon.getResponseCode();
288+
}
275289
}

‎src/test/java/LibraryTest.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
importjava.awt.*;
2727
importjava.io.File;
2828
importjava.io.IOException;
29+
importjava.net.MalformedURLException;
30+
importjava.net.URL;
2931
importjava.nio.file.Files;
3032
importjava.nio.file.Paths;
3133
importjava.text.ParseException;
@@ -251,4 +253,13 @@ public void testQuickSort() {
251253
assertEquals(arr[4],8);
252254
assertEquals(arr[5],13);
253255
}
256+
257+
/**
258+
* Tests for {@link Library#httpGet(URL)}
259+
*/
260+
@Test
261+
publicvoidtestHttpGet()throwsIOException {
262+
intresponseCode =Library.httpGet(newURL("http://www.google.com"));
263+
assertEquals(200,responseCode);
264+
}
254265
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp