Skip to content

Commit 583f89e

Browse files
Initial Commit v0.0.1
0 parents  commit 583f89e

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.idea/
2+
/target/

pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.github.lightlibs</groupId>
8+
<artifactId>SimpleHttpWrapper</artifactId>
9+
<version>0.0.1</version>
10+
11+
<build>
12+
<plugins>
13+
<plugin>
14+
<groupId>org.apache.maven.plugins</groupId>
15+
<artifactId>maven-compiler-plugin</artifactId>
16+
<configuration>
17+
<source>16</source>
18+
<target>16</target>
19+
</configuration>
20+
</plugin>
21+
</plugins>
22+
</build>
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.jetbrains</groupId>
26+
<artifactId>annotations</artifactId>
27+
<version>23.0.0</version>
28+
</dependency>
29+
</dependencies>
30+
31+
</project>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.github.lightlibs.simplehttpwrapper;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
import java.io.*;
7+
import java.net.HttpURLConnection;
8+
import java.net.URL;
9+
import java.nio.charset.StandardCharsets;
10+
11+
public class SimpleHttpWrapper {
12+
13+
public static SimpleHttpResponse get(@NotNull String urlStr, @Nullable String[] headers) throws IOException {
14+
return performRequest(SupportedHttpMethod.GET, urlStr, headers, null);
15+
}
16+
17+
public static SimpleHttpResponse post(@NotNull String urlStr, @Nullable String[] headers, @Nullable String postData) throws IOException {
18+
return performRequest(SupportedHttpMethod.POST, urlStr, headers, postData);
19+
}
20+
21+
public static SimpleHttpResponse performRequest(@NotNull SupportedHttpMethod method, String urlStr, @Nullable String[] headers, @Nullable String postData) throws IOException {
22+
return httpRequest(method.getRawMethodName(), urlStr, headers, postData);
23+
}
24+
25+
@Deprecated
26+
public static SimpleHttpResponse performRequestUnsupported(@NotNull String method, String urlStr, @Nullable String[] headers, @Nullable String postData) throws IOException {
27+
return httpRequest(method, urlStr, headers, postData);
28+
}
29+
30+
@NotNull
31+
private static SimpleHttpResponse httpRequest(@NotNull String method, String urlStr, @Nullable String[] headers, @Nullable String postData) throws IOException {
32+
URL url = new URL(urlStr);
33+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
34+
connection.setRequestMethod(method);
35+
36+
if (headers != null) {
37+
for (String headerEntry : headers) {
38+
String[] parts = headerEntry.split(":");
39+
if (parts.length != 2) continue;
40+
41+
String key = parts[0];
42+
String value = parts[1];
43+
44+
connection.setRequestProperty(key, value);
45+
}
46+
}
47+
48+
if (method.equals("POST") && postData != null) {
49+
try (OutputStream os = connection.getOutputStream()) {
50+
byte[] postDataBytes = postData.getBytes(StandardCharsets.UTF_8);
51+
os.write(postDataBytes, 0, postDataBytes.length);
52+
}
53+
}
54+
55+
int responseCode = connection.getResponseCode();
56+
57+
InputStream inputStream;
58+
if (responseCode >= 200 && responseCode < 300) inputStream = connection.getInputStream();
59+
else inputStream = connection.getErrorStream();
60+
61+
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
62+
StringBuilder strb = new StringBuilder();
63+
while (true) {
64+
String line = reader.readLine();
65+
if (line == null) break;
66+
strb.append(line);
67+
}
68+
reader.close();
69+
connection.disconnect();
70+
71+
return new SimpleHttpResponse(responseCode, strb.toString());
72+
}
73+
74+
public record SimpleHttpResponse(int statusCode, String data) {}
75+
76+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.github.lightlibs.simplehttpwrapper;
2+
3+
public enum SupportedHttpMethod {
4+
5+
GET("GET"),
6+
POST("POST");
7+
8+
private final String rawMethodName;
9+
10+
SupportedHttpMethod(String rawMethodNameIn) {
11+
this.rawMethodName = rawMethodNameIn;
12+
}
13+
14+
public String getRawMethodName() {
15+
return rawMethodName;
16+
}
17+
}

0 commit comments

Comments
 (0)