VBA HTTP GET 请求 - 带冒号的 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17377125/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:56:54  来源:igfitidea点击:

VBA HTTP GET request - cookies with colons

vbaserverxmlhttp

提问by wlgreg

I am trying to send an HTTP GET request in VBA which includes a cookie containing a colon character, like so:

我试图在 VBA 中发送一个 HTTP GET 请求,其中包括一个包含冒号字符的 cookie,如下所示:

objReq.Open "GET", "http://my.url.com?foo=bar", False
objReq.setRequestHeader "Cookie", "abcd=cookie:containing:colons"
objReq.Send

Depending on what object type I use for objReq, however the request gets treated differently.

根据我使用的对象类型objReq,请求的处理方式有所不同。

The following object type works:

以下对象类型有效:

Dim objReq As MSXML2.ServerXMLHTTP
Set objReq = New MSXML2.ServerXMLHTTP

Unfortunately, I need to use a different object type (as MSXML2.ServerXMLHTTPcan't capture sufficient detail about HTTP redirects). From what I've read, I need to use Winhttp.WinHttpRequest, MSXML2.ServerXMLHTTP40, or MSXML2.ServerXMLHTTP60, but using any of those objects results in the following error when including colons in the cookie value.

不幸的是,我需要使用不同的对象类型(因为MSXML2.ServerXMLHTTP无法捕获有关 HTTP 重定向的足够详细信息)。从我读过的内容来看,我需要使用Winhttp.WinHttpRequest, MSXML2.ServerXMLHTTP40, 或MSXML2.ServerXMLHTTP60,但是在 cookie 值中包含冒号时,使用这些对象中的任何一个都会导致以下错误。

enter image description here

在此处输入图片说明

I have tried replacing the colons with Chr(58), %3A, and double-quoting within the string to no avail. I have also tried adding a 'Content-Type' header with various character encodings, but that doesn't seem to work either.

我曾尝试用, 和双引号替换字符串中的冒号Chr(58)%3A但无济于事。我还尝试添加具有各种字符编码的“Content-Type”标头,但这似乎也不起作用。

Anyone know how I can send a cookie value containing colons using the Winhttp.WinHttpRequest, MSXML2.ServerXMLHTTP40, or MSXML2.ServerXMLHTTP60objects?

任何人都知道我可以发送邮件使用含有冒号cookie的值Winhttp.WinHttpRequestMSXML2.ServerXMLHTTP40MSXML2.ServerXMLHTTP60对象?





PS: Alternatively, if anyone knows how I can get the ending URL of a redirect sequence when using MSXML2.ServerXMLHTTP, that would work as well! Winhttp.WinHttpRequestwould allow me to capture a 302 status code, and MSXML2.ServerXMLHTTP40or MSXML2.ServerXMLHTTP60would allow me to use GetOption(-1), but MSXML2.ServerXMLHTTPdoesn't support either of these methods (from what I can tell).

PS:或者,如果有人知道我如何在使用时获得重定向序列的结束 URL MSXML2.ServerXMLHTTP,那也可以!Winhttp.WinHttpRequest将允许我捕获 302 状态代码,MSXML2.ServerXMLHTTP40或者MSXML2.ServerXMLHTTP60允许我使用GetOption(-1),但MSXML2.ServerXMLHTTP不支持这些方法中的任何一种(据我所知)。

回答by Astrus

I did a bit of testing with WinHttpRequest and I came up with the following code:

我对 WinHttpRequest 进行了一些测试,并得出了以下代码:

Dim objReq As WinHttp.WinHttpRequest
Set objReq = New WinHttp.WinHttpRequest
objReq.Option(WinHttpRequestOption_EnableRedirects) = True
objReq.Open "GET", "http://www.example.com", False
objReq.setRequestHeader "Cookie", "abcd=cookie:containing:colons"
objReq.send

I did notice i got the same error that you posted when I forgot to include the "http://" in the url.

我确实注意到当我忘记在 url 中包含“http://”时,我收到了与您发布的相同的错误。

I hope this helps!

我希望这有帮助!