Excel VBA Msxml2.XMLHTTP.6.0 与 Msxml2.ServerXMLHTTP.6.0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41026800/
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
Excel VBA Msxml2.XMLHTTP.6.0 vs Msxml2.ServerXMLHTTP.6.0
提问by AdamK33
I am a self-taught, amateur programmer, and I am new to this forum. Please bear with me.
我是一个自学成才的业余程序员,我是这个论坛的新手。请多多包涵。
About two years ago, I wrote a simple Excel vba program to login in to a website and grab a customer statement in the form of a .csv file. My program utilizes GET and POST requests. This program worked perfectly (for my needs) until about three weeks ago, when it unfortunately broke on me. The program could not get through the initial GET request. Specifically, it would break on the getReq.send line.
大约两年前,我编写了一个简单的 Excel vba 程序来登录网站并以 .csv 文件的形式获取客户声明。我的程序使用 GET 和 POST 请求。这个程序运行完美(满足我的需要),直到大约三周前,不幸的是它在我身上坏了。程序无法通过初始 GET 请求。具体来说,它会在 getReq.send 行中断。
I came across this post: Login into website using MSXML2.XMLHTTP instead of InternetExplorer.Application with VBA
我遇到了这篇文章: Login into website using MSXML2.XMLHTTP 而不是 InternetExplorer.Application with VBA
Here, I learned that you can use "Msxml2.XMLHTTP.6.0" instead of "Msxml2.ServerXMLHTTP.6.0". I modified my code accordingly, eliminating the need to parse cookies after the Get request, and it worked! But I have no idea. Even though I got it to work, I do not feel like I have learned much in the process.
在这里,我了解到您可以使用“Msxml2.XMLHTTP.6.0”而不是“Msxml2.ServerXMLHTTP.6.0”。我相应地修改了我的代码,消除了在 Get 请求之后解析 cookie 的需要,并且成功了!但我不知道。即使我开始工作,我也不觉得我在这个过程中学到了很多东西。
Some information to note:
一些需要注意的信息:
- My original program broke on my work computer (WindowsXP).
- Figuring that it may be an XP issue, and in the market for a new machine anyway, I updated to a new computer running Windows7. The program still did not work, though I received a different error message.
- I ran my code on a Windows10 computer and it worked fine.
- I use identical code to connect to various other websites and it works fine, regardless of what operating system.
- 我原来的程序在我的工作计算机 (WindowsXP) 上坏了。
- 想着可能是XP的问题,又想换新机,我就更新到了一台运行Windows7的新电脑。该程序仍然无法运行,但我收到了不同的错误消息。
- 我在 Windows10 计算机上运行我的代码,它运行良好。
- 我使用相同的代码连接到各种其他网站,无论使用什么操作系统,它都能正常工作。
So, my specific questions:
所以,我的具体问题:
- Why might the code work with Msxml2.XMLHTTP.6.0 but not Msxml2.ServerXMLHTTP.6.0?
- And why might the code have broken in the first place?
- Why would the code work on one particular website, but no another?
- 为什么代码可以使用 Msxml2.XMLHTTP.6.0 而不是 Msxml2.ServerXMLHTTP.6.0?
- 为什么代码一开始就被破坏了?
- 为什么代码可以在一个特定的网站上运行,而不能在另一个网站上运行?
Any insight would be greatly appreciated. I have attached my code (with login info X'd out).
任何见解将不胜感激。我已附上我的代码(登录信息已删除)。
Sub RCGInquiry()
Dim postReq, getReq, cookies
Dim p0 As Integer, p1 As Integer, temp As String
Dim result As String, respHead As String
Set getReq = CreateObject("Msxml2.ServerXMLHTTP.6.0")
'Set getReq = CreateObject("Msxml2.XMLHTTP.6.0")
' Visit homepage so we can find the cookies
getReq.Open "GET", "https://www.rcginquiry.com/sfs/Entry", False
getReq.send
respHead = getReq.getAllResponseHeaders
Debug.Print respHead
' Need to parse the cookie from Respone Headers
cookies = ""
p0 = 1
Do While InStr(p0, respHead, "Set-Cookie:") > 0
p0 = InStr(p0, respHead, "Set-Cookie:") + 11
p1 = InStr(p0, respHead, Chr(10))
temp = Trim(Mid(respHead, p0, p1 - p0))
cookies = cookies & temp & "; "
Loop
cookies = Left(cookies, Len(cookies) - 2)
' Debug.Print cookies
' Login
Set postReq = CreateObject("Msxml2.ServerXMLHTTP.6.0")
'Set postReq = CreateObject("Msxml2.XMLHTTP.6.0")
postReq.Open "POST", "https://www.rcginquiry.com/sfs/Entry", False
postReq.setRequestHeader "Cookie", cookies
postReq.setRequestHeader "Content-type", "application/x-www-form-urlencoded" 'send appropriate Headers
postReq.send "Usrid=XXXX&Psswd=XXXX" ' send login info
'-------------------------------------------------------------------------------
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim FSO As Object
Dim myFile As Object
Dim path As String
Dim y As Integer
curDate = Format(Date, "mm_dd_yy")
' Download CSV
postReq.Open "POST", "https://www.rcginquiry.com/sfs/Downloads/tmp.csv?filetype=POS&format=MFA20&heading=true&allaccts=true&junk=tmp.csv", False
postReq.setRequestHeader "Cookie", cookies 'must resend cookies so it knows i am logged in
postReq.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
postReq.send "filetype=POS&format=MFA20&heading=true&allaccts=true&junk=temp.csv" 'url query parameters
' Writes responseText to a .csv file
Set FSO = CreateObject("Scripting.FileSystemObject")
Set myFile = FSO.createtextfile("C:\Users\Adam\Desktop\POSITION\" & curDate & ".csv", True)
myFile.write (postReq.responseText)
myFile.Close
Set FSO = Nothing
Set myFile = Nothing
End Sub
回答by S Meaden
Welcome to VBA and StackOverflow. Your notes are thorough and so the only thing I can suggest is that you check your proxy settings.
欢迎使用 VBA 和 StackOverflow。你的笔记很详尽,所以我唯一能建议的是你检查你的代理设置。
That link was buried in this link
那个链接被埋在这个链接里
https://support.microsoft.com/en-us/help/290761/frequently-asked-questions-about-serverxmlhttp
https://support.microsoft.com/en-us/help/290761/frequently-asked-questions-about-serverxmlhttp
which you were referred to by ComIntern
ComIntern提到你
回答by user3142056
This blog postsays that ServerXLMHTTP will not work through a proxy on the client, but XMLHTTP will. In Excel VBA I was using ServerXLMHTTP.6.0, and it was failing for some clients inside corporate networks, whereas XMLHTTP worked.
这篇博客文章说 ServerXLMHTTP 不会通过客户端上的代理工作,但 XMLHTTP 会。在 Excel VBA 中,我使用的是 ServerXLMHTTP.6.0,对于公司网络内的某些客户端,它失败了,而 XMLHTTP 却可以工作。