OkHttp Java: Uproszczone żądania HTTP
In modern Java development, efficient handling of HTTP requests is crucial for building robust applications, especially those that rely on web services and APIs. OkHttp, a powerful HTTP & HTTP/2 client for Java and Kotlin, has become a popular choice due to its performance, ease of use, and advanced features.
This article provides a comprehensive guide to OkHttp, covering its key features, installation, and common use cases.
What is OkHttp?
OkHttp is a versatile open-source Java library for handling HTTP requests, offering a comprehensive set of features for seamless integration into your applications. With its intuitive API, creating a new request or executing a simple POST request is as easy as configuring a new request builder with query parameters and a string URL.
Additionally, OkHttp facilitates efficient response handling, providing access to the response body, response headers, and even supporting response caching to optimize network traffic and reduce server availability problems. Whether you're making synchronous or asynchronous calls, OkHttp's connection pooling ensures optimal performance, even when dealing with multiple IP addresses.

For developers accustomed to using Apache HTTP Client, OkHttp offers a more modern and efficient alternative, with improved performance and flexibility. Its support for asynchronous calls and callbacks makes it a preferred choice for applications requiring responsiveness and scalability.
With OkHttp, managing many HTTP clients and requests becomes effortless, empowering developers to focus on building robust and reliable applications without compromising on performance or functionality.
Najważniejsze cechy
Key features of OkHttp include:
- Synchronous and asynchronous request handling: OkHttp allows for both synchronous (blocking) and asynchronous (non-blocking) operations.
- Connection pooling: Reuses HTTP connections to minimize client connectivity problems and improve performance.
- Transparent GZIP compression: Reduces the size of HTTP responses, saving bandwidth and speeding up data transfer.
- Caching: Supports response caching, reducing the need for repeated network requests.
- HTTP/2 support: Enhances performance by allowing multiple requests and responses to be multiplexed over a single connection.
- Timeouts and retries: Offers fine-grained control over connection and read timeouts, as well as retry mechanisms for failed requests.
Installing OkHttp
To start using OkHttp in your Java project, you need to include its dependency in your build configuration. If you're using Maven, add the following dependency to your pom.xml file:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>5.0.0-alpha.14</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>5.0.0-alpha.14</version>
</dependency>
For Gradle, add this line to your build.gradle file:
implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.14'
Make sure to check for the latest version on Maven Central or GitHub.
Podstawowe zastosowanie
Creating an OkHttpClient
The OkHttpClient class is the main entry point for executing HTTP requests. It is recommended to create a single OkHttpClient instance and reuse it throughout your application to take advantage of connection pooling.
import okhttp3.OkHttpClient;
OkHttpClient client = new OkHttpClient();
import okhttp3.OkHttpClient;
OkHttpClient client = new OkHttpClient();
Making GET Requests
To make a simple GET request, you need to create a Request object and execute it using the OkHttpClient.
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
public class OkHttpExample {
public static void main(String[] args) {
OkHttpClient client = new OkHttpClient();
// Create a request specifying the URL
Request request = new Request.Builder()
.url("https://jsonplaceholder.typicode.com/posts/1")
.build();
// Execute the request and handle the response
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) { // Check if the response was successful
System.out.println(response.body().string()); // Print the response body
} else {
System.err.println("Request failed: " + response.code()); // Print error code
}
} catch (IOException e) {
e.printStackTrace(); // Handle exceptions
}
}
}
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
public class OkHttpExample {
public static void main(String[] args) {
OkHttpClient client = new OkHttpClient();
// Create a request specifying the URL
Request request = new Request.Builder()
.url("https://jsonplaceholder.typicode.com/posts/1")
.build();
// Execute the request and handle the response
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) { // Check if the response was successful
System.out.println(response.body().string()); // Print the response body
} else {
System.err.println("Request failed: " + response.code()); // Print error code
}
} catch (IOException e) {
e.printStackTrace(); // Handle exceptions
}
}
}

