Web services 使用ServerXMLHTTP连接到web服务

Web services 使用ServerXMLHTTP连接到web服务,web-services,asp-classic,httprequest,Web Services,Asp Classic,Httprequest,我有一个经典的asp文件,需要连接到web服务。 我对如何在SOAPAction中定义web服务的URL和名称空间感到困惑。 当我运行我的代码并编写响应时。为我在web服务中调用的方法的返回值编写,它会返回wsdl或服务的网页 此代码显示web服务html,就像输入web服务一样。svc url: Dim strSoapReq strSoapReq = "<?xml version=""1.0"" encoding=""utf-8"" ?>" strSoapReq = strSoap

我有一个经典的asp文件,需要连接到web服务。 我对如何在SOAPAction中定义web服务的URL和名称空间感到困惑。 当我运行我的代码并编写
响应时。为我在web服务中调用的方法的返回值编写
,它会返回wsdl或服务的网页

此代码显示web服务html,就像输入web服务一样。svc url:

Dim strSoapReq
strSoapReq = "<?xml version=""1.0"" encoding=""utf-8"" ?>"
strSoapReq = strSoapReq & "<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">"
strSoapReq = strSoapReq & "<s:Body>"

strSoapReq = strSoapReq & "<TestMethod xmlns=""http:<serverName:<port>/PagingService/PagingService"">"
strSoapReq = strSoapReq & "</TestMethod>"
strSoapReq = strSoapReq & "</s:Body>"
strSoapReq = strSoapReq & "</s:Envelope>"

'Create server-side component to make requests  
Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")    
URL = "http:<serverName:<port>/PagingService/PagingService.Paging.svc"

httpRequest.Open "GET", URL, False 
httpRequest.setRequestHeader "Content-Type", "text/xml; charset=utf-8" 
httpRequest.setRequestHeader "SOAPAction", URL & "/TestMethod"
httpRequest.Send(strSoapReq) 

Dim strResult
strResult = httpRequest.responseText
Response.Write(vbCrLf & "Result from web service call: " & vbCrLf & strResult)
此代码的http.Status结果为:415-不支持的媒体 我看到一篇文章有这个错误,他们通过更改
内容类型来纠正它。当我尝试这样做时:
httpRequest.setRequestHeader“内容类型”、“应用程序/soap+xml;字符集=utf-8”


我得到的状态值是400-请求错误。

终于找到了这个值。下面是我的代码,以防一些可怜的仆从需要这样做…也许这会减轻痛苦,缩短运行时间。 我将一个WCF web服务(文件扩展名.svc)连接到一个经典的.asp文件。 我必须向web服务添加一个basicHttpBinding。它也可以是安全的(HTTPS)。 这是添加到服务配置的绑定。文件: (注意它的名称。由
address
属性定义

<endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPaging" contract="PagingService.IPaging">
</endpoint>
您需要重新编译项目,以便WSDL在其定义中具有此绑定。 现在是有趣的部分…经典的asp:(用VBScript编写)。 肥皂信封给了我最大的麻烦:

'Namespaces
Dim NS, NS_SOAP, NS_SOAPENC, NS_XSI, NS_XSD, NS_SOAP_ACTION
'NS is the name of YOUR namespace.  If you did not define it
'in the service interface it is probably the same as this
NS = "http://tempuri.org/"
'It is for SOAP 1.1 - my version of .asp could only use SOAP 1.1
NS_SOAP = "http://schemas.xmlsoap.org/soap/envelope/"
'Next 3 definitions are standard - just copy
NS_SOAPENC = "http://schemas.xmlsoap.org/soap/encoding"
NS_XSI = "http://www.w3.org/2001/XMLSchema-instance"
NS_XSD = "http://www.w3.org/2001/XMLSchema"
'This should also be in your WSDL.  Look up the method you
'want to call; there was an attribute in mine that read 'soap_action'
NS_SOAP_ACTION = "http://tempuri.org/IFileName/<YourMethodName>"

'URL to the WCF service Using basicHttpBinding identified to the name
'you defined in the config file in 'address' attribute
 URL = "https://<serverName>:<port>/ServiceFolder/Service.Paging.svc/basic"

 'This was the hard part for me.  Defining the damn soap message
 'XML DOM objects.
 Dim Envelope, Body, Operation
 Dim ParamUserID, ParamUserName, 

 'Creates an XML DOM object.
 Set objXmlDoc = CreateObject("MSXML2.DOMDocument.6.0") 
 objXmlDoc.async = false

 'Creates the main elements.
 Set Envelope = objXmlDoc.createNode(1, "soap:Envelope", NS_SOAP)
 Envelope.setAttribute "xmlns:soapenc", NS_SOAPENC
 Envelope.setAttribute "xmlns:xsi", NS_XSI
 Envelope.setAttribute "xmlns:xsd", NS_XSD
 objXmlDoc.appendChild Envelope

 Set Body = objXmlDoc.createNode(1, "Body", NS_SOAP)
 Envelope.appendChild Body 

 'Creates an element for the SendPageForGalvanonSystem function.
 Set Operation = objXmlDoc.createNode(1, "<MethodName>", NS)
 Body.appendChild Operation 

 'Add all the parameters to the DOM
 Set ParamUserID = objXmlDoc.createNode(1, "strUserID", NS)
 ParamUserID.text = strUserID
 Operation.appendChild ParamUserID

 Set ParamUserName = objXmlDoc.createNode(1, "strUserName", NS)
 ParamUserName.text = strUserName
 Operation.appendChild ParamUserName

 Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
 httpRequest.Open "POST", URL, False         
 httpRequest.setRequestHeader "Content-Type", "text/xml"     
 httpRequest.setRequestHeader "SOAPAction", NS_SOAP_ACTION
 httpRequest.send objXmlDoc.xml

 'Releases the objects.
 Set ParamUserID = Nothing
 Set ParamUserName = Nothing
 Set Operation = Nothing
 Set Body = Nothing
 Set Envelope = Nothing      
“名称空间”
Dim NS、NS_SOAP、NS_SOAPENC、NS_XSI、NS_XSD、NS_SOAP_动作
'NS是命名空间的名称。如果未定义它
'在服务接口中,可能与此相同
NS=”http://tempuri.org/"
'这是针对SOAP 1.1的-我的.asp版本只能使用SOAP 1.1
NS_SOAP=”http://schemas.xmlsoap.org/soap/envelope/"
'接下来的3个定义是标准的-只需复制
NS_SOAPENC=”http://schemas.xmlsoap.org/soap/encoding"
NS_XSI=”http://www.w3.org/2001/XMLSchema-instance"
NS_XSD=”http://www.w3.org/2001/XMLSchema"
'这也应该在您的WSDL中。请查找您需要的方法
'想要调用;我的属性中有一个是'soap_action'
NS_SOAP_操作=”http://tempuri.org/IFileName/"
'使用名称标识的basicHttpBinding指向WCF服务的URL
'您在“地址”属性的配置文件中定义了
URL=“https://:/ServiceFolder/Service.Paging.svc/basic”
这对我来说是很难的一部分。定义该死的soap消息
'XML DOM对象。
暗信封、主体、操作
Dim ParamUserID,ParamUserName,
'创建一个XML DOM对象。
设置objXmlDoc=CreateObject(“MSXML2.DOMDocument.6.0”)
objXmlDoc.async=false
'创建主要元素。
设置Envelope=objXmlDoc.createNode(1,“soap:Envelope”,NS_soap)
Envelope.setAttribute“xmlns:soapenc”,NS_soapenc
Envelope.setAttribute“xmlns:xsi”,nsxsi
Envelope.setAttribute“xmlns:xsd”,nsxsd
objXmlDoc.appendChild信封
Set Body=objXmlDoc.createNode(1,“Body”,NS_SOAP)
信封。子体
'为SendPageForGalvanonSystem函数创建一个元素。
Set Operation=objXmlDoc.createNode(1,“,NS)
Body.appendChild操作
'将所有参数添加到DOM
Set ParamUserID=objXmlDoc.createNode(1,“strUserID”,NS)
ParamUserID.text=strUserID
Operation.appendChild参数用户ID
Set ParamUserName=objXmlDoc.createNode(1,“strUserName”,NS)
ParamUserName.text=strUserName
Operation.appendChild参数用户名
设置httpRequest=Server.CreateObject(“MSXML2.ServerXMLHTTP.6.0”)
httpRequest.Open“POST”,URL,False
httpRequest.setRequestHeader“内容类型”、“文本/xml”
httpRequest.setRequestHeader“SOAPAction”,NS\u SOAP\u操作
httpRequest.send objXmlDoc.xml
'释放对象。
设置ParamUserID=Nothing
设置ParamUserName=Nothing
设置操作=无
集合体=无
设置信封=无

如果在标题中省略charset=utf-8会发生什么情况?因此它应该是:
httpRequest.setRequestHeader“Content-Type”,“text/xml”
?因此,我更改了上面的行,没有任何更改。此外,我尝试在
Open
语句中使用'GET'和'POST'。'GET'返回wsdl或描述web服务的网站。'POST'不返回任何内容。两者都没有错误。SOAP请求中的命名空间是否假定为URN?如何确定URL的URN你的web服务?我检查了我请求的http状态,得到了错误:415,这是一种“不受支持的媒体类型”。
<basicHttpBinding> 
    <binding name="BasicHttpBinding_IPaging"></binding>
</basicHttpBinding>
'Namespaces
Dim NS, NS_SOAP, NS_SOAPENC, NS_XSI, NS_XSD, NS_SOAP_ACTION
'NS is the name of YOUR namespace.  If you did not define it
'in the service interface it is probably the same as this
NS = "http://tempuri.org/"
'It is for SOAP 1.1 - my version of .asp could only use SOAP 1.1
NS_SOAP = "http://schemas.xmlsoap.org/soap/envelope/"
'Next 3 definitions are standard - just copy
NS_SOAPENC = "http://schemas.xmlsoap.org/soap/encoding"
NS_XSI = "http://www.w3.org/2001/XMLSchema-instance"
NS_XSD = "http://www.w3.org/2001/XMLSchema"
'This should also be in your WSDL.  Look up the method you
'want to call; there was an attribute in mine that read 'soap_action'
NS_SOAP_ACTION = "http://tempuri.org/IFileName/<YourMethodName>"

'URL to the WCF service Using basicHttpBinding identified to the name
'you defined in the config file in 'address' attribute
 URL = "https://<serverName>:<port>/ServiceFolder/Service.Paging.svc/basic"

 'This was the hard part for me.  Defining the damn soap message
 'XML DOM objects.
 Dim Envelope, Body, Operation
 Dim ParamUserID, ParamUserName, 

 'Creates an XML DOM object.
 Set objXmlDoc = CreateObject("MSXML2.DOMDocument.6.0") 
 objXmlDoc.async = false

 'Creates the main elements.
 Set Envelope = objXmlDoc.createNode(1, "soap:Envelope", NS_SOAP)
 Envelope.setAttribute "xmlns:soapenc", NS_SOAPENC
 Envelope.setAttribute "xmlns:xsi", NS_XSI
 Envelope.setAttribute "xmlns:xsd", NS_XSD
 objXmlDoc.appendChild Envelope

 Set Body = objXmlDoc.createNode(1, "Body", NS_SOAP)
 Envelope.appendChild Body 

 'Creates an element for the SendPageForGalvanonSystem function.
 Set Operation = objXmlDoc.createNode(1, "<MethodName>", NS)
 Body.appendChild Operation 

 'Add all the parameters to the DOM
 Set ParamUserID = objXmlDoc.createNode(1, "strUserID", NS)
 ParamUserID.text = strUserID
 Operation.appendChild ParamUserID

 Set ParamUserName = objXmlDoc.createNode(1, "strUserName", NS)
 ParamUserName.text = strUserName
 Operation.appendChild ParamUserName

 Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
 httpRequest.Open "POST", URL, False         
 httpRequest.setRequestHeader "Content-Type", "text/xml"     
 httpRequest.setRequestHeader "SOAPAction", NS_SOAP_ACTION
 httpRequest.send objXmlDoc.xml

 'Releases the objects.
 Set ParamUserID = Nothing
 Set ParamUserName = Nothing
 Set Operation = Nothing
 Set Body = Nothing
 Set Envelope = Nothing