Skip to content

Instantly share code, notes, and snippets.

@Shilo
Last active June 25, 2022 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shilo/4608d9f4b8a2775d7c4afb182e0d25f9 to your computer and use it in GitHub Desktop.
Save Shilo/4608d9f4b8a2775d7c4afb182e0d25f9 to your computer and use it in GitHub Desktop.
Java helper class for handling HTTP GET requests. (Requires Java 11+)
public class App {
public static void main(String[] args) throws Exception {
String ip = "www.google.com";
int port = 80;
boolean isSecure = true;
String url = HttpClientManager.addressToUrl(ip, port, isSecure);
exampleSendSync(url, isSecure);
exampleSendAsync(url, isSecure);
}
static void exampleSendSync(String url, boolean isSecure) {
HttpClientManagerResult result = HttpClientManager.send(url);
onExampleSendFinished(result);
}
static void exampleSendAsync(String url, boolean isSecure) {
HttpClientManager.sendAsync(url,
(result) -> onExampleSendFinished(result));
}
static void onExampleSendFinished(HttpClientManagerResult result) {
if (result.exception != null) {
System.out.println(result.exception);
// todo: handle error here
return;
}
var body = result.response.body();
System.out.println(body);
// todo: handle http body here
}
}
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.net.http.HttpClient;
import java.net.URI;
public class HttpClientManager {
public static Future<?> sendAsync(String url) {
return sendAsync(url, null);
}
public static Future<?> sendAsync(String url, Consumer<HttpClientManagerResult> onFinished) {
var executor = Executors.newSingleThreadExecutor();
return executor.submit(
() -> {
var result = send(url);
if (onFinished != null)
onFinished.accept(result);
executor.shutdown();
});
}
public static HttpClientManagerResult send(String url) {
try {
var request = HttpRequest.newBuilder(new URI(url)).build();
HttpClient client = HttpClient.newHttpClient();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
return new HttpClientManagerResult(response);
} catch (Exception exception) {
return new HttpClientManagerResult(exception);
}
}
public static String addressToUrl(String ip) {
return addressToUrl(ip, 80);
}
public static String addressToUrl(String ip, int port) {
return addressToUrl(ip, port, true);
}
public static String addressToUrl(String ip, int port, boolean isSecure) {
var scheme = isSecure ? "https" : "http";
var url = scheme + "://" + ip;
if (port != 80)
url += ":" + port;
return url;
}
}
import java.net.http.HttpResponse;
public class HttpClientManagerResult {
public HttpResponse<String> response;
public Exception exception;
public HttpClientManagerResult(HttpResponse<String> response) {
this.response = response;
this.exception = null;
}
public HttpClientManagerResult(Exception exception) {
this.response = null;
this.exception = exception;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment