android - 在 android 中使用 WCF 服务

标签 android wcf

我已经创建了这个服务(使用 WCF、Azure):

[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class Service1 : IPMPService
{
    public int Dummy()
    {
        return 0;
    }
}

IPMPService 在哪里:

[ServiceContract]
public interface IPMPService
{
    [WebGet()]
    [OperationContract]
    int Dummy();
}

并尝试在我的 android 应用程序中使用它:
第一次尝试:

String METHOD_NAME = "DummyRequest";
String NAMESPACE = "http://tempuri.org/";
String URL = "http://tyty.cloudapp.net/Service1.svc";
String SOAP_ACTION = "http://tyty.cloudapp.net/Service1.svc/Dummy";

String res = "";
try {
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.call(SOAP_ACTION, envelope);
    SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
    // to get the data
    String resultData = result.toString();
    res = resultData;
    // 0 is the first object of data
    } catch (Exception e) {
        res = e.getMessage();
    }   

result: SoapFault - faultcode: 'a:ActionNotSupported' faultstring: 'The message with Action 'http://tyty.cloudapp.net/Service1.svc/Dummy' cannot be processed at the receiver, 由于 ContractFilter 不匹配在 EndpointDispatcher。这可能是因为契约(Contract)不匹配(发送方和接收方之间的操作不匹配)或发送方和接收方之间的绑定(bind)/安全不匹配。检查发送方和接收方是否具有相同的契约(Contract)和相同的绑定(bind)(包括安全要求,例如消息、传输、无)。 faultactor:'null' 详细信息:null

第二次尝试(取自 here ):

String SERVER_HOST = "http://tyty.cloudapp.net";
int SERVER_PORT = 8080;
String URL1 = "/Service1.svc/Dummy";
String keywords = null;
HttpEntity entity = null;
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(URL1);
try {
HttpResponse response = client.execute(target, get);
entity = response.getEntity();
keywords = EntityUtils.toString(entity);
} catch (Exception e) {
e.printStackTrace();
   } finally {
if (entity != null)
        try {
        entity.consumeContent();
    } catch (IOException e) {
  }
}  

结果:java.net.UnknownHostException:无法解析主机“http://tyty.cloudapp.net”:没有与主机名关联的地址

我还在 C# 中创建了一个(工作中的)客户端,这是它的 App.config:

<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IPMPService" closeTimeout="00:01:00"
            openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
            allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
            maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
            messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
            useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
                realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://tyty.cloudapp.net/Service1.svc"
          binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPMPService"
          contract="IPMPService" name="BasicHttpBinding_IPMPService" />
    </client>
  </system.serviceModel>
</configuration>

如前所述,Android 中的两次尝试都失败了。任何人都可以告诉我我做错了什么,以及如何做对吗?
谢谢。

最佳答案

您的第一次尝试非常接近。只需要两个修复:

String METHOD_NAME = "Dummy";
String NAMESPACE = "http://tempuri.org/";
String URL = "http://tyty.cloudapp.net/Service1.svc";
String SOAP_ACTION = "http://tempuri.org/IPMPService/Dummy";

String res = "";
try {
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.call(SOAP_ACTION, envelope);
    SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
    // to get the data
    String resultData = result.toString();
    res = resultData;
    // 0 is the first object of data
} catch (Exception e) {
    res = e.getMessage();
}   

第一个是方法名称:它只是Dummy(而不是DummyRequest)。第二个是 SOAP 操作:它是 http://tempuri.org/IPMPService/Dummy

两者都可以从 WSDL 派生。您的操作定义为:

<wsdl:operation name="Dummy">
    <wsdl:input wsaw:Action="http://tempuri.org/IPMPService/Dummy" message="tns:IPMPService_Dummy_InputMessage"/>
    <wsdl:output wsaw:Action="http://tempuri.org/IPMPService/DummyResponse" message="tns:IPMPService_Dummy_OutputMessage"/>
</wsdl:operation>

如果您随后查看 IPMPService_Dummy_InputMessage 消息类型,该元素称为 Dummy:

<wsdl:message name="IPMPService_Dummy_InputMessage">
    <wsdl:part name="parameters" element="tns:Dummy"/>
</wsdl:message>

SOAP 操作位于此处:

<wsdl:operation name="Dummy">
    <soap:operation soapAction="http://tempuri.org/IPMPService/Dummy" style="document"/>
    ...

关于android - 在 android 中使用 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10748653/

相关文章:

Android 应用程序在亚马逊 fireTV 中没有响应

android - 在Flutter中根据当前位置的经纬度获取完整的地址详细信息

当重试策略设置为 0 时,Android Volley 向服务器发出 2 个请求

asp.net - ADFS 2.0 身份验证的 Web 服务调用

java - 使用 MixIn 时 Jackson android NPE

android - 带搜索图标的键盘

.net-3.5 - 客户端连接到 WCF 托管端点时触发事件

wcf - 每个 WCF 请求使用一个 LINQ to SQL DataContext 并自动调用 SubmitChanges?

wcf - sharepoint 中的 RESTful WCF

c# - WCF 服务引用不提供非异步方法