Class WebRequest
java.lang.Object
fr.hytale.loader.utils.WebRequest
Utility class for making HTTP requests.
Provides simple methods for GET and POST requests with support for headers and timeouts.
Usage Examples:
// Synchronous GET request
WebRequest request = new WebRequest("https://example.com/data");
String response = request.get();
// Asynchronous GET request
request.getAsync().thenAccept(response -> {
System.out.println("Response: " + response);
});
- Since:
- 1.0.6
- Version:
- 1.0.6
- Author:
- HytaleLoader
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionclient(HttpClient client) Sets a custom HttpClient.delete()Performs a synchronous DELETE request.Performs an asynchronous DELETE request.get()Performs a synchronous GET request.getAsync()Performs an asynchronous GET request.Makes a GET request and returns the full HttpResponse.Adds a header to the request.Performs a synchronous POST request with a body.Performs an asynchronous POST request with a body.Performs a synchronous PUT request with a body.Performs an asynchronous PUT request with a body.static StringMakes a simple GET request to a URL.static StringsimplePost(String url, String jsonBody) Makes a simple POST request to a URL with JSON body.timeout(int seconds) Sets the request timeout in seconds.
-
Constructor Details
-
WebRequest
Creates a new WebRequest for the specified URL.- Parameters:
url- the target URL
-
-
Method Details
-
header
Adds a header to the request.- Parameters:
name- the header namevalue- the header value- Returns:
- this WebRequest for chaining
-
timeout
Sets the request timeout in seconds.- Parameters:
seconds- the timeout in seconds- Returns:
- this WebRequest for chaining
-
client
Sets a custom HttpClient.- Parameters:
client- the HttpClient to use- Returns:
- this WebRequest for chaining
-
get
Performs a synchronous GET request.- Returns:
- the response body as a String
- Throws:
IOException- if an I/O error occursInterruptedException- if the operation is interrupted
-
getAsync
Performs an asynchronous GET request.- Returns:
- a CompletableFuture that will contain the response body
-
post
Performs a synchronous POST request with a body.- Parameters:
body- the request body- Returns:
- the response body as a String
- Throws:
IOException- if an I/O error occursInterruptedException- if the operation is interrupted
-
postAsync
Performs an asynchronous POST request with a body.- Parameters:
body- the request body- Returns:
- a CompletableFuture that will contain the response body
-
put
Performs a synchronous PUT request with a body.- Parameters:
body- the request body- Returns:
- the response body as a String
- Throws:
IOException- if an I/O error occursInterruptedException- if the operation is interrupted
-
putAsync
Performs an asynchronous PUT request with a body.- Parameters:
body- the request body- Returns:
- a CompletableFuture that will contain the response body
-
delete
Performs a synchronous DELETE request.- Returns:
- the response body as a String
- Throws:
IOException- if an I/O error occursInterruptedException- if the operation is interrupted
-
deleteAsync
Performs an asynchronous DELETE request.- Returns:
- a CompletableFuture that will contain the response body
-
getResponse
Makes a GET request and returns the full HttpResponse.- Returns:
- the HttpResponse
- Throws:
IOException- if an I/O error occursInterruptedException- if the operation is interrupted
-
simpleGet
Makes a simple GET request to a URL.- Parameters:
url- the URL to request- Returns:
- the response body
- Throws:
IOException- if an I/O error occursInterruptedException- if the operation is interrupted
-
simplePost
public static String simplePost(String url, String jsonBody) throws IOException, InterruptedException Makes a simple POST request to a URL with JSON body.- Parameters:
url- the URL to requestjsonBody- the JSON body- Returns:
- the response body
- Throws:
IOException- if an I/O error occursInterruptedException- if the operation is interrupted
-