Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > HttpRequest sample for downloading document in ASP.NET

Reply
Thread Tools

HttpRequest sample for downloading document in ASP.NET

 
 
jdhancock@parker.com
Guest
Posts: n/a
 
      01-24-2006
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
%>
--------------------------------------------------------------------------------------------------------------------------------------

 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      01-24-2006
Here you go:

http://msdn.microsoft.com/library/de...ponsetopic.asp

--
HTH,

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

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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
> %>
> --------------------------------------------------------------------------------------------------------------------------------------
>



 
Reply With Quote
 
 
 
 
jdhancock@parker.com
Guest
Posts: n/a
 
      01-25-2006
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!

 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      01-25-2006
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/de...classtopic.asp

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

--
HTH,

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

"Kevin Spencer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Here you go:
>
> http://msdn.microsoft.com/library/de...ponsetopic.asp
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> .Net Developer
> Who is Mighty Abbott?
> A twin turret scalawag.
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
>> 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
>> %>
>> --------------------------------------------------------------------------------------------------------------------------------------
>>

>
>



 
Reply With Quote
 
contact@dacris.com
Guest
Posts: n/a
 
      01-25-2006
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.C reate(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.

 
Reply With Quote
 
jdhancock@parker.com
Guest
Posts: n/a
 
      01-26-2006
Sorry to bother you Kevin. If I'm not an experienced .NET user, I
shouldn't be using .NET. Thanks anyway.

 
Reply With Quote
 
jdhancock@parker.com
Guest
Posts: n/a
 
      01-26-2006
This is very helpful. Thank you,

 
Reply With Quote
 
jdhancock@parker.com
Guest
Posts: n/a
 
      01-26-2006
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

 
Reply With Quote
 
jdhancock@parker.com
Guest
Posts: n/a
 
      01-26-2006
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,

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problems downloading sample projects for Eclipse BillyG Java 0 01-23-2007 09:22 PM
looking for asp.net sample with vb.net backend sample is there one? Jake ASP .Net 0 02-09-2006 10:44 PM
[SiSU] book/document generator, sample documents: summary page for document markup and output Ralph Amissah Ruby 0 11-20-2005 03:23 PM
Searching für a big (!) XML document sample with attributes and approx 6 nesting levels Michael Hertz XML 3 09-22-2004 08:47 PM
Searching für a big (!) XML document sample with attributes and approx 6 nesting levels Michael Hertz Java 0 09-19-2004 03:46 PM



Advertisments