使用MSXML2.ServerXMLHTTP的自定义标头 [英] Custom header with MSXML2.ServerXMLHTTP

查看:574
本文介绍了使用MSXML2.ServerXMLHTTP的自定义标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在尝试使用MSXML2.ServerXMLHTTP发送POST http请求. 我需要添加一个自定义标头"Auth",以便我的请求获得授权,但似乎不起作用. 这是我的post函数的代码:

I am currently trying to use MSXML2.ServerXMLHTTP to send a POST http request. I need to add a custom header "Auth" so that my requests get authorized but it doesn't seem to work. Here is the code for my post function:

Function post(path As String, authToken As String, postData As String) As String
  Dim xhr As Object
  Dim message As String
On Error GoTo error:

  Set xhr = CreateObject("MSXML2.ServerXMLHTTP")
  xhr.Open "POST", path, False
  xhr.setRequestHeader "Content-Type", "application/json"
  xhr.setRequestHeader "Auth", authToken
  xhr.send postData

  If xhr.Status = 200 Then
    message = xhr.responseText
  Else
    message = xhr.Status & ": " & xhr.statusText
  End If

  Set xhr = Nothing
  post = message
error:
  Debug.Print "Error " & Err.Number; ":" & Err.Description
End Function

最后我遇到错误-2147012746找不到请求的标题"(由于我在计算机上使用其他语言,因此该消息实际上已翻译).

And I end up with "Error -2147012746 Requested Header was not found" (The message is actually translated since I use a different language on my computer).

但是,"Microsoft.XMLHTTP"对象没有这个问题. 我做错什么了吗?

However I didn't have this problem with the "Microsoft.XMLHTTP" Object. Am I doing something wrong ?

谢谢您的时间.

推荐答案

尝试将对SetRequestHeader的调用更改为使用字符串文字.当authToken没有设置值时,我重复了这个问题.

Try changing the call to SetRequestHeader to use a string literal. I duplicated the problem when authToken does not have a value set.

对此进行更改

xhr.setRequestHeader "Auth", authToken

对此

xhr.setRequestHeader "Auth", "testdatahere"

这篇关于使用MSXML2.ServerXMLHTTP的自定义标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