Created
August 18, 2012 05:45
-
-
Save numa08/3384712 to your computer and use it in GitHub Desktop.
POSTメソッドを使うって、意外と簡単だった #VBA ref: http://qiita.com/items/3fc619f1e2902201b084
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
'指定したURLからファイルをダウンロードする. | |
'srctUrl : ダウンロード先のURL | |
'distFilename : ダウンロード先のファイルパス | |
'postBody : 中身 | |
'返り値 : 成功すればtrue | |
Function requestHttpPost(srcUrl As String, distFilepath As String, postBody As String) As Boolean | |
Dim i As Long, bLength As Long 'ループカウンター,バイト長 | |
Dim fileNum As Integer '保存先ファイルのファイル番号 | |
Dim responseBody As String 'レスポンスの中身 | |
Dim timByte() As Byte '一次バイト保存先 | |
Dim winHttp As Object '通信用のオブジェクト | |
Set winHttp = CreateObject("WinHttp.WinHttpRequest.5.1") | |
winHttp.Open "POST", srcUrl, False | |
winHttp.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded" | |
winHttp.Send postBody | |
'HTTP Status Code が200 -399の場合のみ処理を行う | |
If winHttp.Status > 199 And winHttp.Status < 400 Then | |
bLength = LenB(winHttp.responseBody) | |
fileNum = FreeFile() | |
Open distFilepath For Binary Access Write As #fileNum | |
Put #fileNum, , winHttp.responseBody | |
Close #fileNum | |
requestHttpPost = True | |
Else | |
requestHttpPost = False | |
End If | |
Exit Function | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment