vba XMLHTTP 和 ServerXMLHTTP 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11605613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:59:19  来源:igfitidea点击:

Differences between XMLHTTP and ServerXMLHTTP

vbaexcel-vbaxmlhttprequestexcel

提问by Davuz

I'm trying to write an Excel add-in that can get data from a web service into Excel.

我正在尝试编写一个 Excel 加载项,该加载项可以将 Web 服务中的数据导入 Excel。

To use it, the user just needs to type a function name provided by the add-in. I found two articles implementing HTTP requests in VBA: XMLHTTPand ServerXMLHTTP.

要使用它,用户只需键入加载项提供的函数名称。我发现了两篇在 VBA 中实现 HTTP 请求的文章:XMLHTTPServerXMLHTTP

I have difficulty with using them. I don't know which one to use. What are the differences between XMLHTTP and ServerXMLHTTP?

我很难使用它们。我不知道该用哪个。XMLHTTP 和 ServerXMLHTTP 之间有什么区别?

回答by HymanOrangeLantern

Davuz,

达乌兹,

Both Tim and Jay provide excellent context and succinct comments. I will simply add a bit and try to give some context.

Tim 和 Jay 都提供了出色的上下文和简洁的评论。我将简单地添加一点并尝试提供一些上下文。

In essence, the XMLHTTP Object is used to create an XMLHttpRequest, which is used to requisition data from a website/webserver/web service. See the wikipedia link for further details: http://en.wikipedia.org/wiki/XMLHttpRequest

本质上,XMLHTTP 对象用于创建 XMLHttpRequest,用于从网站/网络服务器/网络服务请求数据。有关更多详细信息,请参阅维基百科链接:http: //en.wikipedia.org/wiki/XMLHttpRequest

In general, XMLHTTP is general used when communicating as a client to a server - such as when you use an application to make a request to a webservice. the XMLHTTP Object is also used for various client-focused methods of transfer, such as File Transfer Protocol (FTP), url caching and other useful tools. In short, the XMLHTTP Object is used to make a request to a server of some sort and request something of interest to the client, whether it is access to a server via a FTP connection, a series of files from a repository, or a webservice for processing data from the client.

通常,XMLHTTP 通常用于作为客户端与服务器进行通信时 - 例如当您使用应用程序向 Web 服务发出请求时。XMLHTTP 对象还用于各种以客户端为中心的传输方法,例如文件传输协议 (FTP)、url 缓存和其他有用的工具。简而言之,XMLHTTP 对象用于向某种服务器发出请求并请求客户端感兴趣的内容,无论是通过 FTP 连接访问服务器、存储库中的一系列文件,还是 Web 服务用于处理来自客户端的数据。

By contrast, the ServerXMLHTTP is intended for communicating between servers, to applications (clients) and for processing requests from clients. While the ServerXMLHTTP Object keeps the state active - meaning that the information sent/received to/from a destination is retained for future data transactions in the current connection - it also does not actively support certain XMLHTTP functions, such as "URL caching, auto-discovery of proxy servers, HTTP/1.1 chunking, offline support, and support for Gopher and FTP protocols" for the http client stack used by the ServerXMLHTTP object.

相比之下,ServerXMLHTTP 旨在用于服务器之间、应用程序(客户端)之间的通信以及处理来自客户端的请求。虽然 ServerXMLHTTP 对象保持活动状态——这意味着发送/接收到/来自目的地的信息保留用于当前连接中的未来数据事务——但它也不主动支持某些 XMLHTTP 功能,例如“URL 缓存、自动-代理服务器的发现、HTTP/1.1 分块、离线支持以及对 ServerXMLHTTP 对象使用的 http 客户端堆栈的 Gopher 和 FTP 协议的支持。

From a technical standpoint, the XMLHTTP Object uses WinInet (Windows Internet Explorer) for its functionality, while the ServerXMLHTTP Object uses a client stack. The WinInet dll is the backbone of the Windows internet protocol management, and the dll is used for handling HTTP, HTTPS, FTP and similar requests.

从技术角度来看,XMLHTTP 对象使用 WinInet (Windows Internet Explorer) 来实现其功能,而 ServerXMLHTTP 对象使用客户端堆栈。WinInet dll 是 Windows Internet 协议管理的支柱,该 dll 用于处理 HTTP、HTTPS、FTP 和类似请求。

By contrast, when the ServerXMLHTTP Object creates a new client http stack - which is an essence a separate "session" of a HTTP client. The use of a separate session means that multiple instances of the ServerXMLHTTP Object may be active at any given time - depending on memory and availability of socket connections.

