Excel VBA Msxml2.XMLHTTP.6.0与Msxml2.ServerXMLHTTP.6.0

我是一个自学成才的业余程序员,我是这个论坛的新手。 请多多包涵。

大约两年前,我写了一个简单的Excel vba程序来login到一个网站,并以.csv文件的forms获取客户声明。 我的程序利用GET和POST请求。 这个程序在三周前完全运行(为了我的需要),当时不幸的是我断了。 该程序无法通过最初的GET请求。 具体来说,它会在getReq.send行中断开。

我碰到这个职位: login到网站使用MSXML2.XMLHTTP而不是InternetExplorer.Application与VBA

在这里,我了解到您可以使用“Msxml2.XMLHTTP.6.0”而不是“Msxml2.ServerXMLHTTP.6.0”。 我相应地修改了我的代码,无需在Get请求之后parsingCookie,而且工作正常! 但我不知道。 即使我把它运用起来,我也不觉得在这个过程中学到了很多东西。

一些资料要注意:

  • 我原来的程序打破了我的工作电脑(WindowsXP)。
  • 考虑到这可能是一个XP的问题,无论如何在新机器的市场,我更新到一台运行Windows7的新电脑。 该程序仍然没有工作,虽然我收到了不同的错误信息。
  • 我在Windows10电脑上运行我的代码,它运行良好。
  • 我使用相同的代码连接到各种其他网站,它工作正常,不pipe什么操作系统。

所以,我的具体问题是:

  1. 为什么代码可以使用Msxml2.XMLHTTP.6.0而不是Msxml2.ServerXMLHTTP.6.0?
  2. 为什么代码首先会被打破?
  3. 为什么代码在一个特定的网站上工作,但没有另一个?

任何有识之士将不胜感激。 我已经附上我的代码(login信息X'd了)。

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 

欢迎来到VBA和StackOverflow。 你的笔记是彻底的,所以我build议的唯一的事情就是检查你的代理设置。

https://support.microsoft.com/en-us/help/289481/you-may-need-to-run-the-proxycfg-tool-for-serverxmlhttp-to-work

这个链接被埋在这个链接

https://support.microsoft.com/en-us/help/290761/frequently-asked-questions-about-serverxmlhttp

ComIntern提到你

这个博客文章说,ServerXLMHTTP不能通过客户端上的代理工作,但XMLHTTP将会。 在Excel VBA中,我使用的是ServerXLMHTTP.6.0,它在公司networking中的一些客户端失败,而XMLHTTP工作。