473,396 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

HttpWebRequest.GetResponse() problem

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?...download/lotto
(in Dutch) or http://www.lotto.be/pages/show.aspx?...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(IAsyncRes ult
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.GetResponseS tream)

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, "&ctl2:DownloadButton=Downloaden")
End Function
Nov 20 '05 #1
0 1357

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Brian Brown | last post by:
I have code which works as an asp.net page that posts an xml file to web page and gets a response back. When the the calls GetResponse() it goes into the page it's posting to to and works fine....
10
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is...
1
by: Joey Chömpff | last post by:
Hello, When I'm uploading an file to a JRun WebServer (third party) with a Windows Forms application, l always get an TimeOut while uploading, all other request who doesn't request an post are...
1
by: Jeff B | last post by:
I'm trying to create a simple screen scraping application and I kept getting a System.Net.WebException thrown back with a message of "The operation has timed-out." At first I thought it was some...
2
by: Steve Richter | last post by:
I have a page that uses simple HTTP GET to do an ISBN lookup via Amazon.com. The page works when I run it from //localhost. But I have moved it to my godaddy.com shared hoster site, and I get...
5
by: bg | last post by:
Hi all. I 'm trying to automate some of my workflow, by doing some programmatic "POST"s to an internal webserver via HttpWebRequest. The Code is your usual straight forward type of thing. ...
5
by: mr.newsgroupguy | last post by:
I am working in C# .NET 1.1. My app has a button on its main form that checks to see if it has access to a file on our server, just an XML file. On our server we are running W2K IIS with a...
7
by: Marc Bartsch | last post by:
Hi, I have a background worker in my C# app that makes a synchronous HttpWebRequest.GetResponse() call. The idea is to POST a file to a server on the internet. When I call HttpWebRequest.Abort()...
2
by: =?Utf-8?B?TGFycnlLdXBlcm1hbg==?= | last post by:
Our WebDev team seems to have found a problem that exposes a bug in .NET 2.0. This problem can be shown when trying to access a WebService using SSL and through a proxy server after using the...
4
by: =?Utf-8?B?SmltIE93ZW4=?= | last post by:
Hi, I've run into a set of errors I don't understand coming back from HttpWebRequest.GetResponse, In one case, null is returned from the request without an Exception and in the other the request...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.