473,471 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Strange problem with HttpWebRequest

I have a website I am trying to download a file from. This website is
strange in the fact that there is no direct link to the URL of the
file. The way the site works is as follows:

1) Enter in search criteria
2) Click on Download button to go to download page
3) Click on Download button on Download page
4) Server sends 2 bits of data to a script. The data sent does not
include the query data. I'm assuming that the data is stored either in
a server side session variable, or server-side on a temporary database.
5) The script processes and initiates a file download. Inside any of
the web pages, there are NO direct links directly to a file to
download. Once the file is compiled, the webste starts streaming the
data to the standard IE file download dialog. I need to capture this
file that is downloaded into a stream of some sort, then save it out to
a text file. The exported data is a text file.

Here is the code I am trying to use:

<------ CODE -------->
Dim fileURL As New Uri("http://www.somesite.com")
Dim myFileWebRequest As WebRequest =
WebRequest.Create(fileURL)
myFileWebRequest.Method = WebRequestMethods.Http.Post
Dim buffer As Byte() =
Encoding.UTF8.GetBytes("type=normal&cmd=DOWNLOAD")

myFileWebRequest.ContentType =
"application/x-www-form-urlencoded"
myFileWebRequest.ContentLength = buffer.Length
myFileWebRequest.Headers.Add(HttpRequestHeader.Coo kie,
wb.Document.Cookie)
'myFileWebRequest.Headers.Add(HttpRequestHeader.Re ferer,
"http://www.somesite.com/resource/servlet/search")
'myFileWebRequest.Headers.Add(HttpRequestHeader.Ac cept,
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword, */*")

myFileWebRequest.Headers.Add(HttpRequestHeader.Acc eptLanguage, "en-us")

myFileWebRequest.Headers.Add(HttpRequestHeader.Acc eptEncoding, "gzip,
deflate")
'myFileWebRequest.Headers.Add(HttpRequestHeader.Us erAgent,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.1.4322; .NET CLR 2.0.50727)")
'myFileWebRequest.Headers.Add(HttpRequestHeader.Ho st,
"www.somesite.com")
'myFileWebRequest.Headers.Add(HttpRequestHeader.Co nnection,
"Keep-Alive")

myFileWebRequest.Headers.Add(HttpRequestHeader.Cac heControl,
"no-cache")

Dim myFileWebResponse As WebResponse =
CType(myFileWebRequest.GetResponse, HttpWebResponse)
Dim receivestream As Stream =
myFileWebResponse.GetResponseStream
Dim encode As Encoding =
System.Text.Encoding.GetEncoding("utf-8")
Dim readStream As New StreamReader(receivestream, encode)
Dim read(256) As Char
Dim count As Integer = readStream.Read(read, 0, 256)
Dim objWriter As New StreamWriter(sFileName)

While count 0
Dim str As New String(read, 0, count)

objWriter.Write(str)
count = readStream.Read(read, 0, 256)
End While
readStream.Close()
objWriter.Close()

myFileWebResponse.Close()

<--------- END CODE --------->

I am not getting ANY errors or exceptions, the program just hangs on
the following line:
Dim myFileWebResponse As WebResponse =
CType(myFileWebRequest.GetResponse, HttpWebResponse)

It will sit on this line forever if you let it. The other problem is
that the website denies GET requests. It only allows POST requests.
I've had a number of other programmers look at it, and they are just as
stumped as me. If anyone can help, it would be most appreciated!

Also, if you look in the above code, you will notice a large number of
lines commented out that are designed to set items in the header. If I
uncomment them, then the program hangs and tells me that I am not
assigning it correctly and I need to use the name parameter. If the
other header items that are uncomment work, then shouldn't the
commented out ones work the same?

Thanks for the help in advance,
Wade

Nov 29 '06 #1
1 1878
I have often found the Open Source Ethereal network analysis tool to be
helpful with this sort of thing:

http://www.ethereal.com/

--
HTH,

Kevin Spencer
Microsoft MVP
Logostician
http://unclechutney.blogspot.com

Parabola is a mate of plane.
"Blackstar" <ww******@hbm2.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
>I have a website I am trying to download a file from. This website is
strange in the fact that there is no direct link to the URL of the
file. The way the site works is as follows:

1) Enter in search criteria
2) Click on Download button to go to download page
3) Click on Download button on Download page
4) Server sends 2 bits of data to a script. The data sent does not
include the query data. I'm assuming that the data is stored either in
a server side session variable, or server-side on a temporary database.
5) The script processes and initiates a file download. Inside any of
the web pages, there are NO direct links directly to a file to
download. Once the file is compiled, the webste starts streaming the
data to the standard IE file download dialog. I need to capture this
file that is downloaded into a stream of some sort, then save it out to
a text file. The exported data is a text file.

Here is the code I am trying to use:

<------ CODE -------->
Dim fileURL As New Uri("http://www.somesite.com")
Dim myFileWebRequest As WebRequest =
WebRequest.Create(fileURL)
myFileWebRequest.Method = WebRequestMethods.Http.Post
Dim buffer As Byte() =
Encoding.UTF8.GetBytes("type=normal&cmd=DOWNLOAD")

myFileWebRequest.ContentType =
"application/x-www-form-urlencoded"
myFileWebRequest.ContentLength = buffer.Length
myFileWebRequest.Headers.Add(HttpRequestHeader.Coo kie,
wb.Document.Cookie)
'myFileWebRequest.Headers.Add(HttpRequestHeader.Re ferer,
"http://www.somesite.com/resource/servlet/search")
'myFileWebRequest.Headers.Add(HttpRequestHeader.Ac cept,
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword, */*")

myFileWebRequest.Headers.Add(HttpRequestHeader.Acc eptLanguage, "en-us")

myFileWebRequest.Headers.Add(HttpRequestHeader.Acc eptEncoding, "gzip,
deflate")
'myFileWebRequest.Headers.Add(HttpRequestHeader.Us erAgent,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.1.4322; .NET CLR 2.0.50727)")
'myFileWebRequest.Headers.Add(HttpRequestHeader.Ho st,
"www.somesite.com")
'myFileWebRequest.Headers.Add(HttpRequestHeader.Co nnection,
"Keep-Alive")

myFileWebRequest.Headers.Add(HttpRequestHeader.Cac heControl,
"no-cache")

Dim myFileWebResponse As WebResponse =
CType(myFileWebRequest.GetResponse, HttpWebResponse)
Dim receivestream As Stream =
myFileWebResponse.GetResponseStream
Dim encode As Encoding =
System.Text.Encoding.GetEncoding("utf-8")
Dim readStream As New StreamReader(receivestream, encode)
Dim read(256) As Char
Dim count As Integer = readStream.Read(read, 0, 256)
Dim objWriter As New StreamWriter(sFileName)

While count 0
Dim str As New String(read, 0, count)

objWriter.Write(str)
count = readStream.Read(read, 0, 256)
End While
readStream.Close()
objWriter.Close()

myFileWebResponse.Close()

<--------- END CODE --------->

I am not getting ANY errors or exceptions, the program just hangs on
the following line:
Dim myFileWebResponse As WebResponse =
CType(myFileWebRequest.GetResponse, HttpWebResponse)

It will sit on this line forever if you let it. The other problem is
that the website denies GET requests. It only allows POST requests.
I've had a number of other programmers look at it, and they are just as
stumped as me. If anyone can help, it would be most appreciated!

Also, if you look in the above code, you will notice a large number of
lines commented out that are designed to set items in the header. If I
uncomment them, then the program hangs and tells me that I am not
assigning it correctly and I need to use the name parameter. If the
other header items that are uncomment work, then shouldn't the
commented out ones work the same?

Thanks for the help in advance,
Wade

Nov 30 '06 #2

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

Similar topics

3
by: kajol | last post by:
Hi everyone I am trying to get the content of any webpage (URL) using XMLHTTP, and it is working fine for me, but suddenly I have got a URL "http://www.bizrate.com/" which is causing a system...
9
by: Mike Cronin via DotNetMonster.com | last post by:
Hi there, Can anyone tell me what level of encryption is used when making an HTTPS POST request through an instance of the System.Net.HttpWebRequest object? Thanks much in advance! Mike...
2
by: Jonathan Wax | last post by:
Hi, I spent the last week looking for an answer to the following question: How can you upload an xml file to an HTTPS server with a specific certificate. Basically doing the same as this html...
0
by: Rajiv Das | last post by:
Environment: WinXP SP2, .Net 2.0, VS 2005 -------------------------------------------------- This is the code chunk if (url.AbsoluteUri.StartsWith("http://")) { HttpWebRequest HttpWReq =...
0
by: jesper.hvid | last post by:
Hi. I've noticed, after moving some of our code to 2.0, that System.Net.WebRequest.Create(System.String) and System.Uri(System.String) no longer behave as they did in 1.1 framework. Example:...
2
by: peter | last post by:
Hi, I have very strange situation but first description ;) I have: 1) project in VB.NET, in this f.e. 1 function: Public Function Login(ByVal UserName As String, ByVal UserPassword As...
10
by: John Kraft | last post by:
Hello all, I'm experiencing some, imo, strange behavior with the StreamReader object I am using in the code below. Summary is that I am downloading a file from a website and saving it to disk...
17
by: Terry Olsen | last post by:
Given that variable dt = "3/31/2007", why does it produce the following exception on some machines? It works fine on my PC, but others have sent me this exception information because it threw up on...
1
by: tohnsm | last post by:
Hi. I have been trying to create a cookie container. But the cookie I have stored is not correct. Below is the code: request = CType(WebRequest.Create("....."), httpWebRequest) ...
1
by: Proogeren | last post by:
I have a problem with a httpwebrequest that I am creating. The request in itself looks correct but using fiddler I see that a www-authentication header is sent along as well. The code is pasted...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.