【问题标题】:Consuming WCF Soap Service running on Mono from Android KSoap2从 Android KSoap2 使用在 Mono 上运行的 WCF Soap 服务
【发布时间】:2013-07-23 19:52:18
【问题描述】:

我正在开发一个 Android/C# 项目。我需要做的是拥有一个可以在 Windows 或 Linux (Mono) 上运行的 WCF 肥皂服务。

它在 Windows 上运行良好,我可以从 Visual Studio 中提供的 WCF 测试客户端访问 Mono 上的 WCF 服务,它运行良好,但是使用 KSOAP2 访问 android 时出现错误 HTTP Request Failed, HTTP status: 415

下面是soap服务的启动方式

string methodInfo = classDetails + MethodInfo.GetCurrentMethod().Name;
            try
            {
                if (Environment.GetEnvironmentVariable("MONO_STRICT_MS_COMPLIANT") != "yes")
                {
                    Environment.SetEnvironmentVariable("MONO_STRICT_MS_COMPLIANT", "yes");
                }
                if (String.IsNullOrEmpty(soapServerUrl))
                {
                    string message = "Not starting Soap Server: URL or Port number is not set in config file";
                    library.logging(methodInfo, message);
                    library.setAlarm(message, CommonTasks.AlarmStatus.Medium, methodInfo);
                    return;
                }
                Console.WriteLine("Soap Server URL: {0}", soapServerUrl);
                baseAddress = new Uri(soapServerUrl);
                host = new ServiceHost(soapHandlerType, baseAddress);
                BasicHttpBinding basicHttpBinding = new BasicHttpBinding();

                //basicHttpBinding.Namespace = "http://tempuri.org/";



                var meta = new ServiceMetadataBehavior()
                {
                    HttpGetEnabled = true,
                    HttpGetUrl = new Uri("", UriKind.Relative),
                    //HttpGetBinding = basicHttpBinding,
                };
                //meta.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;

                host.Description.Behaviors.Add(meta);

                host.AddServiceEndpoint(soapManagerInterface, basicHttpBinding, soapServerUrl);
                host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");

                var debugBehaviour = new ServiceDebugBehavior()
                {
                    HttpHelpPageEnabled = true,
                    HttpHelpPageUrl = new Uri("", UriKind.Relative),
                    IncludeExceptionDetailInFaults = true,
                    //HttpHelpPageBinding = basicHttpBinding,
                };

                host.Description.Behaviors.Remove(typeof(ServiceDebugBehavior));
                host.Description.Behaviors.Add(debugBehaviour);
                host.Opened += new EventHandler(host_Opened);
                host.Faulted += new EventHandler(host_Faulted);
                host.Closed += new EventHandler(host_Closed);
                host.UnknownMessageReceived += new EventHandler<UnknownMessageReceivedEventArgs>(host_UnknownMessageReceived);
                host.Open();
            }

soapServerURL 是http://192.168.1.74:8000/CritiMon。

以下是我如何尝试使用 KSOAP2 从 android 调用肥皂服务。

final String soapAction = "http://tempuri.org/ISoapInterface/testSoapFunction";
        final String namespace = "http://tempuri.org/";
        final String methodName = "testSoapFunction";
        final String url = "http://192.168.1.74:8000/CritiMon?wsdl";
        String resultData = "";

        new Thread(new Runnable() {

            @Override
            public void run() {
                SoapSerializationEnvelope envelope = null;
                try
                {
                //String resultData = "";

                    SoapObject request = new SoapObject(namespace, methodName);
                    //request.addProperty("firstName", "Chris");
                    //request.addProperty("lastName", "Board");
                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                    envelope.dotNet = true;
                    envelope.setOutputSoapObject(request);

                    HttpTransportSE http = new HttpTransportSE(url);

                    http.call(soapAction, envelope);
                    SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

                    String resultData = result.toString();
                    Log.d("Soap Result", resultData);

                }
                catch (Exception ex)
                {
                    Log.e("Soap Error 2", ex.getMessage());
}

我不知道我能做些什么才能在带有 Android 的 Mono 上实现这一点。

【问题讨论】:

    标签: c# android wcf soap ksoap2


    【解决方案1】:

    首先,您要在线捕获实际的 SOAP 请求。您可以使用 Fiddler 或 SoapUI 执行此操作 - 它们都充当传递本地请求的代理,允许您检查实际的 XML 请求是否存在异常。通过这样做,您可能会发现一些显而易见的事情,或者您至少可以用更多信息更新您的问题。

    因此,在没有任何进一步信息的情况下,我只能提供一个难忘的体验,即从非 .NET 应用程序与 WCF 服务交谈:

    WCF 指定它期望的 XML 请求,但它实际上要求对象属性按特定顺序排列。这可以是数据合同中声明的顺序,也可以是隐含的字母顺序。无论哪种方式,如果您不按指定的顺序提供对象属性,您将被刺痛并且事情将无法正常工作。

    【讨论】:

    • 感谢 SoapUI 确实帮助调试了这个问题。我发现 Android 发送的内容类型稍有错误,所以我设法找出如何将内容类型覆盖为肥皂服务所期望的内容,现在它可以工作了。感谢您的帮助
    猜你喜欢
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    相关资源
    最近更新 更多