相比之下,当 ServerXMLHTTP 对象创建一个新的客户端 http 堆栈时——本质上是 HTTP 客户端的一个单独的“会话”。使用单独的会话意味着 ServerXMLHTTP 对象的多个实例可能在任何给定时间处于活动状态 - 取决于内存和套接字连接的可用性。

So, in a nutshell - in addition to the information from the comments above, the XMLHTTP Object is often used to request information and use it in some way, usually as a client. Similarly useful but often differently used, the ServerXMLHTTP Object may be used to request data, to send data or even to pass received to another application to another application in a relatively efficient manner. This is often used for business applications that require realtime responses, or for providing data to clients given a series of requests, perhaps with conditions built into those requests - and much, much more.

因此,简而言之——除了上述注释中的信息外,XMLHTTP 对象通常用于请求信息并以某种方式使用它,通常作为客户端。类似有用但通常使用方式不同,ServerXMLHTTP 对象可用于请求数据、发送数据或什至以相对有效的方式将接收到的另一个应用程序传递给另一个应用程序。这通常用于需要实时响应的业务应用程序,或者用于在给定一系列请求的情况下向客户端提供数据,这些请求可能带有内置于这些请求中的条件——等等。

Hopefully that sheds some light on the differences between the two. There is much more to be found when reading SO questions/answers on using specific pieces of XMLHTTP type requests, as well as deeper research into MSDN and other sites providing documentation on internet/XMLHTTPRequest specific materials for VB, VBA and Microsoft Office.

希望这能说明两者之间的差异。在阅读有关使用特定 XMLHTTP 类型请求的 SO 问题/答案以及对 MSDN 和其他站点提供有关 VB、VBA 和 Microsoft Office 的 Internet/XMLHTTPRequest 特定材料的文档的更深入研究时,可以找到更多内容。

Let me know if that helps or if you have other questions/thoughts,

如果这有帮助,或者您有其他问题/想法,请告诉我,

~JOL

~JOL

回答by user3902302

I just got hit by a difference: When the Web Server (IIS) is configured to allow only Windows Authentication, not anonymous access, the ServerXMLHTTP calls fail with an authentication error whereas the XMLHTTP calls do not. It seems that the XMLHTTP object automatically handles sending the Windows Authentication credentials when needed. I suspect that adding NetworkCredentials to the ServerXMLHTTP object should be the best solution but I could only find Basic Authentication which explicitly passes plaintext uid/pwd (obviously unacceptable).

我刚刚发现了一个差异:当 Web 服务器 (IIS) 配置为仅允许 Windows 身份验证而不是匿名访问时,ServerXMLHTTP 调用失败并出现身份验证错误,而 XMLHTTP 调用则不会。似乎 XMLHTTP 对象会在需要时自动处理发送 Windows 身份验证凭据。我怀疑将 NetworkCredentials 添加到 ServerXMLHTTP 对象应该是最好的解决方案,但我只能找到显式传递纯文本 uid/pwd 的基本身份验证(显然是不可接受的)。

回答by Rob Lawton

The ServerXMLHTTP object utilises winhttp where as XMLHTTP utilises wininet.

ServerXMLHTTP 对象使用 winhttp,而 XMLHTTP 使用 wininet。

As wininet parses such matters as authentication and proxy configuration, such problems won't appear like they do when utilising ServerXMLHTTP. However the limited flexibility within XMLHTTP may force you to take-on the additional responsibilities with using ServerXMLHTTP, by having to negotiate server and destination end-point authentication mechanisms, although there are methods within winhttp to assist in this.

由于wininet 会解析身份验证和代理配置等问题,因此在使用ServerXMLHTTP 时不会出现此类问题。然而,XMLHTTP 中有限的灵活性可能会迫使您承担使用 ServerXMLHTTP 的额外责任,因为必须协商服务器和目标端点身份验证机制,尽管 winhttp 中有一些方法可以帮助实现这一点。

When porting the more client based XMLHTTP to the more configurable robust ServerXMLHTTP object and classes, you may wonder why you hadn't done so long ago.

当将更多基于客户端的 XMLHTTP 移植到更可配置的健壮 ServerXMLHTTP 对象和类时,您可能想知道为什么这么久之前没有这样做。

Don't even get me started on the ill fated and now defunct WPAD support in either. :)

甚至不要让我开始在这两者中命运多舛且现已不复存在的 WPAD 支持。:)

回答by user2982122

What really helped me was that.

真正帮助我的是那个。

After defining Object:

定义对象后:

Dim objHTTP As Object

Set objHTTP = createObject("MSXML2.ServerXMLHTTP")
...

Then include these two lines:

然后包括这两行:

' --- Disregard any Proxy Settings and go straight to the IP address

    objHTTP.SetProxy 1