Asp Classic使用SOAP请求调用Web服务

时间:2009-05-29 15:29:29

标签: web-services asp-classic

我正在尝试从ASP.NET Web服务获取响应,而不使用get参数。我有以下代码。

strBarcode = "ABC123
strURL ="http://serverName/BarcodeGenerator.asmx"
Set xmlReq = Server.CreateObject("Msxml2.DOMDocument.3.0")
Set xmlResp = Server.CreateObject("Msxml2.DOMDocument.3.0")
Set httpReq = Server.CreateObject("MSXML2.ServerXMLHTTP") 


xmlReq.async = false
strXML = CStr(CreateRequest(strBarcode ))

xmlReq.loadXML(CStr(strXML))

//Open, async

httpReq.open "POST", CStr(strURL), true 

httpReq.setRequestHeader "Host", "serverName"
httpReq.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
httpReq.setRequestHeader "SOAPAction", "http://tempuri.org/GetBarcode"

httpReq.send(xmlReq)



strDone = "0"
bTimeout = false
dStart = Now()
dEnd = Now()
lCounter = 0
lCounterPrev = -1   
intStatus = 0
Do while intStatus <> 4 and (Not bTimeout)
   dEnd = Now()
   lCounter = DateDiff("s",dStart,dEnd)

   if lCounter > 30 then bTimeout = True       
   %>. <%      
   'Wait a second
   httpReq.waitForResponse 1000
   intStatus = httpReq.readyState
Loop

If httpReq.readyState = 4 Then
    bTimeout = false
    Set xmlResp = httpReq.responseXML
    %>
    Status: <%=httpReq.statusText%><BR>
    Response: <%=httpReq.responseText%> <BR><BR>
    <%
    Set nodes = xmlResp.getElementsByTagName("GetBarcodeResult")
    If (nodes is nothing) THen
    %>Nodes is NULL<BR><%
    Else
    %>Number of Nodes: <%=nodes.length%><%
    End IF
    Set node = nodes(0)
    url = node.nodeValue
End If

状态为

  

状态:错误请求

,回复是

  

响应:错误请求(无效主机名)

我做错了什么?

2 个答案:

答案 0 :(得分:1)

这个article(现在通过web.archive.org为后代)解释得最好,但基本上,由于IIS配置,服务器无法找到自己(经典的asp和webservice托管在同一个服务器)。代码没有问题。

答案 1 :(得分:1)

您的代码正在尝试设置Host标头本身。你不应该这样做。

ServerXMLHTTP将为您从提供的URL中绘制主机字符串。通过尝试自己添加它,您正在破坏HTTP协议的重要标准。主机是1.1协议中最有趣的标头,它是1.1请求中必须存在的唯一标头。

我不确定为什么要使用异步请求和WaitForResponse来检测超时。为什么不使用setTimeouts方法和同步请求?