[我正在尝试使用一些经典的ASP从NHS API中提取数据(我很害怕,但我正在努力将订阅密钥成功传递给API。
说明如下:
来自https://developer.api.nhs.uk/documentation/content-api
所以,我写了以下...
<%
Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
xml.Open "GET", "https://api.nhs.uk/conditions/abdominal-aortic-aneurysm-screening/", False
on error resume next
xml.setRequestHeader "subscription‑key", "MY-API-KEY-HERE"
xml.setRequestHeader "Content-Type", "application/json"
xml.setRequestHeader "Accept", "application/json"
xml.Send
Response.Write "<h1>The HTML text</h1><xmp>"
Response.Write xml.responseText
Set xml = Nothing
%>
这只会给我以下响应:{“ statusCode”:401,“ message”:“由于缺少订阅密钥而导致访问被拒绝。向API发出请求时,请确保包括订阅密钥。 }
它们具有5种不同语言的示例脚本,但没有ASP甚至ASP.NET
任何想法,我可以尝试使它正常工作吗?
谢谢
编辑
尝试此处建议的方法How can I post data using cURL in asp classic? ...
<%
Dim http: Set http = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
Dim url: url = "https://api.nhs.uk/conditions/abdominal-aortic-aneurysm-screening/"
'Dim data: data = "something=this" - took this out as its a querystring for POST
With http
Call .Open("GET", url, False)
'Call .SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
Call .SetRequestHeader("subscription‑key", "MY-API-KEY-HERE")
'Call .Send(data) <- the data was the querystring, so not relevant here
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)
End If
%>
这给了我无效的过程调用或参数:'SetRequestHeader'
编辑解决方案
已解决连字符问题的工作代码...
<%
Dim http: Set http = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
Dim url: url = "https://api.nhs.uk/conditions/abdominal-aortic-aneurysm-screening/"
With http
Call .Open("GET", url, False)
Call .SetRequestHeader("subscription-key", "MYKEYHERE")
Call .Send()
End With
If Left(http.Status, 1) = 2 Then
'Request succeeded with a HTTP 2xx response, do something...
Response.Write http.responseText
Else
'Output error
Call Response.Write("Server returned: " & http.Status & " " & http.StatusText)
End If
%>
感谢Lankymart!
对重复的示例进行了尝试,然后返回
无效的过程调用或参数:'SetRequestHeader'
这让我感到困惑,因为该代码之前已经过测试并且可以正常工作,那么有什么变化吗?因此,我深入研究了SetRequestHeader
方法调用。
证明错误仅在此行上发生;
Call .SetRequestHeader("subscription‑key", "MY-API-KEY-HERE")
最后,从标题名称中删除了subscription‑
,它的工作没有引起编译错误。
这导致我使用Asc("‑")
来检查代码中的连字符,并将其与标准连字符进行比较,并确定它们之间是否有区别。
<%
Response.Write Asc("‑") & "<br />" 'From the code
Response.Write Asc("-") & "<br />" 'Standard hyphen
%>
输出:
-15454
45
用标准连字符替换字符,错误消失了,代码开始返回;
Server returned: 401 Unauthorized