HttpRequest sample for downloading document in ASP.NET

J

jdhancock

Can someone give me an example of how I can execute a program on a
corporate server and then download the response to the user? I'm not
sure if I'm asking this right.
Here is the asp code used today. I am attempting to recreate in
ASP.NET.

Thanks in advance,
Clem
--------------------------------------------------------------------------------------------------------------------------------------
Set HttpObj = Server.CreateObject("WinHTTP.WinHTTPRequest.5.1")
HttpObj.setTimeouts 5000, 5000, 15000, 15000
HttpObj.open "GET", "http://" & serverpath & "/doc_qanda.exe?" &
Escape(doc_num) & ",1", False
HttpObj.send
html = HttpObj.responseText
pos = Instr(html, "window.location=""") + 17

If Not Response.IsClientConnected Then
Set HttpObj = Nothing
Response.End
End If

If pos > 17 Then
HttpObj.setTimeouts 5000, 5000, 15000, 15000
HttpObj.open "GET", Mid(html, pos, Instr(pos, html, """") - pos),
False
HttpObj.send
End If

BinaryData = HttpObj.responseBody
ContentType = HttpObj.getResponseHeader("Content-Type")
Set HttpObj = Nothing
If Not Response.IsClientConnected Then
Response.End
End If

Response.AddHeader "Content-Type", ContentType
Response.AddHeader "Content-Length", LenB(BinaryData)
Response.AddHeader "Content-Disposition", "attachment; filename=" &
Escape(handle)
Response.BinaryWrite BinaryData
Response.Flush
%>
--------------------------------------------------------------------------------------------------------------------------------------
 
J

jdhancock

Thank you for the quick response. I assume I should put my execution
statement ("http://" & serverpath & "/doc_qanda.exe?") in place of
"Invalid Site"? I'm not sure what they mean by invalid site? Why
would someone code an invalid site purposely?
Also, can I specify a "GET" vs. "POST"?
Thank you again!
 
K

Kevin Spencer

Come on, now. This was documentation on the WebException.Response property
of the WebException class. The code in it was an example of how to use the
WebException.Response property. It had legitimate code for getting a
Response from an HttpWebRequest, but using an invalid URL, in order to get
the WebException. The introductory text before the example read:

Remarks
Some Internet protocols, such as HTTP, return otherwise valid responses
indicating that an error has occurred at the protocol level. When the
response to an Internet request indicates an error, WebRequest.GetResponse
sets the Status property to WebExceptionStatus.ProtocolError and provides
the WebResponse containing the error message in the Response property of the
WebException that was thrown. The application can examine the WebResponse to
determine the actual error.

Example
[Visual Basic, C#, C++] The following example checks the Status property and
prints to the console the StatusCode and StatusDescription of the underlying
HttpWebResponse instance.

You were supposed to read the code, read the documentation, derive the
correct code for getting a Response from an HttpWebRequest, note the
location in the .Net Framework class library documentation for future use,
and adapt the code to your needs.

For future use, the documentation for the entire CLR class library is in
that reference, and it should be very useful to you when this sort of
situation arises.

The full documentation for the HttpWebRequest class can be found at:
http://msdn.microsoft.com/library/d...ml/frlrfsystemnethttpwebrequestclasstopic.asp

The full documentation for the HttpWebResponse class can be found at:
http://msdn.microsoft.com/library/d...l/frlrfsystemnethttpwebresponseclasstopic.asp

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.
 
C

contact

Here is a more direct example:

(puts the contents of a remote document onto the page)


System.Net.HttpWebRequest req =
(System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
System.Net.HttpWebResponse resp = (System.Net.HttpWebResponse)
req.GetResponse();
System.IO.StreamReader rd = new
System.IO.StreamReader(resp.GetResponseStream());
Response.Write(rd.ReadToEnd());
rd.Close();
resp.Close();


where 'url' is a String variable with the URL of the document you want
to retrieve.
 
J

jdhancock

Sorry to bother you Kevin. If I'm not an experienced .NET user, I
shouldn't be using .NET. Thanks anyway.
 
J

jdhancock

When I use this url from my desktop shortcut, I get the document back
just fine. When using it from my aspx page on localhost, I get a blank
document.
No exception is thrown so I know it's not an authorization issue.

URL = "http://cyl01002/cyl/doc_qanda.exe?133154"
Try
Dim myHttpWebReq As HttpWebRequest = CType(WebRequest.Create(URL),
HttpWebRequest)
Dim myHttpWebResp As HttpWebResponse =
CType(myHttpWebReq.GetResponse(), HttpWebResponse)
Dim sr As StreamReader = New
StreamReader(myHttpWebResp.GetResponseStream)
Response.Write(sr.ReadToEnd)
sr.Close()
myHttpWebResp.Close()
Catch e as WebException
Response.Write("<BR>Error Retrieving document contents " & e.Message)
End Try
Any Ideas?
I apologize for my newbie question.
Thanks in advance
 
J

jdhancock

Found my solution.
Because the URL was an EXE that was ultimately returning the document,
I had to use a MemoryStream instead of a StreamReader. (content type
was not known)

This worked...
Dim ms As New MemoryStream
Dim br As BinaryReader = New
BinaryReader(myHttpWebResp.GetResponseStream)
Dim buffer As Byte()
buffer = br.ReadBytes(4096)
While buffer.Length > 0
ms.Write(buffer, 0, buffer.Length)
buffer = br.ReadBytes(4096)
End While
Response.BinaryWrite(ms.ToArray)

Double bonus, it runs faster. Thanks again,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top