How to manitain session values during HttpWebRequest?

孤街醉人 提交于 2019-12-05 19:30:53

As an alternative, assuming the calling and called pages are in the same application, you could use the Server.Execute method to load the content of the page without making a separate request to the site:

Public Overloads Function ReadURL(ByVal sUrl As String) As String
    Using writer As New StringWriter()
       Server.Execute("~/inventory/purchase_order.aspx?id=5654", writer, False)
       Return writer.ToString()
    End Using
End Function

If I've understood you correctly, you're making a request from one page in your site to another, and you want to send the cookies from the current HttpRequest with your WebRequest?

In that case, you'll need to manually copy the cookies to the CookieContainer:

For Each key As String In Request.Cookies.AllKeys
   Dim sourceCookie As HttpCookie = Request.Cookies(key)

   Dim destCookie As New Cookie(sourceCookie.Name, sourceCookie.Value, sourceCookie.Path, "localhost")
   destCookie.Expires = sourceCookie.Expires
   destCookie.HttpOnly = sourceCookie.HttpOnly
   destCookie.Secure = sourceCookie.Secure

   oCookies.Add(destCookie)
Next

NB: You'll either need to make the ReadUrl function non-Shared, or pass the current HttpRequest as a parameter.

You'll also need to make sure the calling page has EnableSessionState="false" in the <%@ Page ... %> directive, otherwise the page you're calling will hang trying to obtain the session lock.

Your code seems like you will need to make a request and a post. The first request will redirect you to your login page. The second will be a request where you post to the login page, which will start the session and (?) store information into the session variables. That post (to the login page) will then redirect you to the page you want.

I used code in this example http://www.codeproject.com/Articles/145122/Fetching-ASP-NET-authenticated-page-with-HTTPWebRe (I tweaked it a bit) to write an application to do this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!