download web document VB.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • seraieis
    New Member
    • Apr 2008
    • 60

    download web document VB.NET

    Good morning experts!

    I'm trying to figure out a way to download a report from one of my companies intranet sites, and I've hit a bit of a snag.

    The report differs by two items: the session id (auto-generated on log on), and the report key (which I get from another system). I've used screen scraping to get the session ID at log on, and I provide the key.

    However, when I try to use My.Computer.Net work.DownloadFi le, the file that downloads is a page (within the system) that says the session has expired. I have a feeling it's because there's a web-based application that generates the report.
    Code:
            path = _
                "http://system.internal.com/xcgi/xnet.exe?sys=sys" & _
                "&ses=" & [B]session[/B] & _
                "&ISN=&TRN=&cmd=DWN&rid=" & _
                "&rpt=/EDICLAIM/HOSPMED%2ECCMEDC%2E%4002" & _
                "&ver=1722" & _
                "&ixn=" & s & "&ixv=" & [B]key[/B]
    
            Try
                Dim file As String = _
                    My.Computer.FileSystem.SpecialDirectories.Desktop _
                    & "\test.xls"
                If My.Computer.FileSystem.FileExists(file) Then Kill(file)
                My.Computer.Network.DownloadFile(path, file)
                Process.Start(file)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
    Now, if I copy the string into the browser, it opens a file download dialog and I can save it as normal. However, trying to use VB to do the same results in an error. Does anyone have any suggestions as to how to overcome this?

    Thank you!
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You should be using HttpWebRequest to download the file should you not?

    Comment

    • seraieis
      New Member
      • Apr 2008
      • 60

      #3
      It's funny you should say that! I continued trying to figure this out, and just tested using that:
      Code:
                  Dim request As HttpWebRequest = WebRequest.Create(path)
                  Dim response As HttpWebResponse = request.GetResponse()
                  Dim reader As New StreamReader(response.GetResponseStream())
                  Dim f As New IO.StreamWriter(file)
      
                  f.Write(reader.ReadToEnd())
                  f.Close()
                  reader.Close()
      
                  Process.Start(file)
      The file that opens after this runs is still the error page. Is there something I'm missing?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        You may need to do the entire process for grabbing the session id and etc with HttpWebRequest objects?

        If you paste the path you provided into a new IE (or FF) window, does it give the correct response? (I say new window, but I mean new instance, re-using an old instance might send down hidden headers obtained from a previous successfull request to the website)

        Comment

        • seraieis
          New Member
          • Apr 2008
          • 60

          #5
          I tried copying the address into another window and it gave the error page.

          Looking at the JavaScript behind the web page's download button, it looks simple enough:
          Code:
          function goToDownload() {
            if (GetCookie("actind") == "") {
               window.location = "/xcgi/xnet.exe?sys=JHSE2&ses=0ECCE09B851B522572AFFB6451E39C6F55EAFFDC&ISN=&TRN=&cmd=DWN&rid=&rpt=/CLM/HOSPMED%2ECCMEDC%2E%4002&ver=1722&ixn=CKEY$&ixv=2008011840100401017442";
            }
          }
          The string generate by the program:
          Code:
          xcgi/xnet.exe?sys=JHSE2&ses=0ecce09b851b522572affb6451e39c6f55eaffdc&ISN=&TRN=&cmd=DWN&rid=&rpt=/CLM/HOSPMED%2ECCMEDC%2E%4002&ver=1722&ixn=CKEY$&ixv=2008011840100401017442
          Now, I know nothing about cookies, so I'm not sure that it's referencing. Plus, reading through the cookie itself, I couldn't find anything with a label of "actind", so I'm a little confused.

          Is there a way to trick the WebRequest into thinking it's coming from the same instance?

          Comment

          • seraieis
            New Member
            • Apr 2008
            • 60

            #6
            I had a breakthrough!

            Code:
                    Dim d As System.Windows.Forms.HtmlDocument = Me.WebBrowser1.Document
                    d.InvokeScript("goToDownload")
            That brings up the dialog box for saving something from the internet to your computer. Now, short of simulating keystrokes, is there a way to NOT have the save file dialog box appear?

            Comment

            Working...