HttpWebRequest not sending cookies?
HttpWebRequest not sending cookies?
HttpWebRequest not sending cookies?
HttpWebRequest not sending cookies?
HttpWebRequest not sending cookies?
HttpWebRequest not sending cookies? HttpWebRequest not sending cookies? HttpWebRequest not sending cookies? HttpWebRequest not sending cookies? HttpWebRequest not sending cookies? HttpWebRequest not sending cookies? HttpWebRequest not sending cookies? HttpWebRequest not sending cookies?
HttpWebRequest not sending cookies? HttpWebRequest not sending cookies?
HttpWebRequest not sending cookies?
Go Back  Xtreme Visual Basic Talk > > > HttpWebRequest not sending cookies?


Reply
 
Thread Tools Display Modes
  #1  
Old 01-29-2008, 12:08 PM
rrvball rrvball is offline
Newcomer
 
Join Date: Dec 2006
Location: Colorado
Posts: 12
Question HttpWebRequest not sending cookies?


I'm new to VB and am using 2008 VB Express edition. I'm trying to access a webpage's html using HttpWebRequest and that page requires cookies or it sends me to a login page. I found code examples around the web and have written the following code:
Code:
Shared cookies As New Net.CookieContainer
    Shared cookies_init As Boolean = True 'False
    Public theHTML As String
    Public valid As Boolean

    Private Declare Function InternetGetCookie Lib "wininet.dll" Alias _
      "InternetGetCookieA" (ByVal lpszUrlName As String, _
                            ByVal lpszCookieName As String, ByVal lpszCookieData As String, _
                            ByRef lpdwSize As Long) As Boolean
    Private Function GetCookies(ByVal URL As String) As String ', Optional CookieName As String) As String
        ' Get locally-stored cookies for a specified URL
        Dim buffer As String
        Dim length As Long

        ' prepare the receiving buffer
        length = 10240
        buffer = Space$(length)
        ' query WinInet for cookies from this URL
        ' a zero value means failure
        If InternetGetCookie(URL, vbNullString, buffer, length) = 0 Then Return ""

        ' LENGTH has received the size of returned data
        GetCookies = Left(buffer, length)
    End Function
    Public Sub New(ByVal URL As String, _
          Optional ByVal TimeoutSeconds As Integer = 10)
        ' Retrieves the HTML from the specified URL,
        '        Dim objWC As New System.Net.WebClient()
        '        theHTML = New System.Text.UTF8Encoding().GetString( _
        '           objWC.DownloadData(URL))
        '    End Sub
        '    Public Sub Newx(ByVal URL As String, _
        '         Optional ByVal TimeoutSeconds As Integer = 10)
        ' using a default timeout of 10 seconds

        'First check to see if cookies need to be initialized
        If cookies_init = False Then
            Dim cookiedata As String
            cookiedata = GetCookies(URL)
            If cookiedata.Length > 0 Then
                Dim URI As New System.Uri(URL)
                cookies.SetCookies(URI, cookiedata.Replace(";", ","))
            End If
            cookies_init = True
        End If

        Dim objRequest As Net.HttpWebRequest
        Dim objResponse As Net.WebResponse
        Dim objStreamReceive As System.IO.Stream
        Dim objEncoding As System.Text.Encoding
        Dim objStreamRead As System.IO.StreamReader

        Try
            ' Setup our Web request
            objRequest = Net.WebRequest.Create(New System.Uri(URL))
            objRequest.Timeout = TimeoutSeconds * 1000
            objRequest.UseDefaultCredentials = True
            objRequest.AllowAutoRedirect = False
            objRequest.CookieContainer = cookies
            objRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)"

            ' Retrieve data from request
            objResponse = objRequest.GetResponse
            objStreamReceive = objResponse.GetResponseStream
            objEncoding = System.Text.Encoding.GetEncoding("utf-8")
            objStreamRead = New System.IO.StreamReader(objStreamReceive, objEncoding)
            ' Set function return value
            theHTML = objStreamRead.ReadToEnd()
            ' Check if available, then close response
            If Not objResponse Is Nothing Then
                objResponse.Close()
            End If
            valid = True
        Catch
            ' Error occured grabbing data, simply return nothing
            theHTML = ""
            valid = False
        End Try
    End Sub
