Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA setRequestHeader";“授权”;弱点_Vba_Excel_Http_Msxml_Winhttp - Fatal编程技术网

VBA setRequestHeader";“授权”;弱点

VBA setRequestHeader";“授权”;弱点,vba,excel,http,msxml,winhttp,Vba,Excel,Http,Msxml,Winhttp,我试图用下面的代码连接到Web数据库,但在VBA中自动运行时,它似乎不起作用。登录名和密码都很好,因为我可以手动连接它们 对象“WinHttp.WinHttpRequest.5.1”是否可能不适用于这种数据库连接?或者我的Connect sub中缺少一个参数?在这件事上的任何帮助都将不胜感激 Sub Connect() Dim oHttp As Object Set oHttp = CreateObject("WinHttp.WinHttpRequest.5.1") Call oHttp.Op

我试图用下面的代码连接到Web数据库,但在VBA中自动运行时,它似乎不起作用。登录名和密码都很好,因为我可以手动连接它们

对象“WinHttp.WinHttpRequest.5.1”是否可能不适用于这种数据库连接?或者我的Connect sub中缺少一个参数?在这件事上的任何帮助都将不胜感激

Sub Connect()

Dim oHttp As Object
Set oHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
Call oHttp.Open("GET", "http://qrdweb/mg/loan/loans.html?show=all", False)

oHttp.setRequestHeader "Content-Type", "application/xml"
oHttp.setRequestHeader "Accept", "application/xml"
oHttp.setRequestHeader "Authorization", "Basic " + Base64Encode("login123" +  ":" + "pass123")


Call oHttp.send

Sheets("Sheet1").Cells(1, 1).Value = oHttp.getAllResponseHeaders
Sheets("Sheet1").Cells(1, 2).Value = oHttp.ResponseText

End Sub

Private Function Base64Encode(sText)
Dim oXML, oNode
Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
Set oNode = oXML.createElement("base64")
oNode.DataType = "bin.base64"
oNode.nodeTypedValue = StringToBinary(sText)


Base64Encode = oNode.Text
Set oNode = Nothing
Set oXML = Nothing
End Function

Private Function StringToBinary(Text)
Const adTypeText = 2
Const adTypeBinary = 1

Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")

BinaryStream.Type = adTypeText
BinaryStream.Charset = "us-ascii"
BinaryStream.Open
BinaryStream.WriteText Text

'Change stream type To binary
BinaryStream.Position = 0
BinaryStream.Type = adTypeBinary

'Ignore first two bytes - sign of
BinaryStream.Position = 0

StringToBinary = BinaryStream.Read

Set BinaryStream = Nothing
End Function
显示getAllResponseHeaders的oHttp.getAllResponseHeaders输出以下信息:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
        <title>Error 401 Server Error</title>
    </head>
    <body>
<html>

    <head>

        <title>M&M - Loan Viewer</title>

        <script language="javascript" type="text/javascript">

            function showTransactionComments(loanId, date, type, commentsTableWidth) {

    //alert(loanId + " " + date + " " + type + " " + commentsTableWidth);
    if (window.ActiveXObject) {
        return;
缓存控制:必须重新验证,无缓存,无存储

连接:保持活力

日期:2017年2月24日星期五17:19:54 GMT

内容长度:30633

内容类型:text/html;字符集=ISO-8859-1

服务器:nginx/1.11.6

WWW-Authenticate:Digest realm=“QRDWEB-MNM”,domain=“”,nonce=“aB5DLmvuCfok9Zo112jo4S0evgOuXntE”,algorithm=MD5,qop=“auth”,stale=true

显示ResponseText的oHttp.ResponseText输出以下信息:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
        <title>Error 401 Server Error</title>
    </head>
    <body>
<html>

    <head>

        <title>M&M - Loan Viewer</title>

        <script language="javascript" type="text/javascript">

            function showTransactionComments(loanId, date, type, commentsTableWidth) {

    //alert(loanId + " " + date + " " + type + " " + commentsTableWidth);
    if (window.ActiveXObject) {
        return;
根据JScript示例,身份验证似乎需要在同一连接上有两对成功的
Open
/
Send
对。第一个通知HTTP请求对象需要摘要身份验证,而第二个实际执行此操作。尝试以下方法(未测试):

Sub-digest()
将http设置为WinHttpRequest'***而不是“新建”-您可以在下面执行此操作
作为字符串的Dim strResponse
设置http=New-WinHttpRequest
http.Open“GET”http://qrdweb/mg/loan/loans.html?show=all”“错
http.Send'***请先在没有身份验证的情况下尝试
如果是http.Status 401,则退出Sub'***或执行其他操作
http.Open“GET”http://qrdweb/mg/loan/loans.html?show=all”“错
'***另一个打开,与JScript示例相同
http.SetCredentials“login123”,“pass123”,用于\u服务器的HTTPREQUEST\u SetCredentials\u
http.Send
MsgBox CStr(http.Status)&“&http.StatusText'***仅用于检查
Sheets(“Sheet1”).单元格(1,1).Value=http.getAllResponseHeaders
Sheets(“Sheet1”).单元格(1,2).Value=http.ResponseText
“***不知道这两行是干什么用的——我已经把它们注释掉了。”
'http.Open“PROPFIND”http://qrdweb/mg/loan/loans.html?show=all”“错
'http.send
端接头

在我看来,服务器需要摘要身份验证(
WWW-Authenticate:Digest
)。您正在提供服务器可能不愿意接受的基本身份验证。您是否尝试过在VBA代码中使用摘要?我发现它看起来很有用。嗨,谢谢你的指导。我没有摘要身份验证的经验。我正在研究这个话题。connexion更新:我目前正在尝试一种新的登录形式(见下文),我得到了两种可能的结果:第一种结果是使用错误的登录信息时出现相同的401错误,并且立即返回。但是,当我提供正确的登录信息时,操作超时。。。可能是什么原因造成的?代码:Sub-digest()Dim http作为新的WinHttpRequest Dim strResponse作为字符串集http=New WinHttpRequest http。打开“GET”,“False http.SetCredentials”“login123”,“Pass123”,http服务器http.send End SubOk的HTTPREQUEST\u SETCREDENTIALS\u我已更新了Edit2下的主线程