HttpWebResponse Question

calpha

Member
Joined
Nov 13, 2003
Messages
7
Im trying to get a page-scanner going-----to get to the page in question, I first have to login-----in other words----i have to have the cookies saying Ive logged in.

Ive got this part done.

Next, I take the cookies, and would then need to do a get-----problem is, when I set the method="GET" it wont let me set the cookies (cannot send a content body with this verb type).

Below is my code-----its dirty but its just a test so far.

When I change the method to post, it does snag the page, but it snags the top page instead of the page specified by the URL......so Iim not sure how to proceed. Ive got a working version in Winsock & VB6---but its a little easier b/c its all just sending text to the server. Im trying to make this a web service.

Code:
Dim url As String = "http://login.yahoo.com"
        Dim result As String = ""
        Dim strPost As String = "login=<sniped>&passwd=<sniped>"
        Dim myWriter As StreamWriter

        Dim objRequest As HttpWebRequest = WebRequest.Create(url)
        post the page
        objRequest.Method = "POST"
        objRequest.ContentLength = strPost.Length
        objRequest.ContentType = "application/x-www-form-urlencoded"
        make sure to init the cookies collection so it recieves the cookies set by a sucessful login
        objRequest.CookieContainer = New CookieContainer
        now get teh response stream
        Try
            myWriter = New StreamWriter(objRequest.GetRequestStream())
            myWriter.Write(strPost)
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            myWriter.Close()
        End Try

        Dim objResponse As HttpWebResponse = objRequest.GetResponse()
        Dim sr As StreamReader
        sr = New StreamReader(objResponse.GetResponseStream())
        result = sr.ReadToEnd()
        sr.Close()

        url = "http://basketball.fantasysports.yahoo.com/nba?page=leaguehome&lid=<sniped>"
        result = ""
        Dim myWriter2 As StreamWriter
        Dim objRequest2 As HttpWebRequest = WebRequest.Create(url)
        With objRequest2
            .method = get returns error----b/c i cant set cookies
            .Method = "POST"
            set the cookies based on the login above
            .CookieContainer = objRequest.CookieContainer
            Try
                myWriter2 = New StreamWriter(.GetRequestStream())
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                myWriter2.Close()
            End Try
            Dim objresponse2 As HttpWebResponse = .GetResponse()
            Dim sr2 As StreamReader
            sr2 = New StreamReader(objresponse2.GetResponseStream())
            Dim result2 As String = sr2.ReadToEnd()
            sr.Close()
            Me.TextBox3.Text = result2

        End With
 
Found the answer to my own question:

I found a way to cheat it using the "PROPFIND" as the mehtod instead of "GET". THe result is still like a GeT though.

Heres my code:
Code:
Function GetRequestStream(ByVal sURL As String) As String
        Dim objRequest As HttpWebRequest = WebRequest.Create(sURL)
        Dim objResponse As HttpWebResponse
        Dim myWriter As StreamWriter
        Dim sr As StreamReader
        Dim objCookie As CookieContainer
        Dim strRead As String
        With objRequest
            .Method = "PROPFIND"
            .CookieContainer = Me.ckCont
            Try
                myWriter = New StreamWriter(.GetRequestStream())
            Catch ex As Exception
                RaiseEvent ErrorMessage(ex)
                Return Nothing
            End Try
        End With

        Try
            objResponse = objRequest.GetResponse()
            sr = New StreamReader(objResponse.GetResponseStream())
            strRead = sr.ReadToEnd()
            sr.Close()
            objResponse.Close()
            Return strRead

        Catch ex As Exception
            RaiseEvent ErrorMessage(ex)
            Return Nothing
        End Try

    End Function
The cookie variable I get from another similar function, but its only purpose is to login and get the cookei from the cookiecontainer and set the cookiecontainer class variable.
 
Back
Top