HttpRequest sample for downloading document in ASP.NET

Discussion in 'ASP .Net' started by jdhancock@parker.com, Jan 24, 2006.

  1. Guest

    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
    %>
    --------------------------------------------------------------------------------------------------------------------------------------
     
    , Jan 24, 2006
    #1
    1. Advertising

  2. Here you go:

    http://msdn.microsoft.com/library/d...rfsystemnetwebexceptionclassresponsetopic.asp

    --
    HTH,

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

    <> wrote in message
    news:...
    > 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
    > %>
    > --------------------------------------------------------------------------------------------------------------------------------------
    >
     
    Kevin Spencer, Jan 24, 2006
    #2
    1. Advertising

  3. Guest

    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!
     
    , Jan 25, 2006
    #3
  4. 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.

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

    >
    >
     
    Kevin Spencer, Jan 25, 2006
    #4
  5. Guest

    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.
     
    , Jan 25, 2006
    #5
  6. Guest

    Sorry to bother you Kevin. If I'm not an experienced .NET user, I
    shouldn't be using .NET. Thanks anyway.
     
    , Jan 26, 2006
    #6
  7. Guest

    This is very helpful. Thank you,
     
    , Jan 26, 2006
    #7
  8. Guest

    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
     
    , Jan 26, 2006
    #8
  9. Guest

    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,
     
    , Jan 26, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jake
    Replies:
    0
    Views:
    605
  2. BillyG
    Replies:
    0
    Views:
    659
    BillyG
    Jan 23, 2007
  3. Ralph Amissah
    Replies:
    0
    Views:
    207
    Ralph Amissah
    Nov 20, 2005
  4. Tim Chase
    Replies:
    0
    Views:
    97
    Tim Chase
    Feb 16, 2014
  5. Terry Reedy
    Replies:
    0
    Views:
    106
    Terry Reedy
    Feb 16, 2014
Loading...

Share This Page