문제

In my code I'm sending a HttpWebRequest to a page in my website.

When request sends to this page, It doesn't maintain the Session values.

Below is the code, from where I'm generating the web request:

Public Overloads Shared Function ReadURL(ByVal sUrl As String) As String
        Dim sBody As String
        Dim oResponse As HttpWebResponse
        Dim oRequest As HttpWebRequest
        Dim oCookies As New CookieContainer
        oRequest = WebRequest.Create("http://localhost:64802/inventory/purchase_order.aspx?id=5654")
        oRequest.CookieContainer = oCookies
        oResponse = oRequest.GetResponse()
        Dim oReader As New StreamReader(oResponse.GetResponseStream())
        sBody = oReader.ReadToEnd
        oReader.Close()
        oResponse.Close()
        Return sBody
    End Function

Below is the code written on Page_Load of Purchaseorder.aspx.vb:

iDomains_ID = Session("Domains_ID")
        iLogin_ID = Session("Login_ID")
        sPage = Request.Path
        If Request.QueryString.Count > 0 Then sPage &= "?" & Request.QueryString.ToString()
        sPage = shared01.Encrypt(sPage, Application("PK"))

        If Not User.Identity.IsAuthenticated Or iLogin_ID = 0 Then
            Response.Redirect("/login.aspx?page=" & sPage)
            Exit Sub
        End If

Above code doesn't gets the session values and it redirects to the login page.

So, how i can maintain the session on both pages during HttpWebRequest.

Looking for your replies.

EDIT

I've tried to use CookieContainer class as you can see in above code. But it doesn't work at all.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top