XMLHTTP 缓存:Xml 在第一次请求 Excel VBA 后带来相同的数据

Ces*_*sna 6 xml excel vba caching

我正在使用XMLHTTP. 我注意到当我第一次打开 Excel 文件时它读取得很好,但是当 XML 更改并再次运行宏时,它会引入与以前相同的数据。我做了一些关于如何停止缓存的研究。我读到添加一个随机数作为额外参数可以解决它,但对我不起作用。

有任何想法吗?

Sub MLB_PinnyParser()

Dim Req As New XMLHTTP
Dim Resp As New DOMDocument
Req.Open "GET", "http://xml.pinnaclesports.com/pinnaclefeed.aspx?sporttype=Baseball&sportsubtype=MLB", False
Req.send
Resp.LoadXML Req.responseText

 For Each Event In Resp.getElementsByTagName("event")

'More code here

 Next Event

Set Req = Nothing
Set Resp = Nothing

End Sub
Run Code Online (Sandbox Code Playgroud)

Bon*_*ond 6

您通常可以通过修改每个请求的 URL 来克服这个问题。只需添加一个随机数作为查询字符串参数。例如:

' Seed the RNG somewhere at the start of your app...
Randomize

...

Sub MLB_PinnyParser()

    ' Generate a random, six-digit number...
    Dim intRand As Long
    intRand = Int((900000) * Rnd) + 100000

    ' Add the number as a param to the request...
    Dim strUrl As String
    strUrl = "http://xml.pinnaclesports.com/pinnaclefeed.aspx?sporttype=Baseball&sportsubtype=MLB" & "&blah=" & intRand

    Dim Req As New XMLHTTP
    Req.Open "GET", strUrl, False
    ...

End Sub
Run Code Online (Sandbox Code Playgroud)

由于听起来您已经尝试过此操作,请尝试在您的请求中添加几个与缓存相关的标头,以查看是否有所不同:

Req.SetRequestHeader "Cache-Co­ntrol", "no-cache,max-age=0"
Req.SetRequestHeader "pragma", "no-cache"
Run Code Online (Sandbox Code Playgroud)