VBA 学习笔记 - 网络请求

Posted 笑虾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VBA 学习笔记 - 网络请求相关的知识,希望对你有一定的参考价值。

VBA 学习笔记 - 网络请求

Microsoft.XMLHTTP

Sub XMLHTTPTest()

    Dim request As Object
    Dim response As String
    Dim isAsync  As Boolean
    Dim url As String

    Set request = CreateObject("Microsoft.XMLHTTP")
    isAsync = True
    url = "https://cn.bing.com/hp/api/v1/trivia?format=json&id=HPQuiz_20221212_PoinsettiaDay&FORM=Z9FD1"
    
    With request
        .Open "POST", url, isAsync
        .setRequestHeader "Cookie", "jerry=9527"
        .send
        While .ReadyState <> 4
            DoEvents
        Wend
        
        response = .responseText
        Debug.Print "《Microsoft.XMLHTTP》"
        Debug.Print "状态:" & .Status
        Debug.Print "响应:" & response
        Debug.Print vbCrLf
    End With

End Sub

MSXML2.serverXMLHTTP.6.0

Sub serverXMLHTTPTest()

    Dim request As Object
    Dim response As String
    Dim isAsync  As Boolean
    Dim url As String

    Set request = CreateObject("MSXML2.serverXMLHTTP.6.0")
    isAsync = True
    url = "https://cn.bing.com/hp/api/v1/trivia?format=json&id=HPQuiz_20221212_PoinsettiaDay&FORM=Z9FD1"
    
    With request
        .Open "POST", url, isAsync
        .setRequestHeader "Cookie", "jerry=9527"
        .send
        .WaitForResponse
        
        response = .responseText
        Debug.Print "《MSXML2.serverXMLHTTP.6.0》"
        Debug.Print "状态:" & .Status
        Debug.Print "响应:" & response
        Debug.Print vbCrLf
    End With

End Sub

WinHttp.WinHttpRequest.5.1

Sub winHttpRequestTest()

    Dim request As Object
    Dim response As String
    Dim isAsync  As Boolean
    Dim url As String

    Set request = CreateObject("WinHttp.WinHttpRequest.5.1")
    isAsync = True
    url = "https://cn.bing.com/hp/api/v1/trivia?format=json&id=HPQuiz_20221212_PoinsettiaDay&FORM=Z9FD1"
    
    With request
        .Open "POST", url, isAsync
        .setRequestHeader "Cookie", "jerry=9527"
        .send
        .WaitForResponse
        
        response = .responseText
        Debug.Print "《WinHttp.WinHttpRequest.5.1》"
        Debug.Print "状态:" & .Status
        Debug.Print "响应:" & response
        Debug.Print vbCrLf
    End With

End Sub

参考资料

MSDN - WinHttp.WinHttpRequest.5.1

以上是关于VBA 学习笔记 - 网络请求的主要内容,如果未能解决你的问题,请参考以下文章

iOS学习笔记12-网络NSURLConnection

VBA学习笔记之VBA学习思路

iOS开发学习笔记(OC语言)——网络请求

python学习笔记-day8-3-python 网络请求及requests模块

fireflyLogin网络工具设计模式——类工厂(学习笔记)

VBA学习笔记之数据类型