從 HTTP 上下載檔案樣本

來源:互聯網
上載者:User
從 HTTP 上下載檔案樣本,源碼免費(有進度顯示)

*直接運行下面 代碼就可以了,在.vfp8.0環境下測試成功!

* How to download data of this reference (DBF files) using HTTP
* 從 HTTP 上下載檔案
*------------------------------------------------------------------
Clea
#Define INTERNET_OPEN_TYPE_DIRECT    1
#Define INTERNET_FLAG_NEED_FILE     16

#Define HTTP_QUERY_CONTENT_TYPE     1
#Define HTTP_QUERY_CONTENT_LENGTH    5

Do Decl        && declare external functions
*If InternetCheckConnection("http://www.163.com/", 1, 0) <> 0 &&已經串連Internet
*Endif
* initialize Wininet library

Private hOpen
hOpen = InternetOpen ("vfp", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0)
If hOpen = 0
? "GetLastError:", GetLastError()
? "在本機上 WinInet 函數庫無效"
Return
Endif

Local lcBaseUrl, lcDstPath, hFile, lcFile
lcBaseUrl = "http://ftpgz13.pconline.com.cn/pub/download/200203/Mp3RenameEasy.zip"&&根據實際修改這裡的字串
lcFile    = "Mp3RenameEasy.zip"&&儲存的檔案名稱
lcDstPath = "C:\" &&儲存的路徑

* get a handle of the remote file
hFile = InternetOpenUrl (hOpen,;
lcBaseUrl , "", 0,;
INTERNET_FLAG_NEED_FILE, 0)

If hFile <> 0
?hfile
* even if there is no such file, the most evidently
* you will get an ASCII file in response (404 error page)
= http2local (hFile, lcDstPath + lcFile)
= InternetCloseHandle (hFile)
Else
? "網址不能開啟"
? "錯誤號碼:", GetLastError()
Endif

* release WinInet library
= InternetCloseHandle (hOpen)
Return        && main

*--------------------------------------------
Function http2local (hSource, lcTarget)
* reads data from a remote file
#Define TransferBuffer   4096
? lcTarget + " "

* create the target file

hTarget = Fcreate (lcTarget)
If (hTarget = -1)
  ?? "指定的目標檔案無效"
  Return -1
Endif

Local Buffer , BufLen , dwIndex
Buffer = Space(128)
BufLen = Len(Buffer)
dwIndex = 0
If HttpQueryInfo(hSource, HTTP_QUERY_CONTENT_LENGTH, @Buffer, @BufLen, @dwIndex) = 0
  ?"擷取檔案資訊失敗!"
  Return -1
Endif
?
?"檔案大小: " + Left(Buffer,buflen)+" / "

lnTotalBytesRead = 0
lnSec = Seconds()

Do While .T.
  If Inkey() = 27 && 按 "Esc" 鍵
   Exit
  Endif
  lcBuffer = Replicate(Chr(0), TransferBuffer)
  lnBytesRead = 0

  If InternetReadFile (hSource, @lcBuffer,TransferBuffer, @lnBytesRead) = 1

   = Fwrite (hTarget, lcBuffer, lnBytesRead)
   If lnBytesRead = 0
    Exit
   Endif
   lnTotalBytesRead = lnTotalBytesRead + lnBytesRead
   Wait Window Transform(Val(Buffer)/1024) + " KB / " + Transform(lnTotalBytesRead/1024) + " KB " Nowait

  Else
   Exit
  Endif
  DoEvents
Enddo

= Fclose (hTarget)
?? " " + Transform(lnTotalBytesRead) + " Bytes 完成  用時: " + Transform(Seconds() - lnSec) + " 秒"
Return  lnBytesRead
Endfunc

Procedure  Decl
Declare Integer InternetCheckConnection In wininet.Dll ;
  String lpszUrl ,;
  Integer dwFlags ,;
  Integer dwReserved

Declare Integer GetLastError In kernel32

Declare Integer InternetOpen In wininet;
  STRING  sAgent,;
  INTEGER lAccessTypem,;
  STRING  sProxyName,;
  STRING  sProxyBypass,;
  STRING  lFlags

Declare Integer InternetCloseHandle In wininet Integer hInet

Declare Integer InternetOpenUrl In wininet;
  INTEGER hInternet,;
  STRING  lpszUrl,;
  STRING  lpszHeaders,;
  INTEGER dwHeadersLength,;
  INTEGER dwFlags,;
  INTEGER dwContext

Declare Integer InternetReadFile In wininet;
  INTEGER   hFile,;
  STRING  @ lpBuffer,;
  INTEGER   dwNumberOfBytesToRead,;
  INTEGER @ lpdwNumberOfBytesRead

Declare Integer HttpQueryInfo In wininet ;
  INTEGER   hHttpRequest, ;
  INTEGER   lInfoLe

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.