V
Veerle
Hi,
On the website of the Belgian lottery, you can download an excel sheet
with lottery results (the winning numbers) over the years and an excel
sheet with financial results (the winnings) over the year. The page is
http://www.lotto.be/pages/show.aspx?Culture=nl&pageid=results/download/lotto
(in Dutch) or http://www.lotto.be/pages/show.aspx?Culture=en&pageid=results/download/lotto
(in English)
I want to write a Windows Service in Visual Basic .Net that at regular
times, downloads these excel files for me with all the results
starting from januari 2004. So I guessed it would be sufficient to
simulate the posting of the form in my code:
(1) Retrieve the value of the hidden parameter __VIEWSTATE
(2) Construct the url
(3) Do the posting of the form
But then I get:
System.Net.WebException: The remote server returned an error: (400)
Bad Request.
at System.Net.HttpWebRequest.CheckFinalStatus()
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult
asyncResult)
at System.Net.HttpWebRequest.GetResponse()
Anyone has an idea what I'm doing wrong?
This is my code:
Private Sub DownloadFile()
Try
Dim viewstate As String = GetViewState()
Dim resultUrl As String = GetActionUrl(viewstate, True)
Dim moneyUrl As String = GetActionUrl(viewstate, False)
GetExcel(resultUrl)
GetExcel(moneyUrl)
Catch ex As WebException
Console.WriteLine(ex.ToString())
End Try
End Sub
Private Function GetExcel(ByVal url As String) As String
Dim objWebRequest As System.Net.HttpWebRequest
Dim objWebResponse As System.Net.HttpWebResponse
Dim streamReader As System.IO.StreamReader
Dim strHTML As String
objWebRequest = CType(System.Net.WebRequest.Create(url),
System.Net.HttpWebRequest)
objWebRequest.Method = "GET"
objWebResponse = CType(objWebRequest.GetResponse(),
System.Net.HttpWebResponse)
streamReader = New
System.IO.StreamReader(objWebResponse.GetResponseStream)
Console.WriteLine(streamReader.ReadToEnd)
streamReader.Close()
objWebResponse.Close()
objWebRequest.Abort()
End Function
Private Function GetViewState() As String
Dim myWebClient As New WebClient
Dim myDatabuffer As Byte() = myWebClient.DownloadData(formUrl)
Dim download As String =
Encoding.ASCII.GetString(myDatabuffer)
Dim pos1 As Integer = download.IndexOf("__VIEWSTATE") + 20
Dim pos2 As Integer = download.IndexOf("/>", pos1) - 2
GetViewState = HttpUtility.UrlEncode(download.Substring(pos1,
pos2 - pos1))
End Function
Private Function GetActionUrl(ByVal viewstate As String, ByVal
GetResults As Boolean) As String
Dim radio As String
If GetResults Then
radio = "Radio1"
Else
radio = "Radio2"
End If
Dim endDate As Date = Today
Dim startDate As Date = #1/1/2004#
GetActionUrl =
String.Concat("http://www.lotto.be/pages/show.aspx?Culture=nl&pageid=results/download/lotto&__VIEWSTATE=",
viewstate, "&ctl2:r1=", radio, "&ctl2:MinMonth=", startDate.Month,
"&_ctl2:MinYear=", startDate.Year, "&ctl2:MaxMonth=", endDate.Month,
"&_ctl2:MaxYear=", endDate.Year, "&ctl2ownloadButton=Downloaden")
End Function
On the website of the Belgian lottery, you can download an excel sheet
with lottery results (the winning numbers) over the years and an excel
sheet with financial results (the winnings) over the year. The page is
http://www.lotto.be/pages/show.aspx?Culture=nl&pageid=results/download/lotto
(in Dutch) or http://www.lotto.be/pages/show.aspx?Culture=en&pageid=results/download/lotto
(in English)
I want to write a Windows Service in Visual Basic .Net that at regular
times, downloads these excel files for me with all the results
starting from januari 2004. So I guessed it would be sufficient to
simulate the posting of the form in my code:
(1) Retrieve the value of the hidden parameter __VIEWSTATE
(2) Construct the url
(3) Do the posting of the form
But then I get:
System.Net.WebException: The remote server returned an error: (400)
Bad Request.
at System.Net.HttpWebRequest.CheckFinalStatus()
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult
asyncResult)
at System.Net.HttpWebRequest.GetResponse()
Anyone has an idea what I'm doing wrong?
This is my code:
Private Sub DownloadFile()
Try
Dim viewstate As String = GetViewState()
Dim resultUrl As String = GetActionUrl(viewstate, True)
Dim moneyUrl As String = GetActionUrl(viewstate, False)
GetExcel(resultUrl)
GetExcel(moneyUrl)
Catch ex As WebException
Console.WriteLine(ex.ToString())
End Try
End Sub
Private Function GetExcel(ByVal url As String) As String
Dim objWebRequest As System.Net.HttpWebRequest
Dim objWebResponse As System.Net.HttpWebResponse
Dim streamReader As System.IO.StreamReader
Dim strHTML As String
objWebRequest = CType(System.Net.WebRequest.Create(url),
System.Net.HttpWebRequest)
objWebRequest.Method = "GET"
objWebResponse = CType(objWebRequest.GetResponse(),
System.Net.HttpWebResponse)
streamReader = New
System.IO.StreamReader(objWebResponse.GetResponseStream)
Console.WriteLine(streamReader.ReadToEnd)
streamReader.Close()
objWebResponse.Close()
objWebRequest.Abort()
End Function
Private Function GetViewState() As String
Dim myWebClient As New WebClient
Dim myDatabuffer As Byte() = myWebClient.DownloadData(formUrl)
Dim download As String =
Encoding.ASCII.GetString(myDatabuffer)
Dim pos1 As Integer = download.IndexOf("__VIEWSTATE") + 20
Dim pos2 As Integer = download.IndexOf("/>", pos1) - 2
GetViewState = HttpUtility.UrlEncode(download.Substring(pos1,
pos2 - pos1))
End Function
Private Function GetActionUrl(ByVal viewstate As String, ByVal
GetResults As Boolean) As String
Dim radio As String
If GetResults Then
radio = "Radio1"
Else
radio = "Radio2"
End If
Dim endDate As Date = Today
Dim startDate As Date = #1/1/2004#
GetActionUrl =
String.Concat("http://www.lotto.be/pages/show.aspx?Culture=nl&pageid=results/download/lotto&__VIEWSTATE=",
viewstate, "&ctl2:r1=", radio, "&ctl2:MinMonth=", startDate.Month,
"&_ctl2:MinYear=", startDate.Year, "&ctl2:MaxMonth=", endDate.Month,
"&_ctl2:MaxYear=", endDate.Year, "&ctl2ownloadButton=Downloaden")
End Function