WinHttpRequest gzip响应解析

问题描述 投票:2回答:1

我在VBA项目中使用MSXML2.XMLHTTP60进行http冲浪。问题是MSXML2.XMLHTTP60限于四个并发请求。

我正尝试使用WinHttp.WinHttpRequest.5.1,但是还有另一个问题。 MSXML2.XMLHTTP60自动解析gzip结果,但是WinHttpRequest.responseText方法失败,并显示错误:

目标多字节代码页中不存在Unicode字符的映射。

如何使用标准Windows库解析此结果?

代码示例:MSXML2.XMLHTTP60限制:

Public req1 As MSXML2.XMLHTTP60
Public req2 As MSXML2.XMLHTTP60
Public req3 As MSXML2.XMLHTTP60
Public req4 As MSXML2.XMLHTTP60
Public req5 As MSXML2.XMLHTTP60

Private Const url As String = "http://speedtest.tele2.net/100MB.zip"


Public Sub ConcurrentIssue()
    Set req1 = New MSXML2.XMLHTTP60
    req1.Open "get", url, True

    Set req2 = New MSXML2.XMLHTTP60
    req2.Open "get", url, True

    Set req3 = New MSXML2.XMLHTTP60
    req3.Open "get", url, True

    Set req4 = New MSXML2.XMLHTTP60
    req4.Open "get", url, True

    Set req5 = New MSXML2.XMLHTTP60
    req5.Open "get", url, True

    req1.send
    req2.send
    req3.send
    req4.send

    'This query will be wait
    req5.send

End Sub

问题是WinHttp.WinHttpRequest.5.1不支持解压缩(证明链接:https://msdn.microsoft.com/ru-ru/library/windows/desktop/hh227298(v=vs.85).aspx)。我需要自己解压缩响应。

减压问题示例:

Public Sub DecompressOk()
    Set req1 = New MSXML2.XMLHTTP60
    req1.Open "get", "http://www.google.ru", False
    req1.setRequestHeader "User-Agent", "Fiddler"
    req1.setRequestHeader "Accept-Encoding", "gzip, deflate"
    req1.send

    Debug.Print req1.responseText
End Sub

Public Sub WithoutDecompress()
    Dim req As WinHttp.WinHttpRequest
    Set req = New WinHttp.WinHttpRequest

    req.Open "get", "http://www.google.ru", False
    req.setRequestHeader "User-Agent", "Fiddler"
    req.setRequestHeader "Accept-Encoding", "gzip, deflate"
    req.send

    Debug.Print req.responseText
End Sub

我试图做这个技巧没有成功:

Public Sub DecompressIssue()
    Dim req As WinHttp.WinHttpRequest
    Set req = New WinHttp.WinHttpRequest

    req.Open "get", "http://www.google.ru", False
    req.setRequestHeader "User-Agent", "Fiddler"
    req.setRequestHeader "Accept-Encoding", "gzip, deflate"
    req.send

    SaveBinaryToFile req.responseBody, "C:\test.zip"

    Dim xmlReq As MSXML2.XMLHTTP60
    Set xmlReq = New MSXML2.XMLHTTP60

    xmlReq.Open "get", "C:\test.zip", False
    xmlReq.setRequestHeader "Accept-Encoding", "gzip, deflate"
    xmlReq.setRequestHeader "Content-Type", "text/html; charset=windows-1251"
    xmlReq.send

    Debug.Print xmlReq.responseBody
End Sub

Sub SaveBinaryToFile(arrBytes() As Byte, strPath As String)
    With CreateObject("ADODB.Stream")
        .Type = 1 ' adTypeBinary
        .Open
        .Write arrBytes
        .SaveToFile strPath, 2 ' adSaveCreateOverWrite
        .Close
    End With
End Sub
vba web-scraping xmlhttprequest winhttp
1个回答
2
投票

此答案确认为the comment made by omegastripes

.setRequestHeader "Accept-Encoding", "identity"是正确的答案!

我已经花了几天的时间寻找一种解码gzip压缩响应的方法,但是不知道我们可以告诉服务器不要压缩响应。

© www.soinside.com 2019 - 2024. All rights reserved.