-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HTTPRequestUtils with GET and String -> Json in Jsonutils
- Loading branch information
Showing
3 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/me/dkim19375/dkim19375core/external/HTTPRequestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package me.dkim19375.dkim19375core.external; | ||
|
||
import com.squareup.okhttp.HttpUrl; | ||
import com.squareup.okhttp.OkHttpClient; | ||
import com.squareup.okhttp.Request; | ||
import com.squareup.okhttp.Response; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class HTTPRequestUtils { | ||
private HTTPRequestUtils() {} | ||
|
||
private static final OkHttpClient client = new OkHttpClient(); | ||
|
||
public static String sendGET(String url, Map<String, String> headers, Map<String, String> params) throws IOException { | ||
return sendGET(new URL(url), headers, params); | ||
} | ||
|
||
public static String sendGET(URL url, Map<String, String> headers, Map<String, String> params) throws IOException { | ||
final HttpUrl.Builder urlBuilder = Objects.requireNonNull(HttpUrl.parse(url.toString())).newBuilder(); | ||
for (Map.Entry<String, String> key : params.entrySet()) { | ||
urlBuilder.addQueryParameter(key.getKey(), key.getValue()); | ||
} | ||
final String newUrl = urlBuilder.build().toString(); | ||
final Request.Builder builder = new Request.Builder().url(newUrl).get(); | ||
|
||
for (Map.Entry<String, String> key : headers.entrySet()) { | ||
builder.addHeader(key.getKey(), key.getValue()); | ||
} | ||
final Response response = client.newCall(builder.build()).execute(); | ||
return response.body() == null ? null : response.body().string(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters