Logon internetsite and download file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Henrik

    #1

    Logon internetsite and download file

    Hi,,

    Can anyone tell me how to log on to a internet website with username and
    password, navigate to other subpages on that site, and then start downloading
    a textfile to disk??

    Thanks
  • Mike Labosh

    #2
    Re: Logon internetsite and download file

    Here's a snip from a web spider I wrote last year:
    <snip>
    Private Function getHtml(ByVal i As Integer) As String

    Dim url As String = "http://www.wtng.info/wtng-" & Chr(i) & Chr(i) &
    ".html"
    Dim reader As StreamReader
    Dim html As String

    Try
    reader = New StreamReader( _
    CType( _
    CType( _
    WebRequest.Crea te(url), _
    HttpWebRequest) .GetResponse(), _
    HttpWebResponse ).GetResponseSt ream())

    html = reader.ReadToEn d()
    reader.Close()

    Catch ex As Exception
    Try: reader.Close(): Catch exc As Exception: End Try
    html = ""
    Console.WriteLi ne("Could not connect to " & url)
    End Try

    Return html.Trim()

    End Function
    </snip>

    --
    Peace & happy computing,

    Mike Labosh, MCSD
    "Musha ring dum a doo dum a da!" -- James Hetfield
    "Henrik" <Henrik@discuss ions.microsoft. com> wrote in message
    news:70F81168-CAB1-49CC-870C-049BF4E049CF@mi crosoft.com...[color=blue]
    > Hi,,
    >
    > Can anyone tell me how to log on to a internet website with username and
    > password, navigate to other subpages on that site, and then start
    > downloading
    > a textfile to disk??
    >
    > Thanks[/color]


    Comment

    • Mike Labosh

      #3
      Re: Logon internetsite and download file

      Here's a snip from a web spider I wrote last year:
      <snip>
      Private Function getHtml(ByVal i As Integer) As String

      Dim url As String = "http://www.wtng.info/wtng-" & Chr(i) & Chr(i) &
      ".html"
      Dim reader As StreamReader
      Dim html As String

      Try
      reader = New StreamReader( _
      CType( _
      CType( _
      WebRequest.Crea te(url), _
      HttpWebRequest) .GetResponse(), _
      HttpWebResponse ).GetResponseSt ream())

      html = reader.ReadToEn d()
      reader.Close()

      Catch ex As Exception
      Try: reader.Close(): Catch exc As Exception: End Try
      html = ""
      Console.WriteLi ne("Could not connect to " & url)
      End Try

      Return html.Trim()

      End Function
      </snip>

      --
      Peace & happy computing,

      Mike Labosh, MCSD
      "Musha ring dum a doo dum a da!" -- James Hetfield
      "Henrik" <Henrik@discuss ions.microsoft. com> wrote in message
      news:70F81168-CAB1-49CC-870C-049BF4E049CF@mi crosoft.com...[color=blue]
      > Hi,,
      >
      > Can anyone tell me how to log on to a internet website with username and
      > password, navigate to other subpages on that site, and then start
      > downloading
      > a textfile to disk??
      >
      > Thanks[/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Logon internetsite and download file

        "Henrik" <Henrik@discuss ions.microsoft. com> schrieb:[color=blue]
        > Can anyone tell me how to log on to a internet website with username and
        > password, navigate to other subpages on that site, and then start
        > downloading
        > a textfile to disk??[/color]

        \\\
        Dim w As New WebClient()
        w.Credentials = New NetworkCredenti al("username", "password")
        w.DownloadFile( ...)
        ///

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://classicvb.org/petition/>

        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Logon internetsite and download file

          "Henrik" <Henrik@discuss ions.microsoft. com> schrieb:[color=blue]
          > Can anyone tell me how to log on to a internet website with username and
          > password, navigate to other subpages on that site, and then start
          > downloading
          > a textfile to disk??[/color]

          \\\
          Dim w As New WebClient()
          w.Credentials = New NetworkCredenti al("username", "password")
          w.DownloadFile( ...)
          ///

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          • Henrik

            #6
            Re: Logon internetsite and download file

            Hi Mike,,

            Thanks for you reply,, But it's not quite that im looking for?

            I want to connect to a site, where in need to type in my username and
            password on a form and submit this form. After valid check i get a new page,
            where i can download a ".txt"-file. And it's this file im interesed in..

            I've got the download procedure to work just fine by using a
            "WebClient1 "-component.. But my main problem is the logon-procedure to the
            site. I've tried this:

            Dim w As New WebClient
            w.Credentials = New NetworkCredenti al("username", "password")
            w.DownloadFile( "https://Reports/xx/xxx.TXT", "c:\xxx.TXT ")

            It's saves a file,, but its the logonpage??

            "Mike Labosh" wrote:
            [color=blue]
            > Here's a snip from a web spider I wrote last year:
            > <snip>
            > Private Function getHtml(ByVal i As Integer) As String
            >
            > Dim url As String = "http://www.wtng.info/wtng-" & Chr(i) & Chr(i) &
            > ".html"
            > Dim reader As StreamReader
            > Dim html As String
            >
            > Try
            > reader = New StreamReader( _
            > CType( _
            > CType( _
            > WebRequest.Crea te(url), _
            > HttpWebRequest) .GetResponse(), _
            > HttpWebResponse ).GetResponseSt ream())
            >
            > html = reader.ReadToEn d()
            > reader.Close()
            >
            > Catch ex As Exception
            > Try: reader.Close(): Catch exc As Exception: End Try
            > html = ""
            > Console.WriteLi ne("Could not connect to " & url)
            > End Try
            >
            > Return html.Trim()
            >
            > End Function
            > </snip>
            >
            > --
            > Peace & happy computing,
            >
            > Mike Labosh, MCSD
            > "Musha ring dum a doo dum a da!" -- James Hetfield
            > "Henrik" <Henrik@discuss ions.microsoft. com> wrote in message
            > news:70F81168-CAB1-49CC-870C-049BF4E049CF@mi crosoft.com...[color=green]
            > > Hi,,
            > >
            > > Can anyone tell me how to log on to a internet website with username and
            > > password, navigate to other subpages on that site, and then start
            > > downloading
            > > a textfile to disk??
            > >
            > > Thanks[/color]
            >
            >
            >[/color]

            Comment

            • Henrik

              #7
              Re: Logon internetsite and download file

              Hi Mike,,

              Thanks for you reply,, But it's not quite that im looking for?

              I want to connect to a site, where in need to type in my username and
              password on a form and submit this form. After valid check i get a new page,
              where i can download a ".txt"-file. And it's this file im interesed in..

              I've got the download procedure to work just fine by using a
              "WebClient1 "-component.. But my main problem is the logon-procedure to the
              site. I've tried this:

              Dim w As New WebClient
              w.Credentials = New NetworkCredenti al("username", "password")
              w.DownloadFile( "https://Reports/xx/xxx.TXT", "c:\xxx.TXT ")

              It's saves a file,, but its the logonpage??

              "Mike Labosh" wrote:
              [color=blue]
              > Here's a snip from a web spider I wrote last year:
              > <snip>
              > Private Function getHtml(ByVal i As Integer) As String
              >
              > Dim url As String = "http://www.wtng.info/wtng-" & Chr(i) & Chr(i) &
              > ".html"
              > Dim reader As StreamReader
              > Dim html As String
              >
              > Try
              > reader = New StreamReader( _
              > CType( _
              > CType( _
              > WebRequest.Crea te(url), _
              > HttpWebRequest) .GetResponse(), _
              > HttpWebResponse ).GetResponseSt ream())
              >
              > html = reader.ReadToEn d()
              > reader.Close()
              >
              > Catch ex As Exception
              > Try: reader.Close(): Catch exc As Exception: End Try
              > html = ""
              > Console.WriteLi ne("Could not connect to " & url)
              > End Try
              >
              > Return html.Trim()
              >
              > End Function
              > </snip>
              >
              > --
              > Peace & happy computing,
              >
              > Mike Labosh, MCSD
              > "Musha ring dum a doo dum a da!" -- James Hetfield
              > "Henrik" <Henrik@discuss ions.microsoft. com> wrote in message
              > news:70F81168-CAB1-49CC-870C-049BF4E049CF@mi crosoft.com...[color=green]
              > > Hi,,
              > >
              > > Can anyone tell me how to log on to a internet website with username and
              > > password, navigate to other subpages on that site, and then start
              > > downloading
              > > a textfile to disk??
              > >
              > > Thanks[/color]
              >
              >
              >[/color]

              Comment

              • Henrik

                #8
                Re: Logon internetsite and download file

                Hi Herfried,,

                Thanks for replying :o)

                I have tried that?,, it only takes me to the loginform with the username,
                password and submitbutton :o(.. And i have even tried to write in the whole
                url to the file, but still no luck??

                Do i really have to "fill" in username and password and make the
                submit-action programly instead?? and the call the url with the file to
                download afterwards?

                Thanks

                "Herfried K. Wagner [MVP]" wrote:
                [color=blue]
                > "Henrik" <Henrik@discuss ions.microsoft. com> schrieb:[color=green]
                > > Can anyone tell me how to log on to a internet website with username and
                > > password, navigate to other subpages on that site, and then start
                > > downloading
                > > a textfile to disk??[/color]
                >
                > \\\
                > Dim w As New WebClient()
                > w.Credentials = New NetworkCredenti al("username", "password")
                > w.DownloadFile( ...)
                > ///
                >
                > --
                > M S Herfried K. Wagner
                > M V P <URL:http://dotnet.mvps.org/>
                > V B <URL:http://classicvb.org/petition/>
                >
                >[/color]

                Comment

                • Henrik

                  #9
                  Re: Logon internetsite and download file

                  Hi Herfried,,

                  Thanks for replying :o)

                  I have tried that?,, it only takes me to the loginform with the username,
                  password and submitbutton :o(.. And i have even tried to write in the whole
                  url to the file, but still no luck??

                  Do i really have to "fill" in username and password and make the
                  submit-action programly instead?? and the call the url with the file to
                  download afterwards?

                  Thanks

                  "Herfried K. Wagner [MVP]" wrote:
                  [color=blue]
                  > "Henrik" <Henrik@discuss ions.microsoft. com> schrieb:[color=green]
                  > > Can anyone tell me how to log on to a internet website with username and
                  > > password, navigate to other subpages on that site, and then start
                  > > downloading
                  > > a textfile to disk??[/color]
                  >
                  > \\\
                  > Dim w As New WebClient()
                  > w.Credentials = New NetworkCredenti al("username", "password")
                  > w.DownloadFile( ...)
                  > ///
                  >
                  > --
                  > M S Herfried K. Wagner
                  > M V P <URL:http://dotnet.mvps.org/>
                  > V B <URL:http://classicvb.org/petition/>
                  >
                  >[/color]

                  Comment

                  • MARTIN LANNY

                    #10
                    Re: Logon internetsite and download file

                    I have the exact same problem.
                    I need to download the csv file from Google AdSense.
                    Google requires the name and password to be send using "POST" method.

                    This code (below) will only download the html content of the page, but
                    if url points to file it doesn't do anything. Can someone amend this
                    code to work for files as well?

                    Public Function GetPageHTML(ByV al URL As String) As String
                    Dim myWebClient As New WebClient
                    Dim myNameValueColl ection As New NameValueCollec tion
                    myNameValueColl ection.Add("use rname", "joedoe")
                    myNameValueColl ection.Add("pas sword", "123456")
                    Dim responseArray As Byte() = myWebClient.Upl oadValues(URL,
                    "POST", myNameValueColl ection)
                    GetPageHTML = Encoding.UTF8.G etString(respon seArray)
                    End Function

                    Comment

                    • Henrik

                      #11
                      Re: Logon internetsite and download file

                      Fixed my problem by doing this:

                      Dim Request As HttpWebRequest
                      Dim Response As HttpWebResponse
                      Dim URL As String = "https://login.form"
                      Dim Params As String = "action=login&u sername=" & User & "&password= " & Pwd
                      & "&submit=lo gin"
                      Dim CC As New CookieContainer
                      Dim myWriter As StreamWriter
                      Dim sr As StreamReader

                      Request = WebRequest.Crea te(url)
                      Request.Timeout = 120 * 1000
                      Request.CookieC ontainer = CC
                      Request.Method = "POST"
                      Request.Content Length = params.Length
                      Request.Content Type = "applicatio n/x-www-form-urlencoded"
                      Request.KeepAli ve = True


                      myWriter = New StreamWriter(Re quest.GetReques tStream())
                      myWriter.Write( params)

                      myWriter.Close( )


                      Response = Request.GetResp onse()
                      Response.Cookie s = Request.CookieC ontainer.GetCoo kies(Request.Re questUri)

                      For i = 0 To Response.Cookie s.Count - 1
                      CC.Add(Response .Cookies(i))
                      Next

                      sr = New StreamReader(Re sponse.GetRespo nseStream(), Encoding.GetEnc oding(850))
                      ResultHTML = sr.ReadToEnd()
                      -----------------------------------------------------------------------------

                      Then i could create a new request and navigate to the page with my file..

                      Thanks for your help




                      "MARTIN LANNY" wrote:
                      [color=blue]
                      > I have the exact same problem.
                      > I need to download the csv file from Google AdSense.
                      > Google requires the name and password to be send using "POST" method.
                      >
                      > This code (below) will only download the html content of the page, but
                      > if url points to file it doesn't do anything. Can someone amend this
                      > code to work for files as well?
                      >
                      > Public Function GetPageHTML(ByV al URL As String) As String
                      > Dim myWebClient As New WebClient
                      > Dim myNameValueColl ection As New NameValueCollec tion
                      > myNameValueColl ection.Add("use rname", "joedoe")
                      > myNameValueColl ection.Add("pas sword", "123456")
                      > Dim responseArray As Byte() = myWebClient.Upl oadValues(URL,
                      > "POST", myNameValueColl ection)
                      > GetPageHTML = Encoding.UTF8.G etString(respon seArray)
                      > End Function
                      >
                      >[/color]

                      Comment

                      Working...