Making POST Requests
For a POST request, you need to include a request body and return response. OkHttp provides the RequestBody class to handle this.
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import java.io.IOException;
public class OkHttpExample {
// Define the JSON media type
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
public static void main(String[] args) {
OkHttpClient client = new OkHttpClient();
// JSON data to be sent
String json = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}";
// Create request body with JSON data
RequestBody body = RequestBody.create(json, JSON);
// Build the POST request
Request request = new Request.Builder()
.url("https://jsonplaceholder.typicode.com/posts")
.post(body)
.build();
// Execute the request and handle the response
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) { // Check if the response was successful
System.out.println(response.body().string()); // Print the response body
} else {
System.err.println("Request failed: " + response.code()); // Print error code
}
} catch (IOException e) {
e.printStackTrace(); // Handle exceptions
}
}
}
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import java.io.IOException;
public class OkHttpExample {
// Define the JSON media type
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
public static void main(String[] args) {
OkHttpClient client = new OkHttpClient();
// JSON data to be sent
String json = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}";
// Create request body with JSON data
RequestBody body = RequestBody.create(json, JSON);
// Build the POST request
Request request = new Request.Builder()
.url("https://jsonplaceholder.typicode.com/posts")
.post(body)
.build();
// Execute the request and handle the response
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) { // Check if the response was successful
System.out.println(response.body().string()); // Print the response body
} else {
System.err.println("Request failed: " + response.code()); // Print error code
}
} catch (IOException e) {
e.printStackTrace(); // Handle exceptions
}
}
}

Asynchronous Requests
Asynchronous requests are handled using callbacks, allowing your application to remain responsive while waiting for the response.
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
public class OkHttpExample {
public static void main(String[] args) {
OkHttpClient client = new OkHttpClient();
// Create a request specifying the URL
Request request = new Request.Builder()
.url("https://jsonplaceholder.typicode.com/posts/1")
.build();
// Enqueue the request to be executed asynchronously
client.newCall(request).enqueue(new Callback() {
// Handle failure of the request
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace(); // Handle exceptions
}
// Handle successful response
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) { // Check if the response was successful
System.out.println(response.body().string()); // Print the response body
} else {
System.err.println("Request failed: " + response.code()); // Print error code
}
}
});
}
}
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
public class OkHttpExample {
public static void main(String[] args) {
OkHttpClient client = new OkHttpClient();
// Create a request specifying the URL
Request request = new Request.Builder()
.url("https://jsonplaceholder.typicode.com/posts/1")
.build();
// Enqueue the request to be executed asynchronously
client.newCall(request).enqueue(new Callback() {
// Handle failure of the request
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace(); // Handle exceptions
}
// Handle successful response
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) { // Check if the response was successful
System.out.println(response.body().string()); // Print the response body
} else {
System.err.println("Request failed: " + response.code()); // Print error code
}
}
});
}
}
Advanced Features
Interceptors
Interceptors are a powerful feature that allows you to inspect, modify, or retry requests and responses. They can be used for logging, adding headers, or handling authentication.
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
public class OkHttpExample {
public static void main(String[] args) {
// Add an interceptor for modifying requests
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
// Modify the request to add the authorization header
Request request = chain.request().newBuilder()
.addHeader("Authorization", "Bearer your_token_here")
.build();
return chain.proceed(request);
}
})
.build();
// Create a request specifying the URL
Request request = new Request.Builder()
.url("https://jsonplaceholder.typicode.com/posts/1")
.build();
// Execute the request and handle the response
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) { // Check if the response was successful
System.out.println(response.body().string()); // Print the response body
} else {
System.err.println("Request failed: " + response.code()); // Print error code
}
} catch (IOException e) {
e.printStackTrace(); // Handle exceptions
}
}
}
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
public class OkHttpExample {
public static void main(String[] args) {
// Add an interceptor for modifying requests
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
// Modify the request to add the authorization header
Request request = chain.request().newBuilder()
.addHeader("Authorization", "Bearer your_token_here")
.build();
return chain.proceed(request);
}
})
.build();
// Create a request specifying the URL
Request request = new Request.Builder()
.url("https://jsonplaceholder.typicode.com/posts/1")
.build();
// Execute the request and handle the response
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) { // Check if the response was successful
System.out.println(response.body().string()); // Print the response body
} else {
System.err.println("Request failed: " + response.code()); // Print error code
}
} catch (IOException e) {
e.printStackTrace(); // Handle exceptions
}
}
}
Handling Timeouts
OkHttp provides methods to set timeouts for different stages of the HTTP request itself.
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class OkHttpExample {
public static void main(String[] args) {
// Configure timeouts for connections, writes, and reads
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
// Create a request specifying the URL
Request request = new Request.Builder()
.url("https://jsonplaceholder.typicode.com/posts/1")
.build();
// Execute the request and handle the response
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) { // Check if the response was successful
System.out.println(response.body().string()); // Print the response body
} else {
System.err.println("Request failed: " + response.code()); // Print error code
}
} catch (IOException e) {
e.printStackTrace(); // Handle exceptions
}
}
}
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class OkHttpExample {
public static void main(String[] args) {
// Configure timeouts for connections, writes, and reads
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
// Create a request specifying the URL
Request request = new Request.Builder()
.url("https://jsonplaceholder.typicode.com/posts/1")
.build();
// Execute the request and handle the response
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) { // Check if the response was successful
System.out.println(response.body().string()); // Print the response body
} else {
System.err.println("Request failed: " + response.code()); // Print error code
}
} catch (IOException e) {
e.printStackTrace(); // Handle exceptions
}
}
}
Caching Responses
OkHttp can cache responses to reduce request latency and improve performance. Wymaga to skonfigurowania katalogu pamięci podręcznej i jej rozmiaru.
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.File;
import java.io.IOException;
public class OkHttpExample {
public static void main(String[] args) {
// Define the cache directory and size
File cacheDirectory = new File("cacheDirectory");
Cache cache = new Cache(cacheDirectory, 10 * 1024 * 1024); // 10 MB cache
// Build OkHttpClient with caching capability
OkHttpClient client = new OkHttpClient.Builder()
.cache(cache)
.build();
// Create a request specifying the URL
Request request = new Request.Builder()
.url("https://jsonplaceholder.typicode.com/posts/1")
.build();
// Execute the request and handle the response
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) { // Check if the response was successful
System.out.println(response.body().string()); // Print the response body
} else {
System.err.println("Request failed: " + response.code()); // Print error code
}
} catch (IOException e) {
e.printStackTrace(); // Handle exceptions
}
}
}
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.File;
import java.io.IOException;
public class OkHttpExample {
public static void main(String[] args) {
// Define the cache directory and size
File cacheDirectory = new File("cacheDirectory");
Cache cache = new Cache(cacheDirectory, 10 * 1024 * 1024); // 10 MB cache
// Build OkHttpClient with caching capability
OkHttpClient client = new OkHttpClient.Builder()
.cache(cache)
.build();
// Create a request specifying the URL
Request request = new Request.Builder()
.url("https://jsonplaceholder.typicode.com/posts/1")
.build();
// Execute the request and handle the response
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) { // Check if the response was successful
System.out.println(response.body().string()); // Print the response body
} else {
System.err.println("Request failed: " + response.code()); // Print error code
}
} catch (IOException e) {
e.printStackTrace(); // Handle exceptions
}
}
}
Integracja OkHttp z IronPDF w Javie
Połączenie możliwości OkHttp i IronPDF pozwala programistom Java pobierać dane z sieci i konwertować je do formatu PDF. OkHttp to solidny klient HTTP do obsługi żądań sieciowych, natomiast IronPDF to potężna biblioteka do generowania plików PDF z różnych źródeł.
IronPDF — przegląd
IronPDF for Java to kompleksowa biblioteka zaprojektowana w celu uproszczenia generowania plików PDF w aplikacjach Java. Korzystając z intuicyjnego interfejsu API, programiści mogą bez wysiłku tworzyć, modyfikować i renderować dokumenty PDF z różnych źródeł danych, w tym HTML, obrazów i tekstu.
Dzięki obsłudze zaawansowanych funkcji, takich jak szyfrowanie plików PDF, podpisy cyfrowe i interaktywne wypełnianie formularzy, IronPDF umożliwia programistom tworzenie profesjonalnych plików PDF dostosowanych do ich konkretnych wymagań. Płynna integracja i obszerna dokumentacja sprawiają, że jest to idealne rozwiązanie dla programistów Java, którzy chcą wzbogacić swoje aplikacje o solidne funkcje generowania plików PDF.

