System.Net.WebRequest.Create Resetting Session Variables

Mosteck

Active member
Joined
Nov 23, 2008
Messages
31
Location
Toronto
Programming Experience
3-5
I'm trying to use ITextSharp to convert a HTML page to a PDF but running into some problems with the system.net.webrequest.GetResponse as it seems to clear my session states/values.

The code is:

VB.NET:
Protected Sub ExportToPDFButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExportToPDFButton.Click

        ' Creates variables for the location of the page and the destination of the PDF file

        Dim StrURL As Uri = Request.Url
        Dim URL As String = StrURL.ToString
        Dim FilePath As String = Server.MapPath("TEST/PO_" & Session("PurchaseOrderToPrint") & ".htm")
        Dim Writer As New System.IO.StreamWriter(FilePath)

        ' Converts the page

        Dim HTMLText As String = String.Empty
        Dim FileName As String = Server.MapPath("TEST/PO_" & Session("PurchaseOrderToPrint") & ".xml")
        Try
            Dim RequestIP As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create(URL), System.Net.HttpWebRequest)
            RequestIP.Timeout = 10000
            Using ResponseIP As System.Net.HttpWebResponse = DirectCast(RequestIP.GetResponse(), System.Net.HttpWebResponse)
                Using StreamIP As System.IO.Stream = ResponseIP.GetResponseStream
                    Using ReaderText As New System.IO.StreamReader(StreamIP)
                        HTMLText = ReaderText.ReadToEnd
                        Dim Text As String = HTMLText
                        Dim TextWriter As New System.IO.StreamWriter(FileName)
                        TextWriter.Write(Text)
                        TextWriter.Close()
                    End Using
                End Using
            End Using
        Catch ex As Exception
        End Try

        ' Creats the PDF

        Dim Doc As New Document
        pdf.PdfWriter.GetInstance(Doc, New System.IO.FileStream(Server.MapPath("TEST/PO_" & Session("PurchaseOrderToPrint") & ".pdf"), System.IO.FileMode.Create))
        html.HtmlParser.Parse(Doc, Server.MapPath("TEST/PO_" & Session("PurchaseOrderToPrint") & ".htm"))

' Erases temporary files

        If System.IO.File.Exists(Server.MapPath("TEST/PO_" & Session("PurchaseOrderToPrint") & ".htm")) Then
            System.IO.File.Delete(Server.MapPath("TEST/PO_" & Session("PurchaseOrderToPrint") & ".htm"))
        End If

        If System.IO.File.Exists(Server.MapPath("TEST/PO_" & Session("PurchaseOrderToPrint") & ".xml")) Then
            System.IO.File.Delete(Server.MapPath("TEST/PO_" & Session("PurchaseOrderToPrint") & ".xml"))
        End If

    End Sub

Seems when it executes the following section:

VB.NET:
 Using ResponseIP As System.Net.HttpWebResponse = DirectCast(RequestIP.GetResponse(), System.Net.HttpWebResponse)

It goes back to to Page_Load but does not have the session information anymore.

Can anyone shed some light on this?
 
Just an update, I've worked around this by sending a query in the URL (i.e. www.blah.com/test?PO=00000) and then resetting the session variable with
VB.NET:
Session("PurchaseOrderToPrint") = Request.QueryString("PO")
so I don't need to change too much code.

I still would like to find out why the above happens as I'd like to go to a clean URL without the query string.
 
Back
Top