VBA中的HTTP请求:WinHttp,或MSXML2,还是......? [英] HTTP Requests in VBA: WinHttp, or MSXML2, or ...?

查看:976
本文介绍了VBA中的HTTP请求:WinHttp,或MSXML2,还是......?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我正在尝试从VBA发出HTTP请求。试图弄清楚是否使用WinHttp.WinHttpRequest,MSXML2.ServerXMLHTTP60或MSXML2.DOMDocument60,还是其他什么?我已经阅读了以下内容:

I'm trying to do HTTP requests from VBA. Trying to figure out whether to use WinHttp.WinHttpRequest, MSXML2.ServerXMLHTTP60, or MSXML2.DOMDocument60, or something else? I've read the following:

  • "there are even better methods than with winhttp." https://stagesolutions.wordpress.com/2012/04/18/asynchronous-download-from-http-with-vba/#comment-3
  • WinHttpRequest may require ADO to do certain things (parsing the response?)
  • "Msxml2.XMLHTTP and Msxml2.ServerXMLHTTP are two components share the similar interface for fetching XML files over HTTP protocal. The former is built upon URLMon, which relies on WinINet. The later is built upon WinHTTP, which is a server friendly replacement for WinINet. To put it simple - ServerXMLHTTP = XML + WinHTTP" http://stackoverflow.com/questions/1163045/differences-between-msxml2-serverxmlhttp-and-winhttp-winhttprequest/1818383#1818383
  • XML3 is faster than XML2, but i only see XML2 in my VBA references (also, is XML v6 the same thing as MSXML2?) https://msdn.microsoft.com/en-us/library/ms950768.aspx

我更喜欢处理异步响应的能力,我注意到WinHTTP提供了更多的事件,特别是OnError和OnResponseFinished。不确定这是否是获取异步的唯一途径。 

I prefer the ability to handle asynchronous responses, and i noticed WinHTTP offers more events, particularly OnError and OnResponseFinished. Not sure if that's the only way to get asynch. 

我注意到MSXML2.ServerXMLHTTP60不会暴露任何事件。 

I noticed MSXML2.ServerXMLHTTP60 does not expose any events. 

I欢迎任何反馈。 -thx!

I welcome any feedback. -thx!

Option Explicit

Public WithEvents m_XML As MSXML2.DOMDocument60
Public WithEvents m_WinHTTP As WinHttp.WinHttpRequest


Private Sub m_WinHTTP_OnError(ByVal ErrorNumber As Long, ByVal ErrorDescription As String)
End Sub

Private Sub m_WinHTTP_OnResponseFinished()
End Sub

Private Sub m_XML_ondataavailable()
End Sub

Private Sub m_XML_onreadystatechange()
End Sub

推荐答案

MSXML2只是一个命名空间。例如,MSXML6文档的progid是MSXML2.DOMDocument.6.0。

MSXML2 is just a namespace. For example, the progid of the MSXML6 document is MSXML2.DOMDocument.6.0.

WinINet是Internet Explorer提供的网络堆栈。它不适用于服务,但可能与您的VBA代码无关。

WinINet is the network stack provided by Internet Explorer. It does not work with a service but that probably does not matter to your VBA code.

WinHttp是一个不同的堆栈,最适合与服务一起使用。但它缺少WinInet提供的cookie,缓存和代理管理。 

WinHttp is a different stack that best works with services. But it lacks cookie, cache and proxy management provided by WinInet. 

MSXML6为两者提供包装,并且都支持异步模式。您的选择可能取决于您如何处理cookie(例如服务器需要登录?)和代理(您使用IE代理吗?)。

MSXML6 provides wrapper for both and both support async mode. Your choice would probably depend on how you deal with cookies (e.g does the server requires login?) and proxy (do you use the IE proxy?).


这篇关于VBA中的HTTP请求:WinHttp,或MSXML2,还是......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