VBA XMLHTTP 清除身份验证? [英] VBA XMLHTTP clear authentication?

查看:39
本文介绍了VBA XMLHTTP 清除身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一组 VBA 宏,其中使用 XMLHTTP 对象向服务器发送异步请求.我正在发送基本身份验证:

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:

XMLHttpReq.setRequestHeader "Authorization","Basic " & Base64EncodedUserPass

这第一次效果很好.但是,如果用户更改了他们的用户 ID/密码,即使代码创建了一个全新的 XMLHttpReq 对象并将此标头设置为新信息,它也会以第一个用户身份登录到服务器,大概是从缓存的凭据中获取的.

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.

如何让代码忘记"我之前登录过,重新授权?

How can I cause the code to "forget" that I have logged in before, and re-authorize?

编辑,代码的相关部分;它真的不是很复杂:

Edit as 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

推荐答案

由于主要浏览器对缓存方法的实现不同,这些问题已经被以多种方式讨论过.

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

似乎Cache-Control 适用于大多数浏览器,Pragma 仅适用于 Firefox 而不是 IE(不知道为什么...)

It seems that Cache-Control works on most browsers and Pragma only on Firefox and not IE (don't know why...)

If-Modified-Since 用于 IE,因为 IE 在自己的算法中使用不同的设置来确定是否应该缓存请求.XMLHttpRequest 似乎与 HTTP 响应不同.

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.

小心:使用此代码,每次创建新对象时,您都需要 usernamepassword.也许您应该创建一个新对象,将其实例化一次,然后在使用后销毁它.在这两者之间,您将在不同的函数中处理所有请求,只需进行一次身份验证.

Careful : With this code you will need username 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.

MSDN setRequestHeader 方法

MSDN IXMLHTTPRequest

XMLHTTPREQUEST 缓存测试

XMLHttpRequest 和缓存

这篇关于VBA XMLHTTP 清除身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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