VBA xmlhttprequest循环而无需关闭连接 [英] VBA xmlhttprequest loop without closing connection

查看:286
本文介绍了VBA xmlhttprequest循环而无需关闭连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在VBA中使用xmlhttp连接到API.问题是我需要使用相同的API链接多次创建循环,唯一的不同是我发送了不同的JSON字符串.

I am connecting to an API using xmlhttp in VBA. The problem is that I need to create a loop multiple times using the same API link with the only difference that I send different JSON string.

是否可以仅打开一次连接并发送标头和身份验证,然后多次发送数据并接收响应?

Is it possible to open the connection and send headers and authentification only once then send data and receive response multiple times?

主要目的是提高代码速度. 我知道我可以在循环内发送标头和进行身份验证,但是它运行得太慢,无法满足我的需求.

The main purpose is to improve the speed of the code. I am aware that I can send header and authenticate inside the loop but it runs too slow for my needs.

谢谢!

    APIKey = "xx" 
    Session = "ss" 
    API_Link = "zz" 
    Dim xhr: Set xhr = CreateObject("MSXML2.XMLHTTP")
        With xhr
            .Open "POST", API_Link, False
            .setRequestHeader "X-Application", APIKey
            .setRequestHeader "Content-Type", "application/json"
            .setRequestHeader "Accept", "application/json"
            .setRequestHeader "X-Authentication", Session
        End With
        For Each cl In Rng
       'The below runs only the first time then it gives an error 
            xhr.send JSON_Query
            a = xhr.ResponseText 
        next cl

推荐答案

不,我不认为xmlhttp(可以证明是错误的)可以保留标头.最好的办法是在循环外创建xmlhttp对象,并将某些值保留在变量中以供重复使用.您仍然必须提供.Get,.Send和标头的事务.

No, I don't believe you can with xmlhttp (happy to be proven wrong) in terms of keeping headers. The best you can do is create the xmlhttp object outside the loop and hold certain values in variables for re-use. You still have to provide the transactions of .Get , .Send and the headers.

就保留身份验证而言,我认为您可以登录一次,然后使用

In terms of preserving authentication I think you can login once and then pass the cookies (JSESSION) in subsequent requests - depending on expiration period - using MSXML2.ServerXMLHTTP.

其他语言,例如python,您可以实现 http会话.这样,您就可以在请求中保留某些参数.

In other languages e.g. python you have the implementation of http sessions. This allows you to persist certain parameters across requests.

HTTP持久连接

HTTP持久连接,也称为HTTP Keep-alive或HTTP 连接重用,是使用单个TCP连接发送的想法 并接收多个HTTP请求/响应,而不是打开一个 每个请求/响应对都有新的连接.较新的 HTTP/2协议使用相同的想法,并将其进一步扩展到允许 多个并发请求/响应将在单个上多路复用 连接.

HTTP persistent connection, also called HTTP keep-alive, or HTTP connection reuse, is the idea of using a single TCP connection to send and receive multiple HTTP requests/responses, as opposed to opening a new connection for every single request/response pair. The newer HTTP/2 protocol uses the same idea and takes it further to allow multiple concurrent requests/responses to be multiplexed over a single connection.

在python中,例如 :

In python, for example:

会话也可以用于向请求提供默认数据 方法.这是通过向会话的属性提供数据来完成的 对象.

Sessions can also be used to provide default data to the request methods. This is done by providing data to the properties on a Session object.

因此,您将创建Session对象,该对象具有与发​​出请求的API库相同的方法(如xmlhttp一样),然后更新持久存在的标头.

So, you create the Session object, which has the same methods as the API library which issues the requests (like xmlhttp does) and then update headers which persist.

s = requests.Session()
s.auth = ('user', 'pass')
s.headers.update({'x-test': 'true'})

# both 'x-test' and 'x-test2' are sent
s.get('https://httpbin.org/headers', headers={'x-test2': 'true'})

会话标识会话期间源自同一浏览器的请求.所有servlet都可以共享同一会话. JSESSIONID由服务器生成,可以通过cookie传递给客户端 Srinivas Balasani

A session identifies the requests that originate from the same browser during the period of conversation. All the servlets can share the same session. The JSESSIONID is generated by the server and can be passed to client through cookies Srinivas Balasani

优势:

  1. 减少了后续请求中的延迟(无握手).
  2. 由于更少的新连接和TLS握手,减少了CPU使用率和往返次数.
  3. 启用HTTP管道的请求和响应.
  4. 减少了网络拥塞(更少的TCP连接).
  5. 可以在不关闭TCP连接的情况下报告错误.
  1. Reduced latency in subsequent requests (no handshaking).
  2. Reduced CPU usage and round-trips because of fewer new connections and TLS handshakes.
  3. Enables HTTP pipelining of requests and responses.
  4. Reduced network congestion (fewer TCP connections).
  5. Errors can be reported without the penalty of closing the TCP connection.

根据RFC 7230第6.4节,客户端应限制 它维持给定的同时打开连接数 服务器".HTTP/1.1规范的先前版本已声明 特定的最大值,但用RFC 7230的话来说 在许多应用中不切实际...相反...要保守一些 当打开多个连接时".这些准则旨在 缩短HTTP响应时间并避免拥塞.如果HTTP流水线 正确实施,就不会获得性能上的好处 来自其他连接,而其他连接可能会导致 拥塞问题

According to RFC 7230, section 6.4, "a client ought to limit the number of simultaneous open connections that it maintains to a given server". The previous version of the HTTP/1.1 specification stated specific maximum values but in the words of RFC 7230 "this was found to be impractical for many applications... instead... be conservative when opening multiple connections". These guidelines are intended to improve HTTP response times and avoid congestion. If HTTP pipelining is correctly implemented, there is no performance benefit to be gained from additional connections, while additional connections may cause issues with congestion

缺点:

如果客户端没有关闭所有数据时的连接它 需求已收到,保持连接所需的资源 在服务器上打开将对其他客户端不可用.多少 这会影响服务器的可用性以及资源的持续时间 不可用取决于服务器的体系结构和配置.

If the client does not close the connection when all of the data it needs has been received, the resources needed to keep the connection open on the server will be unavailable for other clients. How much this affects the server's availability and how long the resources are unavailable depend on the server's architecture and configuration.

因此,仍然使用python示例,我们通常使用with语句,该语句在执行嵌套在其中的代码块执行后自动确保与退出方法的连接关闭.

So, still using python example, we commonly use a with statement which automatically ensures closure of connection with an exit method after execution of the code block nested within.

我认为WinHttp具有围绕设置的功能Cookie .其他会话信息请此处.尽管有提及

I think WinHttp has functionality around setting cookies. Additional session info here. Whilst there is mention of pointers to string

lpszHeaders:

指向包含附加标头的字符串的指针 的要求.如果该参数可以是WINHTTP_NO_ADDITIONAL_HEADERS 没有要附加的其他标题.

A pointer to a string that contains the additional headers to append to the request. This parameter can be WINHTTP_NO_ADDITIONAL_HEADERS if there are no additional headers to append.

这不是我尝试过的东西.

this is not something I have ever tried.

AFAIK 默认情况下,VBA并未随附TCP实现,尽管您可能会通过COM找到解决方案.

AFAIK VBA doesn't come with a TCP implementation by default though you may find a solution via COM.

这篇关于VBA xmlhttprequest循环而无需关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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