使用 SOAP Web 服务与 GET 而不是 POST 一起使用



我需要通过HTTP调用外部SOAP Web服务。
我有WSDL文件,并通过"添加服务引用"将其添加到Visual Studio中。Visual studio随后添加了许多文件,在参考文件中我可以找到这个:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="Service.IService")]
public interface IService {
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService/Function", ReplyAction="http://tempuri.org/IService/FunctionResponse")]
namespace.Service.ExecuteFunctionResponse ExecuteFunction(namespace.Service.FunctionRequest request);
}

此外,此调用的异步版本以及用于发送接收的对象等。

为了调用该服务,我添加了以下代码:

BasicHttpBinding binding = new BasicHttpBinding();     
EndpointAddress endpointAddress = new EndpointAddress("the address");
serviceChannel = new ServiceClient(binding, endpointAddress).ChannelFactory.CreateChannel();
Response response = serviceChannel.ExecuteFunction(new Request(...));

这导致我收到异常,错误 405 方法不允许。
因此,我似乎必须使用HTTP GET请求而不是默认的POST请求。但是我找不到这种工作方式可以改变的地方。

那么,在哪里可以设置此对 Web 服务的调用的 HTTP 方法?

SOAP 服务使用 HTTP POST,因为它们交换 XML 消息(往往很复杂),并且无法在查询字符串中传输。

您确定必须使用 HTTP GET 吗?也许您收到的错误"405方法不允许"是由某些错误的配置引起的。 我会仔细检查 SOAP 端点 URL 是否设置正确,并检查是否需要额外的安全要求。

编辑过去,有一种做法是创建 ASP.NET Web 服务,这些服务也可以接受 GET。但他们不会期望XML消息。相反,您必须在查询字符串中传递所有参数。例如:https://foo.bar/service.asmx/Func?param1=X&param2=Y(其中 param1 和 param2 是预期参数)。

这样就可以调用 Web 服务,而无需使用 WSDL 并使用 GET 方法。例如,您可以通过使用HttpClient来实现它。 这种方法的缺点是您必须处理纯数据而不是对象。

我希望它可能会有所帮助。

最新更新