The code grabs the cookies from IE and loads the cookiecontainer and I can extract the cookies from the cookiecontainer using the GetCookies member function so I know the cookies exist for that URL. But, when I send the request, no cookies are sent. I captured the headers using WireShark.

Any ideas why the cookies are not being sent?
Is there a better/easier way to do this?
I didn't want to use the webbrowser control because it loads the whole page including all images and popups. I just want to get the source page html and extract some items from it.

Thanks for your help.
Reply With Quote
  #2  
Old 02-04-2008, 09:00 AM
shaul_ahuvaHttpWebRequest not sending cookies? shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

I could be missing something, but I don't see anything wrong with your code at first glance.

Have you tried sending a test cookie (e.g. create a Cookie instance and add it to the request) to see if it might be something with how the cookies are formatted?
Reply With Quote
  #3  
Old 02-04-2008, 01:01 PM
rrvball rrvball is offline
Newcomer
 
Join Date: Dec 2006
Location: Colorado
Posts: 12
Default

Thanks for taking a look.

I haven't tried adding a cookie as you suggested, but I have added the following code to see if the cookies can be retrieved.
Code:
            Dim cookiecol As Net.CookieCollection
            cookiecol = objRequest.CookieContainer.GetCookies(New System.Uri(URL))
            For Each c As Cookie In cookiecol
                Console.WriteLine(c.ToString())
            Next
And the cookies are there. I also examined the cookies in the debugger and see that all the fields seem to be set correctly.

How else do I make sure the cookie is formatted correctly?
Reply With Quote
  #4  
Old 02-12-2008, 10:08 AM
rrvball rrvball is offline
Newcomer
 
Join Date: Dec 2006
Location: Colorado
Posts: 12
Default

Anybody have any answers for my questions?
Reply With Quote
  #5  
Old 02-07-2009, 02:38 PM
munishsethi777 munishsethi777 is offline
Newcomer
 
Join Date: Feb 2009
Posts: 14
Default

Hi

i dont kno if u are still under problems.. but i m somewat on the same boat now.

i m trying to make first request to a website using post, getting its cookies from header and setting it again on the next request's header and then making another post.. i cant still loging.. i dont kno what the mess??

hav u achieved what u were trying to?
Reply With Quote
  #6  
Old 02-08-2009, 12:33 AM
rrvball rrvball is offline
Newcomer
 
Join Date: Dec 2006
Location: Colorado
Posts: 12
Default

No, I never did get it to work and as you can see, nobody offered a solution. You'd think with all these people out there, someone would be able to provide a solution to this seemingly simple problem.
Reply With Quote
  #7  
Old 02-08-2009, 10:46 PM
munishsethi777 munishsethi777 is offline
Newcomer
 
Join Date: Feb 2009
Posts: 14
Default

yes rrv.. i was thinking that u maynot reply.. nice to see your reply atleast.


i have developed a class which does somewhat u did in your sample.. I mean making first request, extracting cookies out of its headers and then setting them to the new request's header.. Now i have a couple of confusions here..

1. Should we extract cookies from first request's header or cookieContainer??

2. After extracting cookies from first request's header of cookieContainer, where should we set it on? Second Request's cookieContainer or header?

I can see many leaders on this forum are replying to such posts..( i have been banging my head in this issue since last 10 days)... they are offering solutions, but cant really find any real time sample by them..
I have asked Atma( one of the active responder on this forum) to review my sample and see whats the glutch.. as i get it resolved, i will definately come back to u/??

Have u gave up on this issue? or still scratching your head into it?

Munish.
Reply With Quote
  #8  
