ios - 在 Objective C(iOS 应用程序)中请求 WCF Web 服务时收到 SOAP 错误

标签 ios wcf soap wsdl nsurl

我正在尝试从 iOS 应用调用网络服务。我已经在 WCF 中开发了该服务。 Web 服务托管在配置了 BasicHTTPBinding 的 IIS 上。我开发了一个简单的 iOS 应用程序,它调用远程 PC 上托管的 Web 服务。

iOS 应用程序运行良好,但 SOAP 服务似乎无法理解我从 iOS 应用程序发送的请求。这是所有详细信息,我得到的错误在最后。

Web服务的WSDL是:

    <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:tns="http://tempuri.org/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" name="SLService" targetNamespace="http://tempuri.org/">
<wsdl:types>
<xsd:schema targetNamespace="http://tempuri.org/Imports">
<xsd:import schemaLocation="http://10.211.55.4/TestService/SLService.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
<xsd:import schemaLocation="http://10.211.55.4/TestService/SLService.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="ISLService_DoWork_InputMessage">
<wsdl:part name="parameters" element="tns:DoWork"/>
</wsdl:message>
<wsdl:message name="ISLService_DoWork_OutputMessage">
<wsdl:part name="parameters" element="tns:DoWorkResponse"/>
</wsdl:message>
<wsdl:portType name="ISLService">
<wsdl:operation name="DoWork">
<wsdl:input wsaw:Action="http://tempuri.org/ISLService/DoWork" message="tns:ISLService_DoWork_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/ISLService/DoWorkResponse" message="tns:ISLService_DoWork_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BasicHttpBinding_ISLService" type="tns:ISLService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="DoWork">
<soap:operation soapAction="http://tempuri.org/ISLService/DoWork" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="SLService">
<wsdl:port name="BasicHttpBinding_ISLService" binding="tns:BasicHttpBinding_ISLService">
<soap:address location="http://10.211.55.4/TestService/SLService.svc"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

这是我在 iOS 中发出 SOAP 请求的方式,问题很可能出在这里:

NSString *soapMessage = [NSString stringWithFormat:@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><DoWork></DoWork></SOAP-ENV:Body></SOAP-ENV:Envelope>"];

NSURL *url=[NSURL URLWithString:@"http://10.211.55.4/TestService/SLService.svc/"];

NSMutableURLRequest *theRequest= [NSMutableURLRequest requestWithURL:url];

NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"urn:SLService/DoWork" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];    
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if (theConnection) {

    responseData = [[NSMutableData data] retain];
} else {

    messageTextView.text=@"Failed";

}

这是 WCF Web 服务类中的内容:

namespace TestSilverlight.Web{

public class SLService : ISLService
{
    public string DoWork()
    {
        return "Service Works!";
    }
}}

这是 WCF 网络服务中 web.config 文件的内容:

    <?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>

    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
</configuration>

这是我在 responseData 对象中得到的 SOAP 响应(错误):

a:ActionNotSupportedThe message with Action 'urn:ISLService/DoWork' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).

我很懒得在这里问问题,希望你们能理解这一点。请帮忙。

最佳答案

问题出在您的 SOAPAction header 上。我怀疑问题与此相同 calling wcf webservice using basichttpbinding without REST or JSON .

解决方案是将您的 SOAPAction 更改为

urn:SLService/DoWork

http://tempuri.org/SLService/DoWork

甚至可能

http://tempuri.org/ISLService/DoWork

关于ios - 在 Objective C(iOS 应用程序)中请求 WCF Web 服务时收到 SOAP 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8230530/

相关文章:

WCF 端口配置错误

c# - WCF:base.Channel 调用中不存在指定的注册表项

c# - Web 服务 - 方法命名?

ios - 找不到 Apple 推送通知服务的证书签名请求 (CSR)

iOS清除HTML5离线缓存数据

ios Horizo​​ntal UISlider 导致 UIPageViewController 在不希望的情况下滑动页面

ios - 应用商店提交错误 Invalid Bundle Structure

c# - WCF 点对点,那里有节点吗?

xml - 如何使用 Axis2 和 Rampart 在 SOAP 请求中添加 MessageID

java - 使用 Java 创建 WADL 和 WSDL?