简体   繁体   English

在ASP.NET中使用SOAP Web服务

[英]Consuming SOAP web service in ASP.NET

I have a SOAP WSDL (found here: https://portal.bsh-partner.com/picenter/server/a2a/ ) and I am trying to consume the web services. 我有一个SOAP WSDL(在这里找到: https//portal.bsh-partner.com/picenter/server/a2a/ ),我正在尝试使用Web服务。

var soapEnvelope = string.Empty;
soapEnvelope = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
soapEnvelope += "<soapenv:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:q0=\"http://thexmlhost.com\">";
soapEnvelope += "<q0:Connect>";
soapEnvelope += "<q0:Username>username</q0:Username>";
soapEnvelope += "<q0:Password>password</q0:Password>";
soapEnvelope += "</q0:Connect>";
soapEnvelope += "</soapenv:Body>";
soapEnvelope += "</soapenv:Envelope>";

var xmlHttp = new MSXML2.ServerXMLHTTP40();
xmlHttp.open("POST", "https://thexmlhost.com/", "", "");
xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttp.setRequestHeader("SOAPAction", "thexmlhost.com/");

xmlHttp.send(soapEnvelope);

xmlHttp.waitForResponse(500);

var outXml = xmlHttp.responseText;

The response keeps returning a general HTTP response error page. 响应不断返回一般HTTP响应错误页面。 Any idea how I should be passing my Soap Envelope to get the proper response back from the web service? 知道如何通过我的Soap Envelope从Web服务获得正确的响应吗?

If you have WSDL, you can create a Service Reference in Visual Studio and it will generate a proxy class for you. 如果您有WSDL,则可以在Visual Studio中创建服务引用,它将为您生成代理类。 Unless I am missing some fine point in your question, it would be a lot more reliable, and much easier, to implement and use. 除非我在你的问题中遗漏了一些细节,否则实施和使用会更加可靠,更容易。

Hope that helps. 希望有所帮助。

I just noticed that you are using this code on the server side. 我刚刚注意到你在服务器端使用这段代码。


This is not the way to consume web services in .NET. 这不是在.NET中使用Web服务的方法。 Use the "Add Service Reference" command in Visual Studio to create proxy classes that will allow you to very easily consume the service. 使用Visual Studio中的“添加服务引用”命令创建代理类,以便您可以非常轻松地使用该服务。

See How to Consume a Web Service for an example, as well as http://msdn.microsoft.com/wcf/ . 有关示例,请参见如何使用Web服务 ,以及http://msdn.microsoft.com/wcf/

Also, never use string manipulation techniques when working with XML. 此外,在使用XML时永远不要使用字符串操作技术。 Briefly, the rules for strings and for XML are different. 简而言之,字符串和XML的规则是不同的。 .NET has several XML APIs to work with XML, and they all know the rules. .NET有几个XML API可以使用XML,它们都知道规则。

Just glancing at your code, it looks like you forgot to add your body start tag: <soapenv:Body> 只是看了看你的代码,看起来你忘了添加你的身体开始标记: <soapenv:Body>

Unless you have a particular reason to be constructing your message manually, I would highly recommend using a proxy generated using the svcutil.exe tool that comes with visual studio 2008 or later. 除非您有特殊的理由手动构建消息,否则我强烈建议您使用Visual Studio 2008或更高版本附带的svcutil.exe工具生成的代理。 (this tool is used automatically by Visual Studio if you use the "Add Service Reference" feature), assuming that you don't have a problem with using WCF to handle communication with your web service. (如果使用“添加服务引用”功能,Visual Studio会自动使用此工具),假设使用WCF处理与Web服务的通信时没有问题。

Alternatively, if for some reason you cannot use WCF, such as if you happen to be developing in .Net 2.0 with Visual Studio 2005 in which case you wouldn't have the "Add Service Reference" feature available, then could still use Visual Studio's older "Add Web Reference" feature to generate a proxy for your web service. 或者,如果由于某种原因您无法使用WCF,例如,如果您正在使用Visual Studio 2005在.Net 2.0中进行开发,在这种情况下您将无法使用“添加服务引用”功能,那么仍然可以使用Visual Studio较旧的“添加Web引用”功能,以便为您的Web服务生成代理。 This feature uses the wsdl.exe tool rather than svcutil.exe. 此功能使用wsdl.exe工具而不是svcutil.exe。

Otherwise, if you really must continue with this approach of constructing the SOAP message manually, then I'd at least recommend adding some logging to your application so that you can more easily analyze the constructed message when something goes wrong. 否则,如果你真的必须继续这种手动构建SOAP消息的方法,那么我至少建议在你的应用程序中添加一些日志记录,以便在出现问题时更容易分析构造的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ASP.NET Web API应用程序中使用.asmx服务 - Consuming .asmx service in asp.net web api application ASP.NET帮助(使用公共Web服务) - ASP.NET help (consuming a public Web Service) ASP.NET MVC-反序列化来自SOAP Web服务的响应 - ASP.NET MVC - deserializing response from SOAP web service 在ASP.Net中创建一个紧凑的非SOAP Web服务 - Create a compact, non-SOAP web service in ASP.Net 从 Asp.net 应用程序使用 WCF SOAP 服务时出错(没有端点侦听) - Error (There was no endpoint listening at) while consuming the WCF SOAP Service from Asp.net Application 如何在ASP.NET Web应用程序中使用Jquery Ajax使用Web服务 - how to consuming web service using jquery ajax in asp.net web application ASP.NET消耗Web服务并读取Soap消息 - ASP.NET Consuming Webservices and reading the Soap Message 在Java中使用需要自定义SOAP标头的.NET Web服务 - Consuming in Java a .NET Web Service that requires a custom SOAP Header 在.NET中使用RPC /编码的SOAP Web服务时出错 - Error Consuming a RPC/Encoded SOAP web service in .NET 单击asp.net mvc中的按钮后,使用Web服务并通过给定的xml数据自动填充文本框 - Consuming a web service and auto populate textbox by the given xml data after clicking a button in asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM