Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consuming WCF service in android

Tags:

android

wcf

I've created this service (using WCF, Azure):

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

Where IPMPService is:

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

And attempted to consume it in my android app:
1st attempt:

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, 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).' faultactor: 'null' detail: null

2nd attempt (taken from 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) {
  }
}  

Result: java.net.UnknownHostException: Unable to resolve host "http://tyty.cloudapp.net": No address associated with hostname

I've also created a (working) client in c#, here is its 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>

As mentioned, both attempts in Android failed. Can anyone please tell me what I'm doing wrong, and how to do it right?
Thanks.

like image 936
Oren A Avatar asked Feb 21 '23 08:02

Oren A


1 Answers

Your first attempt is pretty close. There are just two fixes needed:

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();
}   

The first one is the method name: it just Dummy (instead of DummyRequest). The second is the SOAP action: it's http://tempuri.org/IPMPService/Dummy.

Both can be derived from the WSDL. Your operation is defined as:

<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>

If you then look at the IPMPService_Dummy_InputMessage message type, the element is called Dummy:

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

And the SOAP action is found here:

<wsdl:operation name="Dummy">
    <soap:operation soapAction="http://tempuri.org/IPMPService/Dummy" style="document"/>
    ...
like image 172
Codo Avatar answered Mar 05 '23 18:03

Codo