Tim B James
Tim B James

Reputation: 20364

Session state lost after HttpWebRequest within AJAX post

I have a bit of strange behaviour in an asp.net web application that involves the session state being lost.

Process

A user logs into the application and the session is set. They then fill out 1 field, and the application then does an AJAX POST to a .asmx web service. Within the web service, I am using a HttpWebRequest to grab data from another server.

This data is then output to the browser.

A few more fields are then filled in, and the data is then again Post to the same web service via an AJAX POST.

Problem

Straight after the HttpWebRequest, I grab the username from a session variable. This works. On the next AJAX request however, the session no longer exists.

While testing this, I removed the stage at which the HttpWebRequest is called and my session is never lost. So for some reason, the session is removed AFTER my first AJAX POST and before the second AJAX POST only if I am running the HttpWebRequest code.

Code

I am not doing anything fancy in the code. Just doing a simple jQuery AJAX Post

$.ajax({ url: method, data: params, type: "POST", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { // handle data }, error: function(xhr,status,error) { } });

Creating a System.Net.HttpWebRequest and then getting the System.Net.HttpWebResponse out of that.

Then reading a session variable dim username as string = Session(_SESSION_USERNAME).ToString()

I have never noticed this behaviour before when using HttpWebRequest before (not using any AJAX though)

Function Backfill(value As String) As Details

    Dim details As Details = Nothing
    Dim appSettings As ConfigSettings.AppConfig = ConfigSettings.AppConfig.getConfig()

    Dim url As String = appSettings.Settings.BackfillUrl
    Dim username As String = appSettings.Settings.BackfillUser
    Dim password As String = appSettings.Settings.BackfillPass

    Dim expParameters As String = ""
    Dim xml As XmlDocument = Nothing
    Dim xmlHttp As XMLHTTP = Nothing
    Dim nodeList As XmlNodeList = Nothing
    Dim node As XmlNode = Nothing
    Dim response As String = ""
    Dim success As String = ""

    '
    ' REMOVED TO HIDE INFO
    expParameters = "<PARAMETERS>" & _            
        "</PARAMETERS>"

    Try

        xmlHttp = New XMLHTTP()
        xmlHttp.open("POST", url)
        xmlHttp.Send(expParameters)

        response = xmlHttp.responseText()

        xml = New XmlDocument
        xml.LoadXml(response)
        SaveExperianFile(xml, value)
        nodeList = xml.DocumentElement.ChildNodes
        node = nodeList.Item(0)
        success = node.Attributes.GetNamedItem("success").Value.ToString.Trim
        If success.ToLower.Trim = "y" Then
            details = SetDetails(xml)
        End If

    Catch ex As Exception
    Finally
        If Not xmlHttp Is Nothing Then
            xmlHttp.Dispose()
            xmlHttp = Nothing
        End If
    End Try

    Return details

End Function

edit

The XMLHTTP Class code can be seen here http://codepaste.net/ymnqsf

edit

Seems as though something strange is happening when I am saving the XMLDocument to my file system.

Private Sub SaveExperianFile(xml As XmlDocument, value As String)
    Dim appConfig As ConfigSettings.AppConfig = ConfigSettings.AppConfig.getConfig()
    Try
        xml.Save(HttpContext.Current.Server.MapPath(appConfig.Settings.SavePath & value & "_backfill.xml"))
    Catch ex As Exception

    End Try
End Sub

If I don't call this method, then the session is always set.

Question

Do you know what is causing this behaviour?

Upvotes: 3

Views: 3312

Answers (2)

Amit Rai Sharma
Amit Rai Sharma

Reputation: 4225

Can you check if you just loose the session or the whole application is restarted. If you are saving the XML in the virtual directory / web application folder then it might case web application to restart. If many dozen files were added in short succession to each other, that would be a case to restart the App Pool.

Upvotes: 3

Pankaj Kumar
Pankaj Kumar

Reputation: 1768

just a hunch, but maybe you need to maintain cookies across HttpWebRequests.

have a look at this question for more help.

Http web request doesn't maintaining session

Upvotes: 0

Related Questions