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

时间:2016-12-07 20:29:04

标签: excel vba windows-7

我是一名自学成才的业余程序员,我是这个论坛的新手。请耐心等待。

大约两年前,我写了一个简单的Excel vba程序来登录网站,并以.csv文件的形式获取客户声明。我的程序使用GET和POST请求。这个程序完美地(根据我的需要)工作到大约三个星期前,当它不幸地打破了我。该程序无法完成初始GET请求。具体来说,它会破坏getReq.send行。

我遇到过这篇文章: Login into website using MSXML2.XMLHTTP instead of InternetExplorer.Application with VBA

在这里,我了解到你可以使用" Msxml2.XMLHTTP.6.0"而不是" Msxml2.ServerXMLHTTP.6.0"。我相应地修改了我的代码,消除了在Get请求之后解析cookie的需要,并且它有效!但我不知道。即使我开始工作,我也不觉得自己在这个过程中学到了很多东西。

需要注意的一些信息:

  • 我的原始程序在我的工作计算机上崩溃了(WindowsXP)。
  • 确定它可能是一个XP问题,无论如何在新机器的市场中,我更新到运行Windows7的新计算机。该程序仍然无法正常工作,但我收到了不同的错误消息。
  • 我在Windows10计算机上运行了我的代码并且运行正常。
  • 我使用相同的代码连接到其他各种网站,无论使用何种操作系统,都可以正常使用。

所以,我的具体问题:

  1. 为什么代码可以使用Msxml2.XMLHTTP.6.0但不能使用Msxml2.ServerXMLHTTP.6.0?
  2. 为什么代码可能首先崩溃了?
  3. 为什么代码可以在一个特定的网站上运行,而不是另一个网站?
  4. 非常感谢任何见解。我附上了我的代码(登录信息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
    

2 个答案:

答案 0 :(得分:0)

欢迎使用VBA和StackOverflow。您的笔记是彻底的,因此我唯一可以建议您检查您的代理设置。

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

引用了你

答案 1 :(得分:0)

这个blog post表示ServerXLMHTTP不能通过客户端上的代理工作,但XMLHTTP会。在Excel VBA中,我使用的是ServerXLMHTTP.6.0,它在企业网络中的某些客户端失败,而XMLHTTP工作。