Konfigurowanie zależności
First, add the necessary dependencies to your pom.xml (for Maven) file or build.gradle (for Gradle) file.
Maven
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>2024.3.1</version>
</dependency>
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>2024.3.1</version>
</dependency>
Gradle
implementation 'com.ironsoftware:ironpdf:2024.3.1'
Integracja OkHttp i IronPDF
Teraz połączmy te dwie funkcje: pobieranie treści HTML za pomocą OkHttp i generowanie pliku PDF za pomocą IronPDF.
import com.ironsoftware.ironpdf.PdfDocument;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
import java.nio.file.Paths;
public class OkHttpToPdf {
private final OkHttpClient client = new OkHttpClient(); // Initialize the OkHttpClient
// Method to fetch HTML content from a given URL
public String fetchHtml(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
// Execute the request and return the response body as a string
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
return response.body().string();
}
}
// Method to generate a PDF from a URL
public void generatePdfFromUrl(String url, String outputFilePath) {
try {
String htmlContent = fetchHtml(url); // Fetch the HTML content
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlContent); // Render HTML as PDF
pdf.saveAs(Paths.get(outputFilePath)); // Save the PDF to the specified path
System.out.println("PDF generated successfully at " + outputFilePath);
} catch (IOException e) {
System.err.println("Failed to fetch HTML content: " + e.getMessage());
} catch (Exception e) {
System.err.println("Failed to generate PDF: " + e.getMessage());
}
}
// Main method to demonstrate fetching HTML and generating a PDF
public static void main(String[] args) {
OkHttpToPdf converter = new OkHttpToPdf(); // Create an instance of OkHttpToPdf
converter.generatePdfFromUrl("https://ironpdf.com/java", "website.pdf"); // Fetch HTML and generate PDF
}
}
import com.ironsoftware.ironpdf.PdfDocument;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
import java.nio.file.Paths;
public class OkHttpToPdf {
private final OkHttpClient client = new OkHttpClient(); // Initialize the OkHttpClient
// Method to fetch HTML content from a given URL
public String fetchHtml(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
// Execute the request and return the response body as a string
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
return response.body().string();
}
}
// Method to generate a PDF from a URL
public void generatePdfFromUrl(String url, String outputFilePath) {
try {
String htmlContent = fetchHtml(url); // Fetch the HTML content
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlContent); // Render HTML as PDF
pdf.saveAs(Paths.get(outputFilePath)); // Save the PDF to the specified path
System.out.println("PDF generated successfully at " + outputFilePath);
} catch (IOException e) {
System.err.println("Failed to fetch HTML content: " + e.getMessage());
} catch (Exception e) {
System.err.println("Failed to generate PDF: " + e.getMessage());
}
}
// Main method to demonstrate fetching HTML and generating a PDF
public static void main(String[] args) {
OkHttpToPdf converter = new OkHttpToPdf(); // Create an instance of OkHttpToPdf
converter.generatePdfFromUrl("https://ironpdf.com/java", "website.pdf"); // Fetch HTML and generate PDF
}
}
Wyjaśnienie kodu
Powyższy kod pokazuje, jak pobrać zawartość HTML z adresu URL i przekonwertować ją na plik PDF przy użyciu bibliotek OkHttp i IronPDF w Javie:
-
Instrukcje importu: Importowane są niezbędne biblioteki, w tym IronPDF do generowania plików PDF oraz OkHttp do żądań HTTP.
-
OkHttpClient Initialization: An instance of
OkHttpClientis created. -
fetchHtmlMethod: This method fetches HTML content from a specified URL.- Żądanie jest tworzone na podstawie podanego adresu URL.
- Żądanie zostaje wykonane i uzyskana zostaje odpowiedź.
- If the response is not successful, an
IOExceptionis thrown. - Treść odpowiedzi jest zwracana jako ciąg znaków.
-
generatePdfFromUrlMethod: This method generates a PDF from the HTML content of a specified URL and saves it to a given file path.- The HTML content is fetched using the
fetchHtmlmethod. - The HTML content is rendered as a PDF using
IronPDF. - Plik PDF jest zapisywany w określonej ścieżce.
- Odpowiednia obsługa błędów jest uwzględniona zarówno w przypadku pobierania HTML, jak i generowania plików PDF.
- The HTML content is fetched using the
-
mainMethod: This is the entry point of the program.- An instance of
OkHttpToPdfis created. - The
generatePdfFromUrlmethod is called with a specific URL and output file path.
- An instance of
Wynik
Dane z adresu URL są pobierane za pomocą klienta OkHttp, a następnie renderowane przy użyciu biblioteki IronPDF w celu wydajnej konwersji do formatu PDF, jak pokazano poniżej:

