vba 解压缩文件_VBA如何从HTTP请求解压缩Gzip响应

本文介绍如何在VBA中处理HTTP请求,特别是当响应被Gzip压缩时,如何解压缩以获取纯文本数据。通过修改'Accept-Encoding'标头为'deflate',可以避免收到乱码,从而更容易在宏中解析JSON响应。
摘要由CSDN通过智能技术生成

vba 解压缩文件

In this example, I needed to get a currency exchange rate for two currencies, in order to use them in an expense report. I am using MS Access and macros.

在此示例中,我需要获取两种货币的货币汇率,以便在费用报告中使用它们。 我正在使用MS Access和宏。

I've found a site where I can easily send the currency parameters I needed and the response was returned as a JSON structure, which is easy to manipulate. 

我找到了一个可以轻松发送所需货币参数的站点,并且响应以JSON结构的形式返回,该结构易于操作。

However, when I've checked the request (in WireShark), in order to use it in a macro function, I saw that the result was gibberish:

但是,当我检查了请求(在WireShark中)后,为了在宏函数中使用该请求,我看到结果是乱码:

?      ?V*?-JNU?Rr

R?*I,JO-r=}??\?lpi.P?.
?Y?[?%??? 1P???????????B<l?T- ??Vu   


 

I then started to look for an idea of what might be the "problem". I needed the response data in plain text, much like a browser knows to show you when it performs the same request.

然后,我开始寻找可能是“问题”的想法。 我需要纯文本形式的响应数据,就像浏览器知道在执行相同请求时向您显示的那样。

I saw a post about this here - MS Access VBA How To Decompress a Gzip Response From a HTTP Request and it got me thinking about the compression, since an "Accept-Encoding" header in the request has "gzip" as its value.

我在这里看到了有关此内容的文章-MS Access VBA如何从HTTP请求解压缩Gzip响应 ,这使我想到了压缩,因为请求中的“ Accept-Encoding”标头将其值“ gzip”。

I looked for the possible values for the Accept-Encoding header and I leaned that in order to get the response as plain text, I needed to use deflate, as in the example below:

我寻找了Accept-Encoding标头的可能值,然后将其倾斜以使响应以纯文本格式显示,我需要使用deflate,如以下示例所示:

Accept-Encoding: identity, deflate 

and not 

并不是

Accept-Encoding: identity, gzip;q=0 

This tells the server to not use compress the response with gzip and instead to send me the plain text (or uncompressed, i.e. deflate) version of the response.

这告诉服务器不要使用gzip压缩响应,而是向我发送响应的纯文本(或未压缩,即放气)版本。

The reason the response is compressed is to be more efficient with bandwidth.  With larger datasets it may be necessary to deflate the response on the client if the uncompressed version is far larger than the compressed version.

压缩响应的原因是为了更有效地利用带宽。 对于较大的数据集,如果未压缩版本远大于压缩版本,则可能有必要在客户端上缩小响应。

Now, after modifying this header, I received the response I needed, which made it far easier to parse in my macro:

现在,在修改了此标头之后,我收到了所需的响应,这使得在宏中进行解析变得容易得多:

{"source":"EUR","target":"USD","sourceSum":"1","targetSum":3.9793,"rateDate":"01-06-2017","rateDateSet":"01-06-2017"} 

It solved my problem and I hope it'll help all the others.

它解决了我的问题,希望对其他所有人有所帮助。

Have a great day,

祝你有美好的一天,

Shai

hai

翻译自: https://www.experts-exchange.com/articles/30565/VBA-How-To-Decompress-a-Gzip-Response-From-a-HTTP-Request.html

vba 解压缩文件

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值