[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1737
  • Last Modified:

How to read cookies from the httpWebResponse

I have two domains
www.domain1.com
www.domain2.com
At www.domain1.com I am succesfully reading the default.aspx page from the www.domain2.aspx by using the httpWebResponse object but I dont know how to read the cookies of the www.domain2.aspx

I will really appreciate that if someone can provide the code example in ASP.net using VB.net

Thanks
Protected Sub bttnReadRequest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bttnSendRequest.Click
        'Create the HttpWebRequest object
        Dim req As HttpWebRequest = WebRequest.Create("http://www.domain2.com/default.aspx")
 
        Try
            'Get the data as an HttpWebResponse object
            Dim resp As HttpWebResponse = req.GetResponse()
 
            'Convert the data into a string (assumes that you are requesting text)
            Dim sr As New StreamReader(resp.GetResponseStream())
            Dim results As String = sr.ReadToEnd()
            sr.Close()
 
            html.Text = results
        Catch wex As WebException
            Response.Write("<font color=red>SOMETHING WENT AWRY!<br />Status: " & wex.Status & "Message: " & wex.Message & "</font>")
        End Try
 
    End Sub

Open in new window

0
shieldguy
Asked:
shieldguy
  • 4
  • 3
1 Solution
 
sasapopovicCommented:
Hi shieldguy,

When you make a new HttpWebRequest you should set its CookieContainer property to new instance of CookieContainer class (I'm not good with VB so I'll write in C#):

req.CookieContainer  = new CookieContainer();

Then, when you get a response from the server you can access the cookies collection like this: resp.Cookies;

You can check example in MSDN here: http://msdn2.microsoft.com/en-us/library/system.net.httpwebresponse.cookies.aspx
NB: you can find example in VB on that page also.

I hope this will help you.

Cheers,
Sasa
0
 
shieldguyAuthor Commented:
I am able to read the cookies of the other domain but their is one problem, due to some reason I am still unable to read the cookies of one domain

I am attching the code on which I am generating the cookie on the target domain

Thanks
0
 
shieldguyAuthor Commented:
Here is the code

  Public Sub BasketID()

        Dim tempBasketId As String

        If HttpContext.Current.Request.Cookies("Basket") Is Nothing Then

            tempBasketId = HttpContext.Current.Session.SessionID

            HttpContext.Current.Response.Cookies("Basket")("BasketID") = tempBasketId
            HttpContext.Current.Response.Cookies("Basket").Path = "/"
            HttpContext.Current.Response.Cookies("Basket").Expires = DateTime.Now.AddYears(1)

            'addbasketID(tempBasketId)

        Else

            Try
                tempBasketId = HttpContext.Current.Request.Cookies("Basket")("BasketID")
            Catch ex As Exception : End Try

            ' Need to check if the ID in the cookie relates to an ID in the database
            ' if not we need to add a new basket ID and update the cookie and remove
            ' any entries in the Basket Line table

            If String.IsNullOrEmpty(tempBasketId) Then
                tempBasketId = HttpContext.Current.Session.SessionID
                HttpContext.Current.Response.Cookies("Basket")("BasketID") = tempBasketId
            Else
                HttpContext.Current.Response.Cookies("Basket")("BasketID") = tempBasketId
            End If


            HttpContext.Current.Response.Cookies("Basket").Path = "/"
            HttpContext.Current.Response.Cookies("Basket").Expires = DateTime.Now.AddYears(1)

            'addbasketID(tempBasketId)

        End If

    End Sub
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
sasapopovicCommented:
You forgot to attach the code.

Do you want to say that you can read the cookie from one domain but can not read it from another domain? Are you making those cookies differently on those two domains?
0
 
sasapopovicCommented:
That code looks fine to me although I always make an instance of HttpCookie class and then add it to HttpContext.Current.Response.Cookies collection. Like this:

HttpCookie cookie = new HttpCookie("Basket", tempBasketId);
cookie.Path = "/";
cookie.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Add(cookie);

Maybe it has something to do with the way you are adding that cookie and accessing it. I see you are using cookie index "BasketID" and I didn't work with that before so it may be an issue with usage of cookie index.
0
 
shieldguyAuthor Commented:
sorry but I am getting confused now because i have two sites with different domains on one site I can access the value of the cookies but on the other site I unable to see any cookies. and on the both sites the cookies are created in the same way
as I given the code above

Can you tplease ell me what would be the reason

Thanks
0
 
shieldguyAuthor Commented:
Thanks a million
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now