问题
I am trying to send an HTTP GET request in VBA which includes a cookie containing a colon character, like so:
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.
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.ServerXMLHTTP
can'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.
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.
Anyone know how I can send a cookie value containing colons using the Winhttp.WinHttpRequest
, MSXML2.ServerXMLHTTP40
, or MSXML2.ServerXMLHTTP60
objects?
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.WinHttpRequest
would allow me to capture a 302 status code, and MSXML2.ServerXMLHTTP40
or MSXML2.ServerXMLHTTP60
would allow me to use GetOption(-1)
, but MSXML2.ServerXMLHTTP
doesn't support either of these methods (from what I can tell).
回答1:
I did a bit of testing with WinHttpRequest and I came up with the following code:
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.
I hope this helps!
来源:https://stackoverflow.com/questions/17377125/vba-http-get-request-cookies-with-colons