编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

Excel-VBA REST WCF可在第一个调用上使用,但后续调用将返回缓存的(非当前)数据:Excel-VBA REST WCF works on 1st call, but subsequent calls return cached (non-current) data

anwojcie 网络运维 2022-5-11 10:44 8人围观

腾讯云服务器
Excel-VBA REST WCF可在第一个调用上使用,但后续调用将返回缓存的(非当前)数据的处理方法

我有以下表格的基本WCF客户端服务.第一次调用它时,它将从远程服务器返回正确的数据.但是,数据经常更改.在后续调用中,它返回与第一次调用中相同的数据.因此,Excel似乎返回缓存的数据.如果我从Fiddler或浏览器运行相同的WCF过程,它似乎总是返回当前/刷新的数据(因此,我认为问题出在Excel中,而不是服务器中).如何强制Excel VBA刷新"调用而不是从缓存中获取数据?注意:这将分发给各种最终用户,因此我无法更改客户端配置.

I have a basic WCF client service in the form below. The first time it is called, it returns the correct data from the remote server. However the data changes frequently. On subsequent calls, it returns the same data that it did on the first call. So, Excel appears to return cached data. If I run the same WCF procedure from Fiddler or a browser, it seems to always return current/refreshed data (so I think the issue is in Excel, not the server). How can I force Excel VBA to "refresh" the call instead of getting the data from cache? Note: this will be distributed to a variety of end users, so I cannot make client config changes.

Public Function CallWCF() As String Dim HttpReq As Object Set HttpReq = CreateObject("MSXML2.XMLHTTP") HttpReq.Open "GET", "http://ws.mydomain.com/Rest.svc/getmydata" Call HttpReq.Send Do While HttpReq.readyState <> 4 DoEvents Loop Dim resp As String resp = HttpReq.ResponseText CallWCF = resp Set HttpReq = Nothing End Function 

问题解答

来自 stackoverflow.com/q/11526810/190829,似乎添加了: httpReq.setRequestHeader"If-Modified-Since",星期六,2000年1月1日,00:00:00 GMT" 解决了我PC上的问题(不知道它是否可以在具有不同浏览器的所有PC上运行).其他2: HttpReq.setRequestHeader缓存控件",无缓存" HttpReq.setRequestHeader"Pragma","no-cache" 似乎没有帮助.

From stackoverflow.com/q/11526810/190829, it appears that adding: httpReq.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" solved the issue on my PC (don't know if it will work on all PCs with different browsers). The other 2: HttpReq.setRequestHeader "Cache-Control", "no-cache" and HttpReq.setRequestHeader "Pragma", "no-cache" did not seem to help.

我还尝试在URL后面附加一个伪参数,该伪参数会更改每个请求(例如时间或随机数),并且该参数也有效.但是我认为这将需要更改我所有操作的服务器代码.

I also tried appending the URL with a dummy parameter that changes each request (such as the time or random number) and that also works. However I think it will require changing the server code for all of my operations.

这篇关于Excel-VBA REST WCF可在第一个调用上使用,但后续调用将返回缓存的(非当前)数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^