Aby uzyskać bardziej szczegółowe informacje na temat IronPDF, odwiedź tę stronę dokumentacji IronPDF. Aby uzyskać więcej informacji na temat wykorzystania IronPDF, prosimy również zapoznać się ze stroną Przykłady kodu IronPDF oraz Dokumentacja API IronPDF.
Wnioski
OkHttp to wszechstronny i wydajny klient HTTP dla Javy i Androida, który upraszcza proces wysyłania żądań sieciowych. Dzięki obsłudze operacji synchronicznych i asynchronicznych, puli połączeń, przezroczystej kompresji GZIP, buforowaniu i HTTP/2, klient OkHttp doskonale nadaje się do szerokiego zakresu zastosowań. Dzięki integracji OkHttp z aplikacjami Java można zwiększyć ich wydajność, niezawodność i efektywność.
Dzięki integracji OkHttp z IronPDF można efektywnie pobierać treści HTML ze źródeł internetowych i konwertować je na dokumenty PDF. Takie podejście jest szczególnie przydatne w przypadku aplikacji, które muszą generować raporty, zapisywać strony internetowe lub konwertować treści internetowe na dokumenty offline.
Wykorzystaj potencjał generowania plików PDF w swoich aplikacjach Java dzięki bezpłatnej wersji próbnej IronPDF, która umożliwia płynną integrację profesjonalnego generowania plików PDF z Twoimi projektami. Pobierz teraz i popraw jakość generowania plików PDF!





