1. java.net (librería estándar)
El paquete java.net proporciona clases como URL, URI y HttpURLConnection. Permite trabajar con URLs, abrir conexiones HTTP y manejar recursos remotos.
Ejemplo:
import java.net.*;
import java.io.*;
public class URLExample {
public static void main(String[]
args) throws Exception {
URL url = new
URL("https://example.com");
BufferedReader in = new
BufferedReader(
new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine =
in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
2. Apache HttpClient
Una librería muy usada para realizar peticiones HTTP. Ofrece un manejo más flexible y completo que java.net, incluyendo autenticación, manejo de cookies y conexiones seguras.
Ejemplo:
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;
import org.apache.http.util.EntityUtils;
public class HttpClientExample {
public static void main(String[]
args) throws Exception {
CloseableHttpClient client =
HttpClients.createDefault();
HttpGet request = new
HttpGet("https://example.com");
CloseableHttpResponse response =
client.execute(request);
String result =
EntityUtils.toString(response.getEntity());
System.out.println(result);
client.close();
}
}
3. OkHttp (Square)
Una librería moderna y eficiente para peticiones HTTP. Es ligera, soporta HTTP/2, WebSockets y caché de respuestas. Muy usada en Android.
Ejemplo:
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OkHttpExample {
public static void main(String[]
args) throws Exception {
OkHttpClient client = new
OkHttpClient();
Request request = new
Request.Builder()
.url("https://example.com")
.build();
try (Response response =
client.newCall(request).execute()) {
System.out.println(response.body().string());
}
}
}
4. Jsoup
Aunque es principalmente para parseo de HTML, Jsoup permite conectarse a URLs, realizar peticiones GET/POST y extraer datos de páginas web de manera sencilla.
Ejemplo:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class JsoupExample {
public static void main(String[]
args) throws Exception {
Document doc =
Jsoup.connect("https://example.com").get();
System.out.println(doc.title());
}
}
5. Spring WebClient (Spring Framework)
Un cliente reactivo para realizar peticiones HTTP de manera asíncrona. Forma parte de Spring WebFlux y reemplaza al antiguo RestTemplate.
Ejemplo:
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
public class WebClientExample {
public static void main(String[]
args) {
WebClient client =
WebClient.create("https://example.com");
Mono<String> result =
client.get()
.retrieve()
.bodyToMono(String.class);
System.out.println(result.block());
}
}
6. Unirest (Kong)
Un cliente HTTP ligero y fácil de usar. Soporta JSON y se integra con librerías de serialización como Jackson o Gson.
Ejemplo:
import kong.unirest.Unirest;
public class UnirestExample {
public static void main(String[]
args) {
String response =
Unirest.get("https://example.com")
.asString()
.getBody();
System.out.println(response);
}
}
7. Retrofit (Square)
Librería que simplifica la creación de clientes HTTP mediante interfaces. Muy usada en aplicaciones Android en conjunto con OkHttp y Gson.
Ejemplo:
import retrofit2.*;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
interface ApiService {
@GET("/")
Call<String> getHome();
}
public class RetrofitExample {
public static void main(String[]
args) throws Exception {
Retrofit retrofit = new
Retrofit.Builder()
.baseUrl("https://example.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiService service =
retrofit.create(ApiService.class);
Call<String> call =
service.getHome();
System.out.println(call.execute().body());
}
}
8. Jetty Client
Parte del proyecto Eclipse Jetty, ofrece un cliente HTTP asíncrono con gran rendimiento. Útil en sistemas que requieren alta concurrencia.
Ejemplo:
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
public class JettyClientExample {
public static void main(String[]
args) throws Exception {
HttpClient client = new
HttpClient();
client.start();
ContentResponse response =
client.GET("https://example.com");
System.out.println(response.getContentAsString());
client.stop();
}
}