问题描述
请多多包涵-我正在尝试制作WinHttp.WinHttpRequest.5.1以从API取回数据。经过研究,我正在使用下面的代码。
如果我直接在浏览器中输入以下网址,则会出现一个弹出框,用于输入用户名和密码,填充后,响应将显示在页面上。使用以下代码发送请求时,我们将收到错误消息:服务器已返回401 Forbidden。 401-未经授权:由于凭据无效,访问被拒绝。 您无权使用您提供的凭据查看此目录或页面。 因此,就像用户名和密码的设置凭据一样,它不会通过请求。 我是否需要以其他方式发布登录凭据?
Dim http: Set http = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
http.Option[WinHttpRequestOption_SslErrorIgnoreFlags] = SslErrorFlag_Ignore_All
http.Option(4) = "0x3300"
Dim url: url = "https://myurl.com"
With http
Call .Open("GET",url,False)
Call .SetCredentials("username","passsword",HTTPREQUEST_SETCREDENTIALS_FOR_SERVER)
Call .Send()
End With
If Left(http.Status,1) = 2 Then
'Request succeeded with a HTTP 2xx response,do something...
Else
'Output error
Call Response.Write("Server returned: " & http.Status & " " & http.StatusText & " " &
http.ResponseText)
End If
编辑
.SetCredentials替换为
调用.setRequestHeader(“ Authorization”,“ Basic” +(username +“:” + password))
错误更改为: 服务器返回:500内部服务器错误{“ HttpStatus”:500,“ ReasonPhrase”:“ REST API异常”,“ ErrorMessage”:“输入不是有效的Base-64字符串,因为它包含非base 64字符,更多“,” ErrorType“:” System.FormatException“
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)