Pruebe en producción sin marcas de agua.
Funciona donde lo necesite.
Obtén 30 días de producto totalmente funcional.
Ténlo en funcionamiento en minutos.
Acceso completo a nuestro equipo de asistencia técnica durante la prueba del producto
EnClase HttpClient.NET Framework, proporciona métodos para enviar solicitudes HTTP y recibir respuestas HTTP de un recurso identificado por un URI. Simplifica la realización de llamadas a peticiones HTTP, ya se trate de peticiones GET, POST, PUT o DELETE. Esta guía cubrirá el uso esencial de HttpClient en escenarios prácticos e introducirá elBiblioteca IronPDF.
La clase HttpClient se utiliza para enviar peticiones HTTP. Puede crear una nueva instancia de la siguiente manera:
using System;
using System.Net.Http;
class Program
{
static async Task Main(string[] args)
{
using var client = new HttpClient(); //HttpClient client
var response = await client.GetAsync("https://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q=London"); // HTTP response for GET request
var responseBody = await response.Content.ReadAsStringAsync(); // response content
Console.WriteLine(responseBody);
}
}
using System;
using System.Net.Http;
class Program
{
static async Task Main(string[] args)
{
using var client = new HttpClient(); //HttpClient client
var response = await client.GetAsync("https://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q=London"); // HTTP response for GET request
var responseBody = await response.Content.ReadAsStringAsync(); // response content
Console.WriteLine(responseBody);
}
}
En este ejemplo:
Realizar una solicitud HTTP GET y gestionar la respuesta:
var client = new HttpClient();
var response = await client.GetAsync("https://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q=Paris");
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
var client = new HttpClient();
var response = await client.GetAsync("https://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q=Paris");
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
Enviar una solicitud POST implica añadir un cuerpo de solicitud:
var client = new HttpClient();
var requestBody = new StringContent("{ \"location\": \"New York\" }", Encoding.UTF8, "application/json");
var response = await client.PostAsync("https://api.weatherapi.com/v1/forecast.json?key=YOUR_API_KEY", requestBody);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
var client = new HttpClient();
var requestBody = new StringContent("{ \"location\": \"New York\" }", Encoding.UTF8, "application/json");
var response = await client.PostAsync("https://api.weatherapi.com/v1/forecast.json?key=YOUR_API_KEY", requestBody);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
Una solicitud HTTP PUT actualiza los recursos:
var client = new HttpClient();
var requestBody = new StringContent("{ \"location\": \"Tokyo\", \"days\": 3 }", Encoding.UTF8, "application/json");
var response = await client.PutAsync("https://api.weatherapi.com/v1/forecast.json?key=YOUR_API_KEY", requestBody);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
var client = new HttpClient();
var requestBody = new StringContent("{ \"location\": \"Tokyo\", \"days\": 3 }", Encoding.UTF8, "application/json");
var response = await client.PutAsync("https://api.weatherapi.com/v1/forecast.json?key=YOUR_API_KEY", requestBody);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
Para enviar una solicitud HTTP DELETE:
var client = new HttpClient();
var response = await client.DeleteAsync("https://api.weatherapi.com/v1/locations/1?key=YOUR_API_KEY");
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Resource deleted successfully");
}
var client = new HttpClient();
var response = await client.DeleteAsync("https://api.weatherapi.com/v1/locations/1?key=YOUR_API_KEY");
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Resource deleted successfully");
}
Cada solicitud HTTP devuelve un objeto HttpResponseMessage, que incluye el cuerpo de la respuesta, las cabeceras y el código de estado. Por ejemplo:
var response = await client.GetAsync("https://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q=Sydney");
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine($"Error: {response.StatusCode}");
}
var response = await client.GetAsync("https://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q=Sydney");
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine($"Error: {response.StatusCode}");
}
Las instancias de HttpClient deben reutilizarse para aprovechar la agrupación de conexiones y evitar agotar los recursos del sistema. Un patrón típico es crear una única instancia de HttpClient para toda la vida de su aplicación o servicio. Esto puede hacerse utilizando una variable estática o inyección de dependencia para aplicaciones web.
public static class HttpClientProvider
{
private static readonly HttpClient client = new HttpClient();
public static HttpClient Client => client;
}
public static class HttpClientProvider
{
private static readonly HttpClient client = new HttpClient();
public static HttpClient Client => client;
}
La instancia HttpClient se reutiliza en toda la aplicación, lo que reduce la sobrecarga de crear nuevas conexiones HTTP.
En una aplicación web, el enfoque recomendado es registrar HttpClient como un servicio singleton:
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
}
También puede crear clientes con nombre y clientes tipificados para configuraciones más específicas.
Al reutilizar las instancias de HttpClient, se beneficia de la agrupación de conexiones, que mejora el rendimiento de varias solicitudes al mismo servidor. También puede configurar los ajustes del proxy utilizando la clase HttpClientHandler:
var handler = new HttpClientHandler
{
Proxy = new WebProxy("http://proxyserver:port"),
UseProxy = true
};
var client = new HttpClient(handler);
var handler = new HttpClientHandler
{
Proxy = new WebProxy("http://proxyserver:port"),
UseProxy = true
};
var client = new HttpClient(handler);
Para manejar diferentes códigos de estado HTTP, compruebe la propiedad HttpResponseMessage.StatusCode:
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
async Task MakeRequestAsync()
{
try
{
using var client = new HttpClient();
var response = await client.GetAsync("https://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q=Berlin");
// Response Status Code Cases
switch (response.StatusCode)
{
case HttpStatusCode.OK:
Console.WriteLine("Success");
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Response content: {content}");
break;
case HttpStatusCode.NotFound:
Console.WriteLine("Resource not found");
break;
case HttpStatusCode.Unauthorized:
Console.WriteLine("Unauthorized access");
break;
case HttpStatusCode.InternalServerError:
Console.WriteLine("Server error occurred");
break;
default:
Console.WriteLine($"Unexpected status code: {response.StatusCode}");
break;
}
}
catch (HttpRequestException e)
{
Console.WriteLine($"Request error: {e.Message}");
}
catch (Exception e)
{
Console.WriteLine($"An error occurred: {e.Message}");
}
}
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
async Task MakeRequestAsync()
{
try
{
using var client = new HttpClient();
var response = await client.GetAsync("https://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q=Berlin");
// Response Status Code Cases
switch (response.StatusCode)
{
case HttpStatusCode.OK:
Console.WriteLine("Success");
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Response content: {content}");
break;
case HttpStatusCode.NotFound:
Console.WriteLine("Resource not found");
break;
case HttpStatusCode.Unauthorized:
Console.WriteLine("Unauthorized access");
break;
case HttpStatusCode.InternalServerError:
Console.WriteLine("Server error occurred");
break;
default:
Console.WriteLine($"Unexpected status code: {response.StatusCode}");
break;
}
}
catch (HttpRequestException e)
{
Console.WriteLine($"Request error: {e.Message}");
}
catch (Exception e)
{
Console.WriteLine($"An error occurred: {e.Message}");
}
}
A menudo se trabaja con respuestas JSON. Puede deserializar el contenido de la respuesta en un objeto fuertemente tipado:
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
var client = new HttpClient();
var response = await client.GetAsync("https://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q=London");
var jsonString = await response.Content.ReadAsStringAsync();
var weatherResponse = JsonSerializer.Deserialize<WeatherResponse>(jsonString);
public class WeatherResponse
{
public string Location { get; set; }
public double Temperature { get; set; }
}
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
var client = new HttpClient();
var response = await client.GetAsync("https://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q=London");
var jsonString = await response.Content.ReadAsStringAsync();
var weatherResponse = JsonSerializer.Deserialize<WeatherResponse>(jsonString);
public class WeatherResponse
{
public string Location { get; set; }
public double Temperature { get; set; }
}
El método ReadAsStringAsync simplifica la lectura de contenido JSON directamente en objetos C#.
IronPDF es una biblioteca PDF .NET diseñada para crear, manipular y convertir archivos PDF en C#. Se utiliza ampliamente paragenerar PDF de alta calidad a partir de HTMLel texto debe estar en formato .NET, CSS, JavaScript y otros formatos. IronPDF ofrece funciones como conversión de HTML a PDF, fusión de PDF, marcas de agua e incluso operaciones avanzadas como firmas digitales y cifrado de PDF. Es compatible con varias plataformas, como Windows, Linux y macOS, lo que lo convierte en una solución versátil para el desarrollo multiplataforma.
La combinación de IronPDF con la clase HttpClient en C# es una forma eficaz de generar y manipular documentos PDF a partir de recursos web de forma dinámica. Por ejemplo, puede recuperar contenido HTML de una URL mediante HttpClient y, a continuación, convertir este HTML en un documento PDF utilizando IronPDF. Esto es útil a la hora de generar informes, facturas o cualquier documento basado dinámicamente en contenido web en directo.
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Text.Json;
using IronPdf;
class Program
{
static async Task Main(string[] args)
{
License.LicenseKey = "IRONSUITE.WINSWRITERSIRONSOFTWARECOM.31102-3E7C9B3308-CIHWVP2-L2A7FVEBUSFN-DP6CI32YVXIN-45HQSAZOBOCW-YCYETGS4ENQX-NZXXG4YCPAYI-EBCIJ43PNLJW-DKWC7U-TFXPQ4LKLXGPEA-DEPLOYMENT.TRIAL-G43TTA.TRIAL.EXPIRES.20.MAY.2025";
using var client = new HttpClient();
var response = await client.GetAsync("https://api.weatherapi.com/v1/forecast.json?key=55c9040a6ad34c6c90470702240609&q=London&days=3");
if (response.IsSuccessStatusCode)
{
var jsonContent = await response.Content.ReadAsStringAsync();
// Pretty-print the JSON
var jsonElement = JsonSerializer.Deserialize<JsonElement>(jsonContent);
var formattedJson = JsonSerializer.Serialize(jsonElement, new JsonSerializerOptions { WriteIndented = true });
// Escape the JSON for HTML
formattedJson = System.Web.HttpUtility.HtmlEncode(formattedJson);
var htmlContent = $@"
<html>
<head>
<style>
body {{ font-family: Arial, sans-serif; }}
pre {{ background-color: #f4f4f4; padding: 20px; border-radius: 5px; white-space: pre-wrap; word-wrap: break-word; }}
</style>
</head>
<body>
<h1>Weather Forecast (JSON Data)</h1>
<pre>{formattedJson}</pre>
</body>
</html>";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("F://weather_report.pdf");
Console.WriteLine("PDF generated successfully!");
}
else
{
Console.WriteLine($"Failed to retrieve content. Status code: {response.StatusCode}");
}
}
}
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Text.Json;
using IronPdf;
class Program
{
static async Task Main(string[] args)
{
License.LicenseKey = "IRONSUITE.WINSWRITERSIRONSOFTWARECOM.31102-3E7C9B3308-CIHWVP2-L2A7FVEBUSFN-DP6CI32YVXIN-45HQSAZOBOCW-YCYETGS4ENQX-NZXXG4YCPAYI-EBCIJ43PNLJW-DKWC7U-TFXPQ4LKLXGPEA-DEPLOYMENT.TRIAL-G43TTA.TRIAL.EXPIRES.20.MAY.2025";
using var client = new HttpClient();
var response = await client.GetAsync("https://api.weatherapi.com/v1/forecast.json?key=55c9040a6ad34c6c90470702240609&q=London&days=3");
if (response.IsSuccessStatusCode)
{
var jsonContent = await response.Content.ReadAsStringAsync();
// Pretty-print the JSON
var jsonElement = JsonSerializer.Deserialize<JsonElement>(jsonContent);
var formattedJson = JsonSerializer.Serialize(jsonElement, new JsonSerializerOptions { WriteIndented = true });
// Escape the JSON for HTML
formattedJson = System.Web.HttpUtility.HtmlEncode(formattedJson);
var htmlContent = $@"
<html>
<head>
<style>
body {{ font-family: Arial, sans-serif; }}
pre {{ background-color: #f4f4f4; padding: 20px; border-radius: 5px; white-space: pre-wrap; word-wrap: break-word; }}
</style>
</head>
<body>
<h1>Weather Forecast (JSON Data)</h1>
<pre>{formattedJson}</pre>
</body>
</html>";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("F://weather_report.pdf");
Console.WriteLine("PDF generated successfully!");
}
else
{
Console.WriteLine($"Failed to retrieve content. Status code: {response.StatusCode}");
}
}
}
Recuerde sustituir "YOUR_API_KEY" por una clave API cuando utilice una API meteorológica real.
Este tutorial explora el uso de la clase HttpClient en C# para enviar peticiones HTTP y manejar respuestas. También presentamos IronPDF, una potente biblioteca para generar archivos PDF en aplicaciones .NET. Demostramos cómo combinar estas tecnologías recuperando contenido HTML de un servicio web mediante HttpClient y convirtiéndolo a PDF con IronPDF.
IronPDF ofrece una prueba gratuita, y sus licencias comienzan desde $749, lo que lo convierte en una herramienta valiosa para los desarrolladores que buscan capacidades integrales de generación de PDF.