XHtmlOptions HttpAdditionalHeaders Property. Additional HTTP headers to send in the request. ABCpdf .NET PDF Library.

ABCpdf .net

 
   

 

Type Default Value Read Only Description
[C#]string

[Visual Basic]
String
"" No Additional HTTP headers to send in the request.

 

   

Notes
 

This property specifies HTTP headers added to the default headers. It must follow the syntax of HTTP headers, typically delimited using a cr+lf combination.

For example, you may add the cookies from a response to a request under some authentication (e.g. ASP.NET Forms) where a session ID is returned for subsequent requests so re-authentication is not needed. See the NoCookie property for further information if you specify cookies in the HTTP headers.

The headers are sent in only the request for the URL; they are not sent in the requests for the linked resources (e.g. CSS and images) in the obtained HTML page.

The additional headers are always used by the MSHTML Engine. Under Gecko, these headers are only sent if you are using the Screen Media or if you are using the POST RequestMethod.

 

   

Example
 

The following example shows how this property may be used.

[C#]
Doc doc = new Doc();
string url = ...;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = new CookieContainer(); // required for HttpWebResponse.Cookies
request.Credentials = ...;
using(WebResponse resp = request.GetResponse()) {
  // cookieless Forms Authentication adds authentication ticket to the URL
  url = resp.ResponseUri.AbsoluteUri;
  HttpWebResponse response = (HttpWebResponse)resp;
  if(response.Cookies.Count>0) { // includes ASP.NET_SessionId
    bool needsCookie2 = false;
    StringBuilder builder = new StringBuilder("Cookie: ");
    for(int i = 0; i<response.Cookies.Count; ++i) {
      Cookie cookie = response.Cookies[i];
      if(!needsCookie2 && cookie.Version!=1)
        needsCookie2 = true;
      if(i>0)
        builder.Append("; ");
      builder.Append(cookie.ToString());
    }
    builder.Append(!needsCookie2? "\r\n": "\r\nCookie2: $Version=1\r\n");
    doc.HtmlOptions.HttpAdditionalHeaders = builder.ToString();
  }
}
doc.HtmlOptions.NoCookie = true;
doc.HtmlOptions.PageLoadMethod = PageLoadMethodType.MonikerForHtml;
int id = doc.AddImageUrl(url);
doc.Save(Server.MapPath("HttpHeaders.pdf"));
doc.Clear();

[Visual Basic]
Dim doc As Doc = New Doc()
Dim url As String = ...
Dim request As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
request.CookieContainer = New CookieContainer() ' required for HttpWebResponse.Cookies
request.Credentials = ...
Using resp As WebResponse = request.GetResponse()
  ' cookieless Forms Authentication adds authentication ticket to the URL
  url = resp.ResponseUri.AbsoluteUri
  Dim response As HttpWebResponse = CType(resp, HttpWebResponse)
  If response.Cookies.Count > 0 Then ' includes ASP.NET_SessionId
    Dim needsCookie2 As Boolean = False
    Dim builder As StringBuilder = New StringBuilder("Cookie: ")
    For i As Integer = 0 To response.Cookies.Count - 1
      Dim cookie As Cookie = response.Cookies(i)
      If Not needsCookie2 AndAlso cookie.Version <> 1 Then
        needsCookie2 = True
      End If
      If i > 0 Then builder.Append("; ")
      builder.Append(cookie.ToString())
    Next
    If Not needsCookie2 Then
      builder.Append(ControlChars.CrLf)
    Else
      builder.Append(ControlChars.CrLf & "Cookie2: $Version=1" & ControlChars.CrLf)
    End If
    doc.HtmlOptions.HttpAdditionalHeaders = builder.ToString()
  End If
End Using
doc.HtmlOptions.NoCookie = True
doc.HtmlOptions.PageLoadMethod = PageLoadMethodType.MonikerForHtml
Dim id As Integer = doc.AddImageUrl(url)
doc.Save(Server.MapPath("HttpHeaders.pdf"))
doc.Clear()

ASP.NET_SessionId. If your application runs in ASP.NET, you cannot use the cookie from the application's originating request as it identifies the session. If the site containing the target HTML page is different from your site, the session ID is invalid. If it is the same, this will cause a re-entrance in the same session, and ASP.NET may not allow re-entrance for the same session.