将订阅密钥作为请求 header 传递给 msxml2.ServerXMLHTTP - 经典 ASP/VB

Passing a subscription key as a request header with msxml2.ServerXMLHTTP - Classic ASP/VB

我正在尝试使用一些经典的 ASP 从 NHS API 中提取数据(我所知道的恐怕)但我正在努力成功地将订阅密钥传递给API.

说明如下:

  1. 在 NHS 网站上选择一个页面,例如:https://www.nhs.uk/conditions/acne
  2. 记下路径,例如:conditions/acne.
  3. 使用 curl、Postman 或您的网络浏览器等工具,使用有效的订阅密钥向 https://api.nhs.uk/content/acne 发出 GET 请求:请求中的 {subscription-key} [=74] =].
  4. 您将收到使用 schema.org 构建的 JSON 响应,相关字段在以下文档中进行了说明....

来自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": "Access denied due to missing subscription key. Make sure to include subscription key when making requests to an API." }

他们有 5 种不同语言的示例脚本,但 ASP 甚至 ASP.NET

都没有

有什么想法可以使它正常工作吗?

谢谢

编辑

尝试此处建议的方法 ...

<%
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!

尝试了您对重复示例的理解,结果返回了

Invalid procedure call or argument: 'SetRequestHeader'

这让我感到困惑,因为该代码之前已经过测试并且工作正常,所以发生了什么变化? 所以我深入研究了 SetRequestHeader 方法调用。

原来错误只发生在这一行;

Call .SetRequestHeader("subscription‑key", "MY-API-KEY-HERE")

最后,从 header 名称中删除了 subscription‑,并且没有导致编译错误。

这促使我使用 Asc("‑") 检查代码中的连字符,并将其与标准连字符进行比较,果然它们是不同的。

<%
Response.Write Asc("‑") & "<br />" 'From the code
Response.Write Asc("-") & "<br />" 'Standard hyphen
%>

输出:

-15454
45

用标准连字符替换了字符,错误消失,代码运行返回;

Server returned: 401 Unauthorized