如何使用 Cookies 與 IronPDF
在網路技術的背景下,Cookies 是網站儲存在使用者電腦或裝置上的小型資料片段。它們有多種用途,從會話管理(幫助使用者保持登入狀態)到追蹤和分析(收集使用者行為數據以改進網站)。然而,Cookies 的使用引發了隱私方面的討論,導致出台了如 GDPR 和 CCPA 等法規,現代網絡瀏覽器為了應對這些問題,提供了使用者對 Cookies 管理的控制權。
如何使用 Cookies 與 IronPDF
- 下載此 C# 程式庫來啟用 cookies 的使用
- 準備 HTML 內容以使用自定義 cookies 呈現
- 配置 請求上下文 啟用使用 cookies 的屬性
- 使用
應用Cookies
應用 cookies 的方法 - 使用 自定義Cookies 屬性來實現自定義 Cookies
立即開始在您的專案中使用IronPDF,並享受免費試用。
查看 IronPDF 上 Nuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變PDF。
Install-Package IronPdf
請考慮安裝 IronPDF DLL 直接下載並手動安裝到您的專案或GAC表單: IronPdf.zip
手動安裝到您的項目中
下載DLLApply Cookies Example
在使用方法應用 cookies 之前,將 RequestContext 屬性設置為 RequestContexts.Global。然後,創建 ChromeHttpLoginCredentials 類並將其傳遞給 ApplyCookies
方法。現在,渲染器已準備好使用 cookies 將 HTML 內容渲染為 PDF。
:path=/static-assets/pdf/content-code-examples/how-to/cookies-apply-cookies.cs
using IronPdf;
// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.RequestContext = IronPdf.Rendering.RequestContexts.Global;
ChromeHttpLoginCredentials credentials = new ChromeHttpLoginCredentials() {
NetworkUsername = "testUser",
NetworkPassword = "testPassword"
};
string uri = "http://localhost:51169/Invoice";
// Apply cookies
renderer.ApplyCookies(uri, credentials);
Imports IronPdf
' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()
renderer.RenderingOptions.RequestContext = IronPdf.Rendering.RequestContexts.Global
Dim credentials As New ChromeHttpLoginCredentials() With {
.NetworkUsername = "testUser",
.NetworkPassword = "testPassword"
}
Dim uri As String = "http://localhost:51169/Invoice"
' Apply cookies
renderer.ApplyCookies(uri, credentials)
RequestContexts 枚舉:此枚舉定義了瀏覽器請求上下文,用於在個別渲染之間建立關係。這對於管理 cookies 和用戶偏好設定至關重要。
- Isolated:創建一個與之前或未來渲染隔離的新請求上下文。建議使用,以確保當前渲染不受先前渲染的影響。
- Global:使用全局請求上下文,這在所有渲染之間共享。在某些情況下,這對於在渲染之間持久化某些瀏覽器狀態很有用。
- Auto:默認為 IronPdf.Rendering.RequestContexts.Isolated,但如果用戶曾經調用過 IronPdf.ChromePdfRenderer.ApplyCookies,則切換為 IronPdf.Rendering.RequestContexts.Global。(System.String,IronPdf.ChromeHttpLoginCredentials).
自訂 Cookie 範例
在請求中使用自訂 Cookie 需要設置 CustomCookies 屬性。此屬性接受鍵值對的字典,兩者均為字串。
:path=/static-assets/pdf/content-code-examples/how-to/cookies-apply-custom-cookies.cs
using IronPdf;
using System;
using System.Collections.Generic;
// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
Dictionary<string, string> customCookies = new Dictionary<string, string>();
// Apply custom cookies
renderer.RenderingOptions.CustomCookies = customCookies;
var uri = new Uri("https://localhost:44362/invoice");
PdfDocument pdf = renderer.RenderUrlAsPdf(uri);
Imports IronPdf
Imports System
Imports System.Collections.Generic
' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()
Private customCookies As New Dictionary(Of String, String)()
' Apply custom cookies
renderer.RenderingOptions.CustomCookies = customCookies
Dim uri As New Uri("https://localhost:44362/invoice")
Dim pdf As PdfDocument = renderer.RenderUrlAsPdf(uri)