了解 IronPDF 中的 Kerberos
如何讓 UrlToPdf 在 Kerberos 驗證環境下正常運作?
若要在 IronPDF 的 UrlToPdf 中使用 Kerberos 驗證,您應在渲染設定中設定使用者名稱和密碼。 您可參閱 IronPDF 文件以獲取更多詳細資訊:IronPdf.ChromeHttpLoginCredentials。
我們建議使用 System.Net.Http.HttpClient 來下載 HTML 內容,之後您可以進行渲染。 此方法可讓您在 IronPDF 處理內容之前,先處理 HTTP 請求,包括需要驗證的請求。
以下是一篇關於使用 Kerberos 下載頁面的線上指南:System.Net.Http.HttpClient 如何選擇驗證類型? 此 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
重點:
- HttpClient 和 HttpClientHandler:使用
HttpClientHandler與UseDefaultCredentials = true,以便透過當前使用者的憑證進行 Kerberos 驗證。 - 錯誤處理:實作 try-catch 區塊以管理 HTTP 請求過程中的例外狀況。
- HTML 渲染:取得 HTML 後,如有需要,可利用 IronPDF 將內容渲染為 PDF。

