[{"data":1,"prerenderedAt":141},["Reactive",2],{"/guides/csharp/httpclient/using-cookies":3,"/guides/csharp/httpclient/using-cookies-related":59},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":11,"property":12,"output":13,"related":14,"default":6,"body":17,"_type":54,"_id":55,"_source":56,"_file":57,"_extension":58},"/guides/csharp/httpclient/using-cookies","httpclient",false,"","Using cookies","Learn how to use cookies when converting HTML to PDF using C# and the HttpClient library. This guide offers detailed steps with code samples in C# and the HttpClient library, showing how to manage cookies during conversion.","C#","HttpClient","cookies","pdf",[15,16],"accessing-secured-pages","send-custom-http-headers",{"type":18,"children":19,"toc":51},"root",[20,28,33,46],{"type":21,"tag":22,"props":23,"children":24},"element","p",{},[25],{"type":26,"value":27},"text","In this guide, we'll show you how to use cookies when converting HTML to PDF using C# and the HttpClient library.",{"type":21,"tag":22,"props":29,"children":30},{},[31],{"type":26,"value":32},"When converting HTML to PDF, you might need to send cookies to access authenticated content or maintain session state.",{"type":21,"tag":34,"props":35,"children":40},"pre",{"className":36,"code":38,"language":39,"meta":7},[37],"language-csharp","using System;\nusing System.Net.Http;\nusing System.Net.Http.Headers;\nusing System.Threading.Tasks;\n\n// You can get an API key at https://pdfshift.io\nvar apiKey = \"sk_xxxxxxxxxxxx\";\n\nvar client = new HttpClient();\nclient.DefaultRequestHeaders.Add(\"X-API-Key\", apiKey);\n\nvar payload = new\n{\n    source = \"https://www.example.com\",\n    // Send cookies with the request\n    cookies = new\n    {\n        sessionid = \"abc123\",\n        user = \"john_doe\"\n    }\n};\n\nvar json = System.Text.Json.JsonSerializer.Serialize(payload);\nvar content = new StringContent(json, System.Text.Encoding.UTF8, \"application/json\");\n\nvar response = await client.PostAsync(\"https://api.pdfshift.io/v3/convert/pdf\", content);\n\n// Handle errors:\nif (response.StatusCode >= System.Net.HttpStatusCode.BadRequest)\n{\n    throw new Exception($\"Request failed with status code {response.StatusCode}\");\n}\n\nvar result = await response.Content.ReadAsByteArrayAsync();\n\nSystem.IO.File.WriteAllBytes(\"result.pdf\", result);\n\nConsole.WriteLine(\"The PDF document was generated and saved to result.pdf\");\n","csharp",[41],{"type":21,"tag":42,"props":43,"children":44},"code",{"__ignoreMap":7},[45],{"type":26,"value":38},{"type":21,"tag":22,"props":47,"children":48},{},[49],{"type":26,"value":50},"This allows you to maintain session state when accessing protected content.",{"title":7,"searchDepth":52,"depth":52,"links":53},2,[],"markdown","content:guides:csharp:httpclient:using-cookies.md","content","guides/csharp/httpclient/using-cookies.md","md",[60,64,68,72,76,80,84,88,92,96,100,104,108,112,116,120,124,128,132,136,137],{"_path":61,"title":62,"language":10,"library":11,"_id":63},"/guides/csharp/httpclient/accessing-secured-pages","Accessing secured pages","content:guides:csharp:httpclient:accessing-secured-pages.md",{"_path":65,"title":66,"language":10,"library":11,"_id":67},"/guides/csharp/httpclient/adding-a-custom-header-or-footer","Adding a custom header or footer","content:guides:csharp:httpclient:adding-a-custom-header-or-footer.md",{"_path":69,"title":70,"language":10,"library":11,"_id":71},"/guides/csharp/httpclient/adding-a-text-watermark","Adding a text watermark","content:guides:csharp:httpclient:adding-a-text-watermark.md",{"_path":73,"title":74,"language":10,"library":11,"_id":75},"/guides/csharp/httpclient/adding-an-image-watermark","Adding an image watermark","content:guides:csharp:httpclient:adding-an-image-watermark.md",{"_path":77,"title":78,"language":10,"library":11,"_id":79},"/guides/csharp/httpclient/avoid-conversion-on-error","Avoid conversion on error","content:guides:csharp:httpclient:avoid-conversion-on-error.md",{"_path":81,"title":82,"language":10,"library":11,"_id":83},"/guides/csharp/httpclient/convert-html-to-pdf-from-a-url","Convert HTML to PDF from a URL","content:guides:csharp:httpclient:convert-html-to-pdf-from-a-url.md",{"_path":85,"title":86,"language":10,"library":11,"_id":87},"/guides/csharp/httpclient/convert-html-to-pdf-from-raw-html","Convert HTML to PDF from raw HTML","content:guides:csharp:httpclient:convert-html-to-pdf-from-raw-html.md",{"_path":89,"title":90,"language":10,"library":11,"_id":91},"/guides/csharp/httpclient/define-a-time-limit","Define a time limit","content:guides:csharp:httpclient:define-a-time-limit.md",{"_path":93,"title":94,"language":10,"library":11,"_id":95},"/guides/csharp/httpclient/exporting-only-a-specific-set-of-pages","Exporting only a specific set of pages","content:guides:csharp:httpclient:exporting-only-a-specific-set-of-pages.md",{"_path":97,"title":98,"language":10,"library":11,"_id":99},"/guides/csharp/httpclient/generate-a-document-with-full-height","Generate a document with full height","content:guides:csharp:httpclient:generate-a-document-with-full-height.md",{"_path":101,"title":102,"language":10,"library":11,"_id":103},"/guides/csharp/httpclient/loading-css-from-a-string","Loading CSS from a string","content:guides:csharp:httpclient:loading-css-from-a-string.md",{"_path":105,"title":106,"language":10,"library":11,"_id":107},"/guides/csharp/httpclient/loading-css-from-a-url","Loading CSS from a URL","content:guides:csharp:httpclient:loading-css-from-a-url.md",{"_path":109,"title":110,"language":10,"library":11,"_id":111},"/guides/csharp/httpclient/loading-javascript-from-a-string","Loading JavaScript from a string","content:guides:csharp:httpclient:loading-javascript-from-a-string.md",{"_path":113,"title":114,"language":10,"library":11,"_id":115},"/guides/csharp/httpclient/loading-javascript-from-a-url","Loading JavaScript from a URL","content:guides:csharp:httpclient:loading-javascript-from-a-url.md",{"_path":117,"title":118,"language":10,"library":11,"_id":119},"/guides/csharp/httpclient/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:csharp:httpclient:protecting-the-generated-pdf.md",{"_path":121,"title":122,"language":10,"library":11,"_id":123},"/guides/csharp/httpclient/receive-a-webhook-event","Receive a webhook event","content:guides:csharp:httpclient:receive-a-webhook-event.md",{"_path":125,"title":126,"language":10,"library":11,"_id":127},"/guides/csharp/httpclient/save-your-pdf-online-and-get-back-a-url","Save your PDF online and get back a URL","content:guides:csharp:httpclient:save-your-pdf-online-and-get-back-a-url.md",{"_path":129,"title":130,"language":10,"library":11,"_id":131},"/guides/csharp/httpclient/save-your-pdf-to-your-amazon-s3-bucket","Save your PDF to your Amazon S3 bucket","content:guides:csharp:httpclient:save-your-pdf-to-your-amazon-s3-bucket.md",{"_path":133,"title":134,"language":10,"library":11,"_id":135},"/guides/csharp/httpclient/send-custom-http-headers","Send custom HTTP headers","content:guides:csharp:httpclient:send-custom-http-headers.md",{"_path":4,"title":8,"language":10,"library":11,"_id":55},{"_path":138,"title":139,"language":10,"library":11,"_id":140},"/guides/csharp/httpclient/waiting-for-a-custom-element-to-be-ready","Waiting for a custom element to be ready","content:guides:csharp:httpclient:waiting-for-a-custom-element-to-be-ready.md",1780405099802]