URLダウンロード(U)         <TOP>


URLをダウンロードします。

InternetOpen インターネットのハンドルの作成

InternetCloseHandle Win32インターネット関数のハンドルのクローズ

InternetReadFile インターネット上のファイルの読み込み

InternetOpenUrl URLのオープン

 

URLDownloadToFileは、直接ファイルに保存を実行しますが、この例では保存部を別に作成しなければなりません。

参考

Urlダウンロード(V)読み上げ

 

'================================================================
'= Urlダウンロード(U)
'=    (InternetOpen.bas)
'================================================================
#include "Windows.bi"

#define USER_AGENT "InternetReadFile Test"   'Header(任意)
#define INTERNET_OPEN_TYPE_DIRECT 1          'Proxyを経由せずにアクセスする
#define INTERNET_OPEN_TYPE_PROXY 3           'sProxyName で指定したProxy経由でアクセスする
#define INTERNET_FLAG_RELOAD -2147483648     '
#define WI_READ_READSIZE 1024                'InternetReadFile で一度に読み込むサイズ
#define WI_INITBUFSIZE 32767                 '

' インターネットのハンドルの作成
Declare Function Api_InternetOpen& Lib "wininet" Alias "InternetOpenA" (ByVal sAgent$, ByVal lAccessType&, ByVal sProxyName$, ByVal sProxyBypass$, ByVal lFlags&)

' Win32インターネット関数のハンドルのクローズ
Declare Function Api_InternetCloseHandle% Lib "wininet" Alias "InternetCloseHandle" (ByVal hInet&)

' インターネット上のファイルの読み込み
Declare Function Api_InternetReadFile% Lib "wininet" Alias "InternetReadFile" (ByVal hFile&, ByVal sBuffer$, ByVal lNumBytesToRead&, lNumberOfBytesRead&)

' URLのオープン
Declare Function Api_InternetOpenUrl& Lib "wininet" Alias "InternetOpenUrlA" (ByVal hInternetSession&, ByVal lpszUrl$, ByVal lpszHeaders$, ByVal dwHeadersLength&, ByVal dwFlags&, ByVal dwContext&)

Var Shared Edit1 As Object
Var Shared Edit2 As Object
Var Shared Edit3 As Object
Var Shared Text1 As Object
Var Shared Button1 As Object

Edit1.Attach GetDlgItem("Edit1") : Edit1.SetFontSize 14
Edit2.Attach GetDlgItem("Edit2") : Edit2.SetFontSize 12
Edit3.Attach GetDlgItem("Edit3") : Edit3.SetFontSize 14
Text1.Attach GetDlgItem("Text1") : Text1.SetFontSize 14
Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14

Var Shared URL As String
Var Shared FileName As String
Var Shared Buff As String
Var Shared hFile As Byte

'================================================================
'=
'================================================================
Declare Sub Save_File edecl ()
Sub Save_File()
    hFile = FreeFile
    Open FileName For BinIO As hFile

    If Lof(hFile) <> 0 Then
        Close hFile
        Kill FileName
        Open FileName For BinIO As hFile
    End If

    FWrite hFile, Buff
    Close hFile
End Sub

'================================================================
'=
'================================================================
Declare Sub Html_Dsp edecl ()
Sub Html_Dsp()
    Var Word As String

    hFile = FreeFile
    Open FileName For BinInp As hFile
    Buff = Space$(Lof(hFile))
    FRead hFile, Buff
    Buff = JConv$(Buff, 0, 2)
    Edit2.SetWindowText Buff
    Close hFile
End Sub

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    URL = "http://tokovalue.jp/"
    FileName = "c:\temp\url\test5.htm"

    Edit1.SetWindowText URL
    Edit3.SetWindowText FileName
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var nOpen As Long
    Var nFile As Long
    Var Leng As Integer
    Var ReadSize As Long
    Var Ret As Long

    URL = GetDlgItemText("Edit1")
    FileName = GetDlgItemText("Edit3")

    Leng = 32767
    Buff = Space$(Leng)

    nOpen = Api_InternetOpen("", INTERNET_OPEN_TYPE_DIRECT, ByVal 0, ByVal 0, 0)
    nFile = Api_InternetOpenUrl(nOpen, URL, ByVal 0, ByVal 0, INTERNET_FLAG_RELOAD, ByVal 0)

    Ret = Api_InternetReadFile(nFile, Buff, Leng, ReadSize)
    Buff = Left$(buff, ReadSize)

    Ret = Api_InternetCloseHandle(nFile)
    Ret = Api_InternetCloseHandle(nOpen)

    Save_File
    Html_Dsp
End Sub

'================================================================
'=
'================================================================
Declare Sub MainForm_Resize edecl ()
Sub MainForm_Resize()
    If GetWidth < 550 Or GetHeight < 370 Then
        SetWindowSize 550, 370
    End If
    Edit2.SetWindowSize GetWidth - 30, GetHeight - 144
    Button1.MoveWindow GetWidth - 88, GetHeight - 66
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End