伊莉討論區

標題: 用HttpWebRequest 下載檔案 [打印本頁]

作者: darkjack    時間: 2014-2-1 08:48 PM     標題: 用HttpWebRequest 下載檔案

本帖最後由 darkjack 於 2014-2-1 08:48 PM 編輯

這是我在網路上搜尋到的 一段程式碼..我修改了一些
請自行增加 Textbox 與 Button 各一個
  1.     Sub Loading(ByRef MyWeb As WebBrowser)
  2.         Do Until WebBrowserRead.ReadyState = WebBrowserReadyState.Complete
  3.             Application.DoEvents()

  4.         Loop
  5.     End Sub


  6.   Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
  7.         Dim url As Uri = New Uri("https://www.google.com.tw/search?safe=off&hl=zh-TW&site=imghp&tbm=isch&source=hp&q=" & TextBox1.Text)

  8.         Dim objWebClient As New WebClient
  9.         Dim temp As Integer
  10.         Dim DataLinkStr(1), DataLinkStr2(1) As String


  11.         Dim GetStrAllLen, Str1, Str2 As Integer


  12.         '載入網頁
  13.         WebBrowserRead.Navigate(url)

  14.         Loading(WebBrowserRead)

  15.         For Each [HtmlElement] As HtmlElement In WebBrowserRead.Document.All
  16.             If [HtmlElement].TagName = "A" And [HtmlElement].GetAttribute("href") <> Nothing Then

  17.                 DataLinkStr = Split([HtmlElement].OuterHtml, "imgurl=")
  18.                 If DataLinkStr.Length > 1 Then

  19.                     DataLinkStr2 = Split(DataLinkStr(1), "&") '分割

  20.                     RichTextBox1.Text &= DataLinkStr2(0) & vbCrLf

  21.                     Str1 = InStrRev(DataLinkStr2(0), "/")  '取得 某字串位置
  22.                     Str1 = Str1 + 1 '往後推一字元

  23.                     GetStrAllLen = DataLinkStr2(0).Length '取得總字數(包涵http://)

  24.                     Str2 = GetStrAllLen - Str1

  25.                     If Strings.Right(DataLinkStr2(0), 4) = ".jpg" Or Strings.Right(DataLinkStr2(0), 4) = ".png" Then

  26.                         ListBox1.Items.Add(Strings.Mid(DataLinkStr2(0), Str1, Str2 + 1)) '取出要存的檔名
  27.                         Try
  28.                             HttpWebDownload(DataLinkStr2(0))
  29.                             Download_Count += 1
  30.                         Catch ex As WebException

  31.                         End Try

  32.                     Else
  33.                         Dim str3split() As String
  34.                         Dim str3 As String
  35.                         str3split = Split(DataLinkStr2(0), ".jpg")
  36.                         str3 = str3split(0) & ".jpg"

  37.                         temp += 1
  38.                     End If
  39.                     'objWebClient.DownloadFileAsync(New Uri(DataLinkStr2(0)), "G:\test\1\" & Strings.Mid(DataLinkStr2(0), Str1, Str2 + 1))
  40.                 End If
  41.             End If
  42.         Next

  43.     End Sub


  44.     Private Download_Count As Integer
  45.     Private Download_C_temp As Integer

  46.     Private Function HttpWebDownload(ByVal InputUrl As String)
  47.         Dim bytes_ As Integer = 0
  48.         Dim MyResponse As HttpWebResponse
  49.         Dim MyRequest As HttpWebRequest
  50.         Dim RemoteStream As Stream
  51.         Dim MyFilewStream As Stream

  52.         Dim TimeStart As DateTime = Now
  53.         Dim TS As TimeSpan

  54.         MyRequest = HttpWebRequest.Create(InputUrl)
  55.         If MyRequest IsNot Nothing Then
  56.             'MyResponse.GetResponseStream()
  57.             MyRequest.Timeout = (5 * 1000)
  58.             MyResponse = MyRequest.GetResponse
  59.             If MyResponse IsNot Nothing Then
  60.                 '判斷網頁狀態
  61.                 If MyResponse.StatusCode = HttpStatusCode.OK Then


  62.                     RemoteStream = MyResponse.GetResponseStream

  63.                     MyFilewStream = File.Create("G:\temp\1\" & Download_Count & ".jpg") '檔名

  64.                     Dim buffer(1024) As Byte
  65.                     Dim bytesRead As Integer

  66.                     TimeStart = Now

  67.                     Do
  68.                         bytesRead = RemoteStream.Read(buffer, 0, buffer.Length)

  69.                         MyFilewStream.Write(buffer, 0, bytesRead)

  70.                         bytes_ += bytesRead

  71.                         Application.DoEvents()
  72.                         '判斷執行時間
  73.                         TS = Now.Subtract(TimeStart)
  74.                         Console.WriteLine(TS)
  75.                         If TS.TotalSeconds > 7 Then
  76.                             Exit Do
  77.                         End If
  78.                     Loop While bytesRead > 0
  79.                 End If
  80.             End If
  81.         End If

  82.         If MyResponse IsNot Nothing Then MyResponse.Close()
  83.         If RemoteStream IsNot Nothing Then RemoteStream.Close()
  84.         If MyFilewStream IsNot Nothing Then MyFilewStream.Close()

  85.         Return bytes_

  86.     End Function
複製代碼

作者: zaq12345    時間: 2020-4-5 11:36 PM

謝謝大大的分享..大大辛苦了




歡迎光臨 伊莉討論區 (http://fdc01.mobile.wahas.com/) Powered by Discuz!