如何使用 Cookies 與 IronPDF

This article was translated from English: Does it need improvement?
Translated
View the article in English

查克尼思·賓

在網路技術的背景下,Cookies 是網站儲存在使用者電腦或裝置上的小型資料片段。它們有多種用途,從會話管理(幫助使用者保持登入狀態)到追蹤和分析(收集使用者行為數據以改進網站)。然而,Cookies 的使用引發了隱私方面的討論,導致出台了如 GDPR 和 CCPA 等法規,現代網絡瀏覽器為了應對這些問題,提供了使用者對 Cookies 管理的控制權。


C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

立即開始在您的專案中使用IronPDF,並享受免費試用。

第一步:
green arrow pointer

查看 IronPDFNuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變PDF。

C# NuGet 程式庫用于 PDF nuget.org/packages/IronPdf/
Install-Package IronPdf

請考慮安裝 IronPDF DLL 直接下載並手動安裝到您的專案或GAC表單: IronPdf.zip

手動安裝到您的項目中

下載DLL

Apply 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)
VB   C#

RequestContexts 枚舉:此枚舉定義了瀏覽器請求上下文,用於在個別渲染之間建立關係。這對於管理 cookies 和用戶偏好設定至關重要。

  • Isolated:創建一個與之前或未來渲染隔離的新請求上下文。建議使用,以確保當前渲染不受先前渲染的影響。
  • Global:使用全局請求上下文,這在所有渲染之間共享。在某些情況下,這對於在渲染之間持久化某些瀏覽器狀態很有用。
  • Auto:默認為 IronPdf.Rendering.RequestContexts.Isolated,但如果用戶曾經調用過 IronPdf.ChromePdfRenderer.ApplyCookies,則切換為 IronPdf.Rendering.RequestContexts.Global。(System.String,IronPdf.ChromeHttpLoginCredentials).

在請求中使用自訂 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)
VB   C#

查克尼思·賓

軟體工程師

Chaknith 是開發者界的夏洛克福爾摩斯。他第一次意識到自己可能有個軟體工程的未來,是在他為了娛樂而參加程式挑戰的時候。他的重點是 IronXL 和 IronBarcode,但他也引以為豪的是,他幫助客戶解決所有產品的問題。Chaknith 利用他與客戶直接對話中獲得的知識,以進一步改進產品。他的實際反饋超越了 Jira 工單,並支持產品開發、文件撰寫和行銷,以提升客戶的整體體驗。不在公司時,他通常在學習機器學習、寫程式和徒步旅行。