4

我编写了一个 ASP 脚本来生成 JSON 数据/字符串。如何将该数据发送到 Web 服务?我得到了以下信息,但我无权访问服务器来注册任何 dll 文件。我做了一些搜索,发现我应该使用 XMLHttpRequest 但不知道该怎么做。

请帮忙。谢谢你。

连接到 Web 服务 Web 服务通过 HTTP 协议工作。建议此 Web 服务在投入生产后使用安全套接字层 (HTTPS)。Web 服务旨在使用以下 URL:http ://thedomain.com/api/push

该站点在标头中使用身份验证令牌来防止爬虫干扰 Web 服务。发送数据时,添加一个名为 HTTP_TOKENKEY 的标头,其值为 ABCDEFGHIJKL。这是推荐使用 HTTPS 的原因之一。Web 服务将在请求的 POST 中查找 JSON 数据并将 JSON 响应发送回客户端。

这就是我到目前为止所拥有的


strJSONToSend = theevent 

webserviceurl = "http://thedomain.com/api/push"

Set objRequest = Server.createobject("MSXML2.XMLHTTP.3.0") 
objRequest.open "POST", webserviceurl, False 

objRequest.setRequestHeader "Content-Type", "application/json; charset=UTF-8" 
objRequest.setRequestHeader "HTTP_TOKENKEY","ABCDEFGHIJKLMNOPQ" 
objRequest.setRequestHeader "SOAPAction", webserviceurl

results = objRequest.send (strJSONToSend)
write (results)

set objJSONDoc = nothing 
set objResult = nothing

这冻结了,什么都不做

4

1 回答 1

2

这是我之前在此 URL 上发布的一些代码,适用于您的情况:

http://naterice.com/articles/69

strJSONToSend = theevent 
webserviceurl = "http://thedomain.com/api/push"

sResponseHTML = GetHTTP(strJSONToSend, webserviceurl)
If len(HTTPErrorHandeler) > 0 Then
  strResponse = HTTPErrorHandeler
Else
  strResponse = sResponseHTML
End If

Response.Write strResponse

Function GetHTTP(sSendHTML, sURL)
  'This script is provided under the Creative Commons license located'
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not'
  'be used for commercial purposes with out the expressed written consent'
  'of NateRice.com'
  Set oHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")
  oHTTP.Open "POST", sURL, false
  oHTTP.setRequestHeader "Content-Type", "application/json; charset=UTF-8" 
  oHTTP.setRequestHeader "HTTP_TOKENKEY","ABCDEFGHIJKLMNOPQ" 
  oHTTP.setRequestHeader "SOAPAction", webserviceurl

  On Error Resume Next
  oHTTP.send sSendHTML
  sHTTPResponse = oHTTP.responseText

  If Err.Number = 0 Then
    GetHTTP = sHTTPResponse
  Else
    GetHTTP = HTTPErrorHandeler
  End If
  On Error Goto 0

  Set oHTTP = Nothing
End Function


Function HTTPErrorHandeler
  'This script is provided under the Creative Commons license located'
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not'
  'be used for commercial purposes with out the expressed written consent'
  'of NateRice.com'
  If Err.Number <> 0 Then
  HTTPErrorHandeler = "ERROR <br />" & _
             "  ERR Number: " & Err.Number & " <br />" & _
             "  ERR Description: " & Err.Description
  Else
  HTTPErrorHandeler = ""
  End If
End Function
于 2014-03-19T18:21:37.970 回答