Old 02-09-2009, 03:18 PM
rrvball rrvball is offline
Newcomer
 
Join Date: Dec 2006
Location: Colorado
Posts: 12
Default

I have not worked on that problem. I was doing some experimenting and finally gave up the project because I couldn't get this to work.

My understanding was that the cookies would be sent with the request as long as the cookieContainer contained cookies for the corresponding URL. So, you only need one cookieContainer for your whole application (or that's how I understood it).

If you find a solution, I'd appreciate knowing how you got it to work.
Reply With Quote
  #9  
Old 02-09-2009, 09:35 PM
munishsethi777 munishsethi777 is offline
Newcomer
 
Join Date: Feb 2009
Posts: 14
Default

So u gave up on that.. thats sad..
yes this happens when u see all doors of assistance closed...
yes i have come to kno looking at all the posts that cookieContainer needs to be set and there are a few way for doing the same.

request2.cookieContainer = response2.cookieContainer.

or

Code:
       ckCookieCollection  = response.getCookies
       If Not ckCookieCollection Is Nothing Then
            Dim strCookTemp As String = ""
            Dim iCookieIndex As Integer = 0

            For Each ckCookie As Cookie In ckCookieCollection
                strCookTemp += ckCookie.Name + "=" + ckCookie.Value
                iCookieIndex += 1
                If iCookieIndex < ckCookieCollection.Count Then strCookTemp += "; "
            Next
            webrequest.Headers.Add("Cookie", strCookTemp)
        End If
and some more other solutions to set the cookies.

Now i tried these all ways, bust still cant find any luck yet...
If u are interested in this issue once again, i am posting my development progress.. Try to implement it and lets solve this issue and help rests on the blog.

i think leaders or admins of this blogs are either not that technical to solve this one, or they are not bothering ppl coming in and asking help..

let me attach c# project here...main class to be used is cookingRequest.cs

there are couple of other classes that i have been trying to work around with.

all the best.. give it a try man.
Attached Files
File Type: zip PostSubmitter.zip (113.2 KB, 24 views)
Reply With Quote
  #10  
Old 02-09-2009, 11:56 PM
munishsethi777 munishsethi777 is offline
Newcomer
 
Join Date: Feb 2009
Posts: 14
Default

rrv .. kindly let me kno where we can chat?

my yahoo is munishsethi777
gmail is munishsethi777
msn munishsethi777
Reply With Quote
  #11  
Old 02-10-2009, 09:56 PM
munishsethi777 munishsethi777 is offline
Newcomer
 
Join Date: Feb 2009
Posts: 14
Default

hi rrv..i got it working.

actually good news is my target is achieved, i can login using vbform.. but bad news is i hav to use a third party http://www.chilkatsoft.com/HttpDotNet.asp for the same..
But anyways.. i got it working and thats wat will count at last..
I am going to fiddle with this DLL downloaded and will try to see whats actually this DLL doing, afterall he has done some code in this DLL even..

as i get some hint, i will post back to u again...
Reply With Quote
  #12  
Old 02-16-2009, 10:25 AM
rrvball rrvball is offline
Newcomer
 
Join Date: Dec 2006
Location: Colorado
Posts: 12
Default

Congrats on getting it working. Although, using a 3rd party piece of software isn't what I was hoping for.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
HttpWebRequest not sending cookies?
HttpWebRequest not sending cookies?
HttpWebRequest not sending cookies? HttpWebRequest not sending cookies?
HttpWebRequest not sending cookies?
HttpWebRequest not sending cookies?
HttpWebRequest not sending cookies? HttpWebRequest not sending cookies? HttpWebRequest not sending cookies? HttpWebRequest not sending cookies? HttpWebRequest not sending cookies? HttpWebRequest not sending cookies? HttpWebRequest not sending cookies?
HttpWebRequest not sending cookies?
HttpWebRequest not sending cookies?
 
HttpWebRequest not sending cookies?
HttpWebRequest not sending cookies?
 
-->