Note that there are some explanatory texts on larger screens.

plurals
  1. POSession state lost after HttpWebRequest within AJAX post
    primarykey
    data
    text
    <p>I have a bit of strange behaviour in an <code>asp.net</code> web application that involves the session state being lost.</p> <p><strong>Process</strong></p> <p>A user logs into the application and the session is set. They then fill out 1 field, and the application then does an <code>AJAX POST</code> to a <code>.asmx</code> web service. Within the web service, I am using a <code>HttpWebRequest</code> to grab data from another server.</p> <p>This data is then output to the browser.</p> <p>A few more fields are then filled in, and the data is then again <code>Post</code> to the same web service via an <code>AJAX POST</code>.</p> <p><strong>Problem</strong></p> <p>Straight after the <code>HttpWebRequest</code>, I grab the username from a session variable. This works. On the next <code>AJAX</code> request however, the session no longer exists.</p> <p>While testing this, I removed the stage at which the <code>HttpWebRequest</code> is called and my session is never lost. So for some reason, the session is removed AFTER my first <code>AJAX POST</code> and before the second <code>AJAX POST</code> only if I am running the <code>HttpWebRequest</code> code.</p> <p><strong>Code</strong></p> <p>I am not doing anything fancy in the code. Just doing a simple jQuery AJAX Post</p> <p>$.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) { } });</p> <p>Creating a <code>System.Net.HttpWebRequest</code> and then getting the <code>System.Net.HttpWebResponse</code> out of that.</p> <p>Then reading a session variable <code>dim username as string = Session(_SESSION_USERNAME).ToString()</code></p> <p>I have never noticed this behaviour before when using <code>HttpWebRequest</code> before (not using any AJAX though)</p> <pre><code>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 = "&lt;PARAMETERS&gt;" &amp; _ "&lt;/PARAMETERS&gt;" 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 </code></pre> <p><strong>edit</strong></p> <p>The XMLHTTP Class code can be seen here <a href="http://codepaste.net/ymnqsf" rel="nofollow">http://codepaste.net/ymnqsf</a></p> <p><strong>edit</strong></p> <p>Seems as though something strange is happening when I am saving the <code>XMLDocument</code> to my file system.</p> <pre><code>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 &amp; value &amp; "_backfill.xml")) Catch ex As Exception End Try End Sub </code></pre> <p>If I don't call this method, then the session is always set.</p> <p><strong>Question</strong></p> <p>Do you know what is causing this behaviour?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload