Created
November 28, 2012 12:08
-
-
Save honda0510/4160772 to your computer and use it in GitHub Desktop.
モーグにログインしてモーグのフリーソフトをダウンロードする例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ポップアップのメニュー選択 | |
http://www.moug.net/faq/viewtopic.php?t=64934 | |
モーグのフリーソフト | |
http://www.moug.net/cgi-bin/softwaredl.cgi?excel+SI2012111501 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
' 参照設定 | |
' Microsoft WinHTTP Services, version 5.1 | |
' Microsoft ActiveX Data Objects 2.x Library | |
Sub test() | |
Dim UserName As String | |
Dim Password As String | |
Dim Cookie As String | |
Dim FileUrl As String | |
Dim SavePath As String | |
UserName = "月" | |
Password = "パスワード" | |
Cookie = Login(UserName, Password) | |
FileUrl = "http://www.moug.net/cgi-download/excel/2012111501/AirStatus.zip" | |
SavePath = "C:\work\AirStatus.zip" | |
Download FileUrl, SavePath, Cookie | |
MsgBox "OK" | |
End Sub | |
Function Login(UserName As String, Password As String) As String | |
Const MOUG_LOGIN_URL As String = "https://www.moug.net/faq/login.php" | |
Dim Http As WinHttp.WinHttpRequest | |
Dim Body As String | |
Set Http = New WinHttp.WinHttpRequest | |
Http.Open "POST", MOUG_LOGIN_URL, False | |
Http.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded" | |
Body = "username=" & UserName & "&password=" & Password | |
Http.Send Body | |
If Http.Status <> 200 Then | |
Err.Raise Http.Status, , Http.StatusText | |
End If | |
Login = Http.GetResponseHeader("Set-Cookie") | |
End Function | |
Sub Download(Url As String _ | |
, SavePath As String _ | |
, Optional Cookie As String = "" _ | |
, Optional Timeout As Long = 0 _ | |
, Optional SaveOption As ADODB.SaveOptionsEnum = adSaveCreateNotExist) | |
Dim Http As WinHttp.WinHttpRequest | |
Dim Stream As ADODB.Stream | |
Set Http = New WinHttp.WinHttpRequest | |
If Timeout > 0 Then | |
Http.SetTimeouts Timeout, Timeout, Timeout, Timeout | |
End If | |
Http.Open "GET", Url, False | |
If Len(Cookie) Then | |
Http.SetRequestHeader "Cookie", Cookie | |
End If | |
Http.Send | |
If Http.Status <> 200 Then | |
Err.Raise Http.Status, , Http.StatusText | |
End If | |
Set Stream = New ADODB.Stream | |
Stream.Type = adTypeBinary | |
Stream.Open | |
Stream.Position = 0 | |
Stream.Write Http.ResponseBody | |
Stream.SaveToFile SavePath, SaveOption | |
Stream.Close | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment