 |
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
shhac Expert Cheater
Reputation: 0
Joined: 30 Oct 2007 Posts: 108
|
Posted: Thu Apr 02, 2009 7:58 am Post subject: VB.NET HttpWebRequest/DataStreams Credentials |
|
|
Edit: Original message + code after last break
Somebody test this for me?
example usage:
Code: | Dim Wrapper As New HTTPWrapper, _
page As String = vbNullString
Wrapper.newSession
If Wrapper.simpleGET("http://google.com") Then
page = Wrapper.HTMLResponse
Else
MsgBox(Wrapper.StatusCode)
End If |
But what I want checked is something that needs cookies
Code: | Public Class HTTPWrapper
'HTTPWrapper Class by shhac
'Version 0.9 // 2009-04-15
'Sessions - Not checked cookies work yet
Public Status As String = vbNullString, _
StatusCode As Integer = 0, _
Credentials As Net.NetworkCredential = Net.CredentialCache.DefaultCredentials, _
HeadersSent As New Net.WebHeaderCollection, _
HeadersResponse As New Net.WebHeaderCollection, _
ResponseURI As System.Uri = Nothing, _
Cookies As New Net.CookieContainer, _
HTMLResponse As String = vbNullString, _
UserAgent As String = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8 (.NET CLR 3.5.30729)"
Private inSession = False
Public Function flush() As Boolean
Status = vbNullString
StatusCode = 0
Credentials = Net.CredentialCache.DefaultCredentials
HeadersSent.Clear()
HeadersResponse.Clear()
ResponseURI = Nothing
Cookies = Nothing
Cookies = New Net.CookieContainer
HTMLResponse = vbNullString
Return True
End Function
Public Function endSession() As Boolean
flush()
inSession = False
Return True
End Function
Public Function newSession() As Boolean
flush()
inSession = True
Return True
End Function
Public Function makeCookie(ByVal URI As String, ByVal name As String, ByVal val As String, Optional ByVal secure As Boolean = False, Optional ByVal expires As Date = Nothing) As Net.Cookie
Dim cookieOut As New Net.Cookie
With cookieOut
.Domain = URI
.Name = name
.Value = val
.Secure = secure
If Not expires = Nothing Then
.Expires = expires
End If
End With
Return cookieOut
End Function
Private Function strHeaderToCookieCol(ByVal Header As String, ByVal Host As String) As Net.CookieCollection
Dim cookieCol As New Net.CookieCollection, _
strHeader As String = Header.Replace(vbCr, vbNullChar).Replace(vbLf, vbNullChar), _
strCookies As String() = strHeader.Split(","), _
i As Integer = 0
While i < strCookies.Length
If strCookies(i).IndexOf("expires=", StringComparison.OrdinalIgnoreCase) > 0 Then
cookieCol.Add(strToCookie(strCookies(i) + "," + strCookies(i + 1), Host))
i += 2
Else
cookieCol.Add(strToCookie(strCookies(i), Host))
i += 1
End If
End While
Return cookieCol
End Function
Private Function strToCookie(ByVal strCookie As String, ByVal Host As String) As Net.Cookie
If Not strCookie = vbNullString Then
Dim cookie As New Net.Cookie, _
cookieParts As String() = strCookie.Split(";"), _
equalSign As Integer = 0, _
strTmp As String = vbNullString
For j As Integer = 0 To cookieParts.Count - 1
If Not cookieParts(j) = vbNullString Then
If j = 0 Then
equalSign = cookieParts(j).IndexOf("=")
cookie.Name = cookieParts(j).Substring(0, equalSign)
cookie.Value = cookieParts(j).Substring(equalSign + 1)
ElseIf cookieParts(j).IndexOf("path=", StringComparison.OrdinalIgnoreCase) >= 0 Then
equalSign = cookieParts(j).IndexOf("=")
strTmp = cookieParts(j).Substring(equalSign + 1)
If Not strTmp = vbNullString Then
cookie.Path = strTmp
Else
cookie.Path = "/"
End If
ElseIf cookieParts(j).IndexOf("domain=", StringComparison.OrdinalIgnoreCase) >= 0 Then
equalSign = cookieParts(j).IndexOf("=")
strTmp = cookieParts(j).Substring(equalSign + 1)
If Not strTmp = vbNullString Then
cookie.Domain = strTmp
Else
cookie.Domain = Host
End If
ElseIf cookieParts(j).IndexOf("expires=", StringComparison.OrdinalIgnoreCase) >= 0 Then
equalSign = cookieParts(j).IndexOf("=")
strTmp = cookieParts(j).Substring(equalSign + 1)
If Not strTmp = vbNullString Then
cookie.Expires = strTmp
End If
End If
End If
Next j
If cookie.Path = vbNullString Then
cookie.Path = "/"
End If
If cookie.Domain = vbNullString Then
cookie.Domain = Host
End If
Return cookie
Else
Return Nothing
End If
End Function
Public Function cookiesFromHeaders(ByVal Headers As Net.WebHeaderCollection, ByVal Host As String) As Net.CookieCollection
If Headers.AllKeys.Contains("Set-Cookie") Then
Return strHeaderToCookieCol(Headers.Item("Set-Cookie"), Host)
Else
Return Nothing
End If
End Function
Public Function makeCookieCollection(ByVal Cookies() As Net.Cookie) As Net.CookieCollection
Dim cookieCol As New Net.CookieCollection
For Each cookie As Net.Cookie In Cookies
cookieCol.Add(cookie)
Next cookie
Return cookieCol
End Function
Public Function httpPOST(ByVal URL As String, Optional ByVal postData As String = vbNullString, Optional ByVal refURL As String = vbNullString, Optional ByVal CookiesIn As Net.CookieCollection = Nothing, Optional ByVal user As String = vbNullString, Optional ByVal pass As String = vbNullString, Optional ByVal customHeaders As String() = Nothing) As Boolean
Dim request As Net.HttpWebRequest = CType(Net.HttpWebRequest.Create(URL), Net.HttpWebRequest), _
ret As Boolean = True
With request
.UserAgent = UserAgent
If Not user Is vbNullString AndAlso Not pass Is vbNullString Then
.Credentials = New Net.NetworkCredential(user, pass)
Credentials = .Credentials
ElseIf inSession Then
Credentials = .Credentials
Else
.Credentials = Net.CredentialCache.DefaultCredentials
End If
If Not refURL Is vbNullString Then
.Referer = refURL
ElseIf inSession Then
If Not ResponseURI = Nothing Then
.Referer = ResponseURI.AbsolutePath.ToString
End If
End If
If inSession Then
If Not CookiesIn Is Nothing Then
Cookies.Add(CookiesIn)
End If
.CookieContainer = Cookies
Else
Cookies = Nothing
If Not CookiesIn Is Nothing Then
Cookies = New Net.CookieContainer
Cookies.Add(CookiesIn)
.CookieContainer = Cookies
End If
End If
If Not customHeaders Is Nothing Then
For Each newHeader As String In customHeaders
.Headers.Add(newHeader)
Next newHeader
End If
HeadersSent = .Headers
End With
If Not postData = vbNullString Then
Dim postBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(postData)
With request
.Method = "POST"
.ContentType = "application/x-www-form-urlencoded"
.ContentLength = postBytes.Length
End With
Dim dataStreamOut As System.IO.Stream = request.GetRequestStream()
With dataStreamOut
.Write(postBytes, 0, postBytes.Length)
.Close()
End With
dataStreamOut = Nothing
End If
Dim response As Net.HttpWebResponse = Nothing
Try
response = request.GetResponse()
Catch Ex As System.Net.WebException
If Ex.Status = Net.WebExceptionStatus.ProtocolError Then
response = Ex.Response
ret = False
Else
Status = Ex.Message
StatusCode = 0
HTMLResponse = vbNullString
request = Nothing
Return False
End If
End Try
With response
Status = .StatusDescription
StatusCode = .StatusCode
HeadersResponse = .Headers
ResponseURI = .ResponseUri
End With
If inSession Then
Dim newCookies As Net.CookieCollection = cookiesFromHeaders(HeadersResponse, ResponseURI.Host)
If Not newCookies Is Nothing Then
Cookies.Add(newCookies)
End If
End If
Dim dataStream As System.IO.Stream = response.GetResponseStream(), _
reader As New System.IO.StreamReader(dataStream), _
responseString As String = reader.ReadToEnd()
reader.Close()
dataStream.Close()
response.Close()
HTMLResponse = responseString
request = Nothing
response = Nothing
dataStream = Nothing
reader = Nothing
Return ret
End Function
Public Function httpGET(ByVal URL As String, Optional ByVal refURL As String = vbNullString, Optional ByVal CookiesIn As Net.CookieCollection = Nothing, Optional ByVal user As String = vbNullString, Optional ByVal pass As String = vbNullString, Optional ByVal customHeaders() As String = Nothing) As Boolean
Return httpPOST(URL, vbNullString, refURL, CookiesIn, user, pass, customHeaders)
End Function
Public Function simplePOST(ByVal URL As String, ByVal postData As String(), Optional ByVal referer As String = vbNullString) As Boolean
Dim postDataString As String = vbNullString
For Each part As String In postData
postDataString = postDataString & "&" & part
Next part
postDataString = postDataString.Trim("&")
Return httpPOST(URL, postDataString, referer)
End Function
Public Function simplePOSTwithCookies(ByVal URL As String, ByVal postData As String(), ByVal cookies As Net.Cookie(), Optional ByVal referer As String = vbNullString) As Boolean
Dim cookieCol As New Net.CookieCollection, _
postDataString As String = vbNullString
For Each cookie As Net.Cookie In cookies
cookieCol.Add(cookie)
Next cookie
For Each part As String In postData
postDataString = postDataString & "&" & part.Replace("&", "&")
Next part
postDataString = postDataString.Trim("&")
Return httpPOST(URL, postDataString, referer, cookieCol)
End Function
Public Function simpleGET(ByVal URL As String, Optional ByVal referer As String = vbNullString) As Boolean
Return httpGET(URL, referer)
End Function
Public Function simpleGETwithCookies(ByVal URL As String, ByVal cookies As Net.Cookie(), Optional ByVal referer As String = vbNullString) As Boolean
Dim cookieCol As New Net.CookieCollection
For Each cookie As Net.Cookie In cookies
cookieCol.Add(cookie)
Next cookie
Return httpGET(URL, referer, cookieCol)
End Function
End Class |
--------------------------------------------------
What different ways are cookies made? Just from the Set-Cookie Header? Multiple cookies from same website?
My current code
Code: | Public Class HTTPWrapper
'HTTPWrapper Class by shhac
'Version 0.8 // 2009-04-13
'Sessions - Cookies not being made / check Set-Cookie header
Public Status As String = vbNullString, _
StatusCode As Integer = 0, _
Credentials As Net.NetworkCredential = Net.CredentialCache.DefaultCredentials, _
HeadersSent As New Net.WebHeaderCollection, _
HeadersResponse As New Net.WebHeaderCollection, _
Cookies As New Net.CookieContainer, _
HTMLResponse As String = vbNullString
Private inSession = False, _
lastPage As String = vbNullString
Public Function flush() As Boolean
Status = vbNullString
StatusCode = 0
Credentials = Net.CredentialCache.DefaultCredentials
HeadersSent.Clear()
HeadersResponse.Clear()
Cookies = Nothing
Cookies = New Net.CookieContainer
HTMLResponse = vbNullString
lastPage = vbNullString
Return True
End Function
Public Function endSession() As Boolean
flush()
inSession = False
Return True
End Function
Public Function newSession() As Boolean
flush()
inSession = True
Return True
End Function
Public Function makeCookie(ByVal URI As String, ByVal name As String, ByVal val As String, Optional ByVal secure As Boolean = False, Optional ByVal expires As Date = Nothing) As Net.Cookie
Dim cookieOut As New Net.Cookie
With cookieOut
.Domain = URI
.Name = name
.Value = val
.Secure = secure
If Not expires = Nothing Then
.Expires = expires
End If
End With
Return cookieOut
End Function
Public Function makeCookieCollection(ByVal Cookies() As Net.Cookie) As Net.CookieCollection
Dim cookieCol As New Net.CookieCollection
For Each cookie As Net.Cookie In Cookies
cookieCol.Add(cookie)
Next cookie
Return cookieCol
End Function
Public Function httpPOST(ByVal URL As String, Optional ByVal postData As String = vbNullString, Optional ByVal refURL As String = vbNullString, Optional ByVal CookiesIn As Net.CookieCollection = Nothing, Optional ByVal user As String = vbNullString, Optional ByVal pass As String = vbNullString, Optional ByVal customHeaders As String() = Nothing) As Boolean
Dim request As Net.HttpWebRequest = CType(Net.HttpWebRequest.Create(URL), Net.HttpWebRequest), _
ret As Boolean = True
With request
If Not user Is vbNullString AndAlso Not pass Is vbNullString Then
.Credentials = New Net.NetworkCredential(user, pass)
Credentials = .Credentials
ElseIf inSession Then
Credentials = .Credentials
Else
.Credentials = Net.CredentialCache.DefaultCredentials
End If
If Not refURL Is vbNullString Then
.Referer = refURL
ElseIf inSession Then
If Not lastPage Is vbNullString Then
.Referer = lastPage
End If
lastPage = URL
End If
If inSession Then
If Not CookiesIn Is Nothing Then
Cookies.Add(CookiesIn)
End If
.CookieContainer = Cookies
.Headers.Add(HeadersSent)
If Not customHeaders Is Nothing Then
For Each newHeader As String In customHeaders
.Headers.Add(newHeader)
Next newHeader
End If
HeadersSent = .Headers
Else
Cookies = Nothing
If Not CookiesIn Is Nothing Then
Cookies = New Net.CookieContainer
Cookies.Add(CookiesIn)
.CookieContainer = Cookies
End If
If Not customHeaders Is Nothing Then
For Each newHeader As String In customHeaders
.Headers.Add(newHeader)
Next newHeader
End If
HeadersSent = .Headers
End If
End With
If Not postData = vbNullString Then
Dim postBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(postData)
With request
.Method = "POST"
.ContentType = "application/x-www-form-urlencoded"
.ContentLength = postBytes.Length
End With
Dim dataStreamOut As System.IO.Stream = request.GetRequestStream()
With dataStreamOut
.Write(postBytes, 0, postBytes.Length)
.Close()
End With
dataStreamOut = Nothing
End If
Dim response As Net.HttpWebResponse = Nothing
Try
response = request.GetResponse()
Catch Ex As System.Net.WebException
If Ex.Status = Net.WebExceptionStatus.ProtocolError Then
response = Ex.Response
ret = False
Else
Status = Ex.Message
StatusCode = 0
HTMLResponse = vbNullString
request = Nothing
Return False
End If
End Try
With response
Status = .StatusDescription
StatusCode = .StatusCode
HeadersResponse = .Headers
End With
Dim dataStream As System.IO.Stream = response.GetResponseStream(), _
reader As New System.IO.StreamReader(dataStream), _
responseString As String = reader.ReadToEnd()
reader.Close()
dataStream.Close()
response.Close()
HTMLResponse = responseString
request = Nothing
response = Nothing
dataStream = Nothing
reader = Nothing
Return ret
End Function
Public Function httpGET(ByVal URL As String, Optional ByVal refURL As String = vbNullString, Optional ByVal CookiesIn As Net.CookieCollection = Nothing, Optional ByVal user As String = vbNullString, Optional ByVal pass As String = vbNullString, Optional ByVal customHeaders() As String = Nothing) As Boolean
Return httpPOST(URL, vbNullString, refURL, CookiesIn, user, pass, customHeaders)
End Function
Public Function simplePOST(ByVal URL As String, ByVal postData As String(), Optional ByVal referer As String = vbNullString) As Boolean
Dim postDataString As String = vbNullString
For Each part As String In postData
postDataString = postDataString & "&" & part
Next part
postDataString = postDataString.Trim("&")
Return httpPOST(URL, postDataString, referer)
End Function
Public Function simplePOSTwithCookies(ByVal URL As String, ByVal postData As String(), ByVal cookies As Net.Cookie(), Optional ByVal referer As String = vbNullString) As Boolean
Dim cookieCol As New Net.CookieCollection, _
postDataString As String = vbNullString
For Each cookie As Net.Cookie In cookies
cookieCol.Add(cookie)
Next cookie
For Each part As String In postData
postDataString = postDataString & "&" & part.Replace("&", "&")
Next part
postDataString = postDataString.Trim("&")
Return httpPOST(URL, postDataString, referer, cookieCol)
End Function
Public Function simpleGET(ByVal URL As String, Optional ByVal referer As String = vbNullString) As Boolean
Return httpGET(URL, referer)
End Function
Public Function simpleGETwithCookies(ByVal URL As String, ByVal cookies As Net.Cookie(), Optional ByVal referer As String = vbNullString) As Boolean
Dim cookieCol As New Net.CookieCollection
For Each cookie As Net.Cookie In cookies
cookieCol.Add(cookie)
Next cookie
Return httpGET(URL, referer, cookieCol)
End Function
End Class |
--------------------------------------------------
Thanks for all your help guys. </sarcasm>
Got the error stuff done, now I might try taking out the repeated bits of the code as private functions...
Code: | Public Class HTTPWrapper
'HTTPWrapper Class by shhac
'Version 0.7 // 2009-04-12
'Credential problems / re-use rather than always new (Credentials is currently not used, only set)
Public Status As String = vbNullString, _
StatusCode As Integer = 0, _
Credentials As Net.NetworkCredential = Net.CredentialCache.DefaultCredentials, _
Headers As New Net.WebHeaderCollection, _
Cookies As New Net.CookieCollection, _
HTMLResponse As String = vbNullString
Public Function newSession() As Boolean
Status = vbNullString
StatusCode = 0
Credentials = Net.CredentialCache.DefaultCredentials
Headers.Clear()
Cookies = New Net.CookieCollection
HTMLResponse = vbNullString
Return True
End Function
Public Function makeCookie(ByVal URI As String, ByVal name As String, ByVal val As String, Optional ByVal secure As Boolean = False, Optional ByVal expires As Date = Nothing) As Net.Cookie
Dim cookieOut As New Net.Cookie
With cookieOut
.Domain = URI
.Name = name
.Value = val
.Secure = secure
If Not expires = Nothing Then
.Expires = expires
End If
End With
Return cookieOut
End Function
Public Function httpPOST(ByVal URL As String, Optional ByVal postData As String = vbNullString, Optional ByVal refURL As String = vbNullString, Optional ByVal CookiesIn As Net.CookieCollection = Nothing, Optional ByVal user As String = vbNullString, Optional ByVal pass As String = vbNullString, Optional ByVal customHeaders As String() = Nothing) As Boolean
Dim request As Net.HttpWebRequest = CType(Net.HttpWebRequest.Create(URL), Net.HttpWebRequest), _
ret As Boolean = True
With request
If Not user Is vbNullString And Not pass Is vbNullString Then
.Credentials = New Net.NetworkCredential(user, pass)
Credentials = .Credentials
Else
.Credentials = Net.CredentialCache.DefaultCredentials
End If
If Not refURL Is vbNullString Then
.Referer = refURL
End If
If Not CookiesIn Is Nothing Then
Dim newContainer As New Net.CookieContainer
newContainer.Add(CookiesIn)
.CookieContainer = newContainer
End If
If Not customHeaders Is Nothing Then
For Each newHeader As String In customHeaders
.Headers.Add(newHeader)
Next newHeader
End If
End With
If Not postData = vbNullString Then
Dim postBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(postData)
With request
.Method = "POST"
.ContentType = "application/x-www-form-urlencoded"
.ContentLength = postBytes.Length
End With
Dim dataStreamOut As System.IO.Stream = request.GetRequestStream()
With dataStreamOut
.Write(postBytes, 0, postBytes.Length)
.Close()
End With
dataStreamOut = Nothing
End If
Dim response As Net.HttpWebResponse = Nothing
Try
response = request.GetResponse()
Catch Ex As System.Net.WebException
If Ex.Status = Net.WebExceptionStatus.ProtocolError Then
response = Ex.Response
ret = False
Else
Status = Ex.Message
StatusCode = 0
HTMLResponse = vbNullString
request = Nothing
Return False
End If
End Try
With response
Status = .StatusDescription
StatusCode = .StatusCode
Headers = .Headers
Cookies = .Cookies
End With
Dim dataStream As System.IO.Stream = response.GetResponseStream(), _
reader As New System.IO.StreamReader(dataStream), _
responseString As String = reader.ReadToEnd()
reader.Close()
dataStream.Close()
response.Close()
HTMLResponse = responseString
request = Nothing
response = Nothing
dataStream = Nothing
reader = Nothing
Return ret
End Function
Public Function httpGET(ByVal URL As String, Optional ByVal refURL As String = vbNullString, Optional ByVal CookiesIn As Net.CookieCollection = Nothing, Optional ByVal user As String = vbNullString, Optional ByVal pass As String = vbNullString, Optional ByVal customHeaders() As String = Nothing) As Boolean
Return httpPOST(URL, vbNullString, refURL, CookiesIn, user, pass, customHeaders)
End Function
Public Function simplePOST(ByVal URL As String, ByVal postData As String(), Optional ByVal referer As String = vbNullString) As Boolean
Dim postDataString As String = vbNullString
For Each part As String In postData
postDataString = postDataString & "&" & part
Next part
postDataString = postDataString.Trim("&")
Return httpPOST(URL, postDataString, referer)
End Function
Public Function simplePOSTwithCookies(ByVal URL As String, ByVal postData As String(), ByVal cookies As Net.Cookie(), Optional ByVal referer As String = vbNullString) As Boolean
Dim cookieCol As New Net.CookieCollection, _
postDataString As String = vbNullString
For Each cookie As Net.Cookie In cookies
cookieCol.Add(cookie)
Next cookie
For Each part As String In postData
postDataString = postDataString & "&" & part.Replace("&", "&")
Next part
postDataString = postDataString.Trim("&")
Return httpPOST(URL, postDataString, referer, cookieCol)
End Function
Public Function simpleGET(ByVal URL As String, Optional ByVal referer As String = vbNullString) As Boolean
Return httpGET(URL, referer)
End Function
Public Function simpleGETwithCookies(ByVal URL As String, ByVal cookies As Net.Cookie(), Optional ByVal referer As String = vbNullString) As Boolean
Dim cookieCol As New Net.CookieCollection
For Each cookie As Net.Cookie In cookies
cookieCol.Add(cookie)
Next cookie
Return httpGET(URL, referer, cookieCol)
End Function
End Class |
--------------------------------------------------
In the two functions httpPOST and httpGET below, I want to be able to remove the "try", but at the moment, if I do and the credentials were wrong i.e. I get a 403 error, it causes it to crash. So, basically, I want to know how to modify my code to check the credentials (or whatever else may cause an error). Thanks for your help.
(and to leechers, yes this code will work as it is if you want to use it)
Code: | Public Class HTTPWrapper
'HTTPWrapper Class by shhac
'Version 0.5 // 2009-04-02
'Credential problems / check that you don't get a 403 / re-use rather than always new (Credentials is currently not used, only set)
Public Status As String = vbNullString, _
StatusCode As Integer = 0, _
Credentials As Net.NetworkCredential = Net.CredentialCache.DefaultCredentials, _
Headers As New Net.WebHeaderCollection, _
Cookies As New Net.CookieCollection, _
HTMLResponse As String = vbNullString
Public Function newSession() As Boolean
Status = vbNullString
StatusCode = 0
Credentials = Net.CredentialCache.DefaultCredentials
Headers.Clear()
Cookies = New Net.CookieCollection
HTMLResponse = vbNullString
Return True
End Function
Public Function makeCookie(ByVal URI As String, ByVal name As String, ByVal val As String, Optional ByVal secure As Boolean = False, Optional ByVal expires As Date = Nothing) As Net.Cookie
Dim cookieOut As New Net.Cookie
With cookieOut
.Domain = URI
.Name = name
.Value = val
.Secure = secure
If Not expires = Nothing Then
.Expires = expires
End If
End With
Return cookieOut
End Function
Public Function httpPOST(ByVal URL As String, Optional ByVal postData As String = vbNullString, Optional ByVal refURL As String = vbNullString, Optional ByVal CookiesIn As Net.CookieCollection = Nothing, Optional ByVal user As String = vbNullString, Optional ByVal pass As String = vbNullString, Optional ByVal customHeaders As String() = Nothing) As Boolean
If postData Is vbNullString Then
Return httpGET(URL, refURL, Cookies, user, pass, customHeaders)
Else
Dim request As Net.HttpWebRequest = CType(Net.HttpWebRequest.Create(URL), Net.HttpWebRequest), _
postBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(postData)
With request
If Not user Is vbNullString And Not pass Is vbNullString Then
.Credentials = New Net.NetworkCredential(user, pass)
Credentials = .Credentials
Else
.Credentials = Net.CredentialCache.DefaultCredentials
End If
.Method = "POST"
.ContentType = "application/x-www-form-urlencoded"
.ContentLength = postBytes.Length
If Not refURL Is vbNullString Then
.Referer = refURL
End If
If Not CookiesIn Is Nothing Then
Dim newContainer As New Net.CookieContainer
newContainer.Add(CookiesIn)
.CookieContainer = newContainer
End If
If Not customHeaders Is Nothing Then
For Each newHeader As String In customHeaders
.Headers.Add(newHeader)
Next newHeader
End If
End With
Try
Dim dataStream As System.IO.Stream = request.GetRequestStream()
With dataStream
.Write(postBytes, 0, postBytes.Length)
.Close()
End With
Dim response As Net.WebResponse = request.GetResponse()
Status = CType(response, Net.HttpWebResponse).StatusDescription
StatusCode = CType(response, Net.HttpWebResponse).StatusCode
Headers = CType(response, Net.HttpWebResponse).Headers
Cookies = CType(response, Net.HttpWebResponse).Cookies
dataStream = response.GetResponseStream()
Dim reader As New System.IO.StreamReader(dataStream)
Dim responseString As String = reader.ReadToEnd()
reader.Close()
dataStream.Close()
response.Close()
HTMLResponse = responseString
Return True
Catch ex As Exception
Status = CType(ex.Message, String)
Return False
End Try
End If
End Function
Public Function httpGET(ByVal URL As String, Optional ByVal refURL As String = vbNullString, Optional ByVal CookiesIn As Net.CookieCollection = Nothing, Optional ByVal user As String = vbNullString, Optional ByVal pass As String = vbNullString, Optional ByVal customHeaders() As String = Nothing) As Boolean
Dim request As Net.HttpWebRequest = CType(Net.HttpWebRequest.Create(URL), Net.HttpWebRequest)
With request
If Not user Is vbNullString And Not pass Is vbNullString Then
.Credentials = New Net.NetworkCredential(user, pass)
Credentials = .Credentials
Else
.Credentials = Net.CredentialCache.DefaultCredentials
End If
If Not refURL Is vbNullString Then
.Referer = refURL
End If
If Not CookiesIn Is Nothing Then
Dim newContainer As New Net.CookieContainer
newContainer.Add(CookiesIn)
.CookieContainer = newContainer
End If
If Not customHeaders Is Nothing Then
For Each newHeader As String In customHeaders
.Headers.Add(newHeader)
Next newHeader
End If
End With
Try
Dim response As Net.WebResponse = request.GetResponse()
Status = CType(response, Net.HttpWebResponse).StatusDescription
StatusCode = CType(response, Net.HttpWebResponse).StatusCode
Headers = CType(response, Net.HttpWebResponse).Headers
Dim dataStream As System.IO.Stream = response.GetResponseStream()
Dim reader As New System.IO.StreamReader(dataStream)
Dim responseString As String = reader.ReadToEnd()
reader.Close()
dataStream.Close()
response.Close()
HTMLResponse = responseString
Return True
Catch ex As Exception
Status = CType(ex.Message, String)
Return False
End Try
End Function
Public Function simplePOST(ByVal URL As String, ByVal postData As String(), Optional ByVal referer As String = vbNullString) As Boolean
Dim postDataString As String = vbNullString
For Each part As String In postData
postDataString = postDataString & "&" & part
Next part
postDataString = postDataString.Trim("&")
Return httpPOST(URL, postDataString, referer)
End Function
Public Function simplePOSTwithCookies(ByVal URL As String, ByVal postData As String(), ByVal cookies As Net.Cookie(), Optional ByVal referer As String = vbNullString) As Boolean
Dim cookieCol As New Net.CookieCollection, _
postDataString As String = vbNullString
For Each cookie As Net.Cookie In cookies
cookieCol.Add(cookie)
Next cookie
For Each part As String In postData
postDataString = postDataString & "&" & part.Replace("&", "&")
Next part
postDataString = postDataString.Trim("&")
Return httpPOST(URL, postDataString, referer, cookieCol)
End Function
Public Function simpleGET(ByVal URL As String, Optional ByVal referer As String = vbNullString) As Boolean
Return httpGET(URL, referer)
End Function
Public Function simpleGETwithCookies(ByVal URL As String, ByVal cookies As Net.Cookie(), Optional ByVal referer As String = vbNullString) As Boolean
Dim cookieCol As New Net.CookieCollection
For Each cookie As Net.Cookie In cookies
cookieCol.Add(cookie)
Next cookie
Return httpGET(URL, referer, cookieCol)
End Function
End Class |
|
|
Back to top |
|
 |
sansharam How do I cheat?
Reputation: 0
Joined: 06 Sep 2009 Posts: 3 Location: Bangalore
|
Posted: Sun Sep 06, 2009 12:25 am Post subject: |
|
|
Dear Mr shahc
I need your help to login to yahoo using vb.net form control...
Can you help me
Please
Sansharam
|
|
Back to top |
|
 |
shhac Expert Cheater
Reputation: 0
Joined: 30 Oct 2007 Posts: 108
|
Posted: Sun Sep 06, 2009 7:43 am Post subject: |
|
|
If you're using the class I wrote, just look at the yahoo login page source code to get the names of the fields and it would be something like
Code: | Dim Wrapper As New HTTPWrapper, _
email As String = "", _
pass As String = ""
Wrapper.newSession()
Wrapper.httpGET("https://login.yahoo.com/config/login_verify2")
If Wrapper.httpPOST("https://login.yahoo.com/config/login_verify2", ".tries=1&.src=ym&.slogin=" & email & "&passwd=" & pass & "hasMsgr=0&.done=http://yahoo.com") Then
'continue here
Else
MsgBox("Error: " & Wrapper.StatusCode)
End If |
But there is another field in the login that looks like this Code: | <input type="hidden" value="someHash.anotherHash" name=".challenge"/> | and I'm not sure about what you'd do.. maybe take it from the previous GET.
If you're not using my module then it is basically the same idea except the function names are a little different and the args might be in a different order.
|
|
Back to top |
|
 |
sansharam How do I cheat?
Reputation: 0
Joined: 06 Sep 2009 Posts: 3 Location: Bangalore
|
Posted: Sat Sep 12, 2009 8:43 am Post subject: This is to Download Finance data |
|
|
Hi
I am a Stock Market Technical Analyst
I just wanted to download Yahoo realtime data of stocks which requires login to yahoo ... and hold cookie ..
so I request you to help in that
if Login success generate cookie policy and based on that I have make a webrequest in yahoo finance and get data in ASCII(in .txt) which I can use in my charting software....
if I didnt login to yahoo.... morover if there is no cokie then the data will be 15 min delayed.... I can login through IE also but it wont looks gud ... so please
Thank you for your reply aand help....
Sansharam
|
|
Back to top |
|
 |
shhac Expert Cheater
Reputation: 0
Joined: 30 Oct 2007 Posts: 108
|
Posted: Sat Sep 12, 2009 10:25 am Post subject: |
|
|
If you're not using session functions made already, you have to pass the cookies returned from the previous page (the logged in page) into the next request (for where the stocks are). To do this you'd have to store them in a variable somewhere, then set them as the CookieContainer.
|
|
Back to top |
|
 |
sansharam How do I cheat?
Reputation: 0
Joined: 06 Sep 2009 Posts: 3 Location: Bangalore
|
Posted: Tue Oct 20, 2009 11:44 am Post subject: error |
|
|
Hi
I tried with your code but its giving status message as 200 for correct password and even for wrong password also
Please help me
my email id is sansharam at gmail dot com
|
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|