了解IronPDF中的Kerberos
This article was translated from English: Does it need improvement?
Translated
View the article in English
如何讓 UrlToPdf 與Kerberos身份驗證搭配使用?
若要將Kerberos驗證與 IronPDF 的 UrlToPdf 一起使用,您應該在渲染設定中設定使用者名稱和密碼。 您可以參考IronPDF文件以了解更多詳情: IronPDF 。
我們建議使用.NET下載 HTML 內容,之後您可以對其進行渲染。 這種方法可讓您在IronPDF處理內容之前處理 HTTP 請求,包括需要驗證的請求。
以下是關於使用Kerberos下載頁面的線上指南: .NET如何選擇身份驗證類型? 此 StackOverflow 連結提供了有關使用 HttpClient 實現身份驗證的詳細討論。
為了解析並確保下載 HTML 中的所有必要資源,請考慮使用 HTML Agility Pack。 這個.NET函式庫有助於有效地操作和查詢 HTML 文件。
// Example: Using HttpClient with Kerberos Authentication
// Import the necessary namespaces
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace KerberosAuthenticationExample
{
class Program
{
static async Task Main(string[] args)
{
// Create an instance of HttpClient
HttpClientHandler handler = new HttpClientHandler
{
// Automatically use default network credentials
UseDefaultCredentials = true // Enables Windows authentication (e.g., Kerberos)
};
using HttpClient httpClient = new HttpClient(handler);
try
{
// Send a GET request to the desired URL
HttpResponseMessage response = await httpClient.GetAsync("https://your-secure-url.com");
// Ensure the request was successful
response.EnsureSuccessStatusCode();
// Read and display the response body
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
// If needed, render the HTML to PDF with IronPDF here
// IronPdf.HtmlToPdf renderer = new IronPdf.HtmlToPdf();
// renderer.RenderHtmlAsPdf(responseBody).SaveAs("output.pdf");
}
catch (HttpRequestException e)
{
// Handle any error responses from the server or connection issues
Console.WriteLine("\nException Caught!");
Console.WriteLine($"Message :{e.Message}");
}
}
}
}
// Example: Using HttpClient with Kerberos Authentication
// Import the necessary namespaces
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace KerberosAuthenticationExample
{
class Program
{
static async Task Main(string[] args)
{
// Create an instance of HttpClient
HttpClientHandler handler = new HttpClientHandler
{
// Automatically use default network credentials
UseDefaultCredentials = true // Enables Windows authentication (e.g., Kerberos)
};
using HttpClient httpClient = new HttpClient(handler);
try
{
// Send a GET request to the desired URL
HttpResponseMessage response = await httpClient.GetAsync("https://your-secure-url.com");
// Ensure the request was successful
response.EnsureSuccessStatusCode();
// Read and display the response body
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
// If needed, render the HTML to PDF with IronPDF here
// IronPdf.HtmlToPdf renderer = new IronPdf.HtmlToPdf();
// renderer.RenderHtmlAsPdf(responseBody).SaveAs("output.pdf");
}
catch (HttpRequestException e)
{
// Handle any error responses from the server or connection issues
Console.WriteLine("\nException Caught!");
Console.WriteLine($"Message :{e.Message}");
}
}
}
}
' Example: Using HttpClient with Kerberos Authentication
' Import the necessary namespaces
Imports Microsoft.VisualBasic
Imports System
Imports System.Net
Imports System.Net.Http
Imports System.Net.Http.Headers
Imports System.Threading.Tasks
Namespace KerberosAuthenticationExample
Friend Class Program
Shared Async Function Main(ByVal args() As String) As Task
' Create an instance of HttpClient
Dim handler As New HttpClientHandler With {.UseDefaultCredentials = True}
Using httpClient As New HttpClient(handler)
Try
' Send a GET request to the desired URL
Dim response As HttpResponseMessage = Await httpClient.GetAsync("https://your-secure-url.com")
' Ensure the request was successful
response.EnsureSuccessStatusCode()
' Read and display the response body
Dim responseBody As String = Await response.Content.ReadAsStringAsync()
Console.WriteLine(responseBody)
' If needed, render the HTML to PDF with IronPDF here
' IronPdf.HtmlToPdf renderer = new IronPdf.HtmlToPdf();
' renderer.RenderHtmlAsPdf(responseBody).SaveAs("output.pdf");
Catch e As HttpRequestException
' Handle any error responses from the server or connection issues
Console.WriteLine(vbLf & "Exception Caught!")
Console.WriteLine($"Message :{e.Message}")
End Try
End Using
End Function
End Class
End Namespace
$vbLabelText
$csharpLabel
要點:
- HttpClient 和 HttpClientHandler :使用
HttpClientHandler和UseDefaultCredentials = true允許使用目前使用者的憑證進行Kerberos驗證。 -錯誤處理:實作 try-catch 區塊來管理 HTTP 請求期間的異常。 - HTML 渲染:取得 HTML 後,如有必要,可使用IronPDF將內容渲染成 PDF。
準備好開始了嗎?
Nuget 下載 18,560,885 | 版本: 2026.4 剛剛發布

