facebooktwittermenuarrow-up

GemBox Support Forum

Is there a way to inject HTTP headers into conversion process?

I have HTML text that contains an image link. I am assuming that during the conversion process, GemBox.Document goes out to fetch the image.

However, this image link requires a key in the HTTP header (or a cookie). Is there a way to inject either an HTTP header (preferable) or a cookie into the conversion process?

Hi Robert,

Yes, you can handle the HtmlLoadOptions.ResourceLoading event.
For example, try something like this:

var options = new HtmlLoadOptions();

options.ResourceLoading += (sender, e) =>
{
    if (e.Type != ResourceType.Image)
        return;

    var resourceRequest = (HttpWebRequest)WebRequest.Create(e.Uri);
    resourceRequest.Headers.Add("X-Custom-Header", "Custom Value");

    using (var resourceResponse = resourceRequest.GetResponse())
    using (var stream = resourceResponse.GetResponseStream())
    using (var memoryStream = new MemoryStream())
    {
        stream.CopyTo(memoryStream);
        e.Data = memoryStream.ToArray();
    }
};

DocumentModel document = new DocumentModel();
document.Content.LoadText("Your HTML Text", options);

I hope this helps.

Regards,
Mario

1 Like