VBA XMLHTTP 清除身份验证?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11526810/
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
VBA XMLHTTP clear authentication?
提问by Josh
I am writing a set of VBA macros in which it uses the XMLHTTP object to send asynchronous requests to a server. I am sending Basic Authentication with:
我正在编写一组 VBA 宏,其中它使用 XMLHTTP 对象向服务器发送异步请求。我正在发送基本身份验证:
XMLHttpReq.setRequestHeader "Authorization","Basic " & Base64EncodedUserPass
This works great the first time. But if the user changes their userid/password, even if the code creates a brand new XMLHttpReq object and sets this header to the new information, it logs in to the server as the first user, presumably from cached credentials.
第一次效果很好。但是,如果用户更改了他们的用户 ID/密码,即使代码创建了一个全新的 XMLHttpReq 对象并将此标头设置为新信息,它也会作为第一个用户登录到服务器,大概是从缓存的凭据。
How can I cause the code to "forget" that I have logged in before, and re-authorize?
如何让代码“忘记”我之前登录过并重新授权?
Editas requested, the relevant part of the code; it really isn't very complicated:
根据要求编辑代码的相关部分;它真的不是很复杂:
myURL = "http://my.domain.com/myscript.cgi"
Dim oHttp As New MSXML2.XMLHTTP
oHttp.Open "POST", myURL, False
oHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded'"
oHttp.setRequestHeader "Authorization","Basic " & Base64EncodedUsernamePassword
oHttp.send "PostArg1=PostArg1Value"
Result = oHttp.responseText
回答by Romain
These questions have been discussed in many ways due to major browsers different implementations of caching methods.
由于主要浏览器对缓存方法的实现不同,这些问题已经以多种方式进行了讨论。
I will give you what worked for me and then the sources I found on this feature.
我会给你什么对我有用,然后是我在这个功能上找到的来源。
The only solution I could came across was to force the browser to not cache the request.
我遇到的唯一解决方案是强制浏览器不缓存请求。
myURL = "http://my.domain.com/myscript.cgi"
Dim oHttp As New MSXML2.XMLHTTP
oHttp.Open "POST", myURL, False
oHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded'"
oHttp.setRequestHeader("Cache-Control", "no-cache");
oHttp.setRequestHeader("Pragma", "no-cache");
oHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
oHttp.setRequestHeader "Authorization","Basic " & Base64EncodedUsernamePassword
oHttp.send "PostArg1=PostArg1Value"
Result = oHttp.responseText
It seems that Cache-Control
works on most browsers and Pragma
only on Firefox and not IE (don't know why...)
似乎Cache-Control
适用于大多数浏览器,并且Pragma
仅适用于 Firefox 而不是 IE(不知道为什么......)
If-Modified-Since
is used for IE, since IE uses different settings in his own algorithm to determine whether or not the request should be cached. XMLHttpRequest seem to not be treated as the same as HTTP responses.
If-Modified-Since
用于IE,因为IE在自己的算法中使用不同的设置来确定是否应该缓存请求。XMLHttpRequest 似乎与 HTTP 响应不同。
Careful: With this code you will needusername
and password
each time a new object is created. Maybe you should create a new object, instantiate it once and then destroy it after use. In between you would have all your requests handled in different functions with only one authentication.
小心:有了这个代码,您将需要username
与password
各创建一个新的对象时。也许您应该创建一个新对象,将其实例化一次,然后在使用后销毁它。在这两者之间,您将在不同的功能中处理所有请求,只需进行一次身份验证。