I am working on android project and I am trying to implement KSoap library.
I have created a C# Console application that is hosting a C# WCF soap service and I am trying to get android to talk to the soap service.
Below is my C# WCF Service.
class SoapServer : ISoapInterface
{
public string testSoapFunction()
{
return "Hello World";
}
}
Below is the Soap Interface
[ServiceContract]
public interface ISoapInterface
{
[OperationContract]
string testSoapFunction();
}
Below is how the soap service is started
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;
}
baseAddress = new Uri(soapServerUrl);
host = new ServiceHost(soapHandlerType, baseAddress);
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
var meta = new ServiceMetadataBehavior()
{
HttpGetEnabled = true,
HttpGetUrl = new Uri("", UriKind.Relative),
};
host.Description.Behaviors.Add(meta);
var debugBehaviour = new ServiceDebugBehavior()
{
HttpHelpPageEnabled = true,
HttpHelpPageUrl = new Uri("", UriKind.Relative),
IncludeExceptionDetailInFaults = true
};
ServiceEndpoint endpnt = host.AddServiceEndpoint(
soapManagerInterface,
basicHttpBinding,
"EmailServer",
new Uri("", UriKind.Relative));
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();
Below is how I am calling the service via android.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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.69:8000/CritiMon";
String resultData = "";
new Thread(new Runnable() {
@Override
public void run() {
String resultData = "";
SoapSerializationEnvelope soapEnv = new SoapSerializationEnvelope(SoapEnvelope.VER11);
SoapObject request = new SoapObject(namespace, methodName);
soapEnv.setOutputSoapObject(request);
HttpTransportSE http = new HttpTransportSE(url);
http.debug = true;
try
{
http.call(soapAction, soapEnv);
SoapObject result = (SoapObject)soapEnv.bodyOut;
if (result != null)
{
resultData = result.getProperty(0).toString();
Log.d("Soap Result", resultData);
}
}
catch (Exception ex)
{
Log.e("Soap", ex.toString());
}
}
}).start();
When I run the above code I am getting the following response:
java.io.IOException: HTTP request failed. HTTP Status: 500.
Within the soap server I also fire the unknown message received event handler which contains
<v:Envelope xmlns:i="http://www.w3.org/2001/
XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://s
chemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/enve
lope/">
<v:Header>
<To v:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addr
essing/none">http://192.168.1.69:8000/CritiMon</To>
<Action v:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/
addressing/none">http://tempuri.org/ISoapInterface/testSoapFunction</Action>
</v:Header>
<v:Body>
Below is the WSDL from http://192.168.1.69:8000/CritiMon?wsdl
<wsdl:definitions name="SoapServer" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:tns="http://tempuri.org/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata">
<wsdl:types>
<xsd:schema targetNamespace="http://tempuri.org/Imports">
<xsd:import schemaLocation="http://192.168.1.69:8000/CritiMon?xsd=xsd0" namespace="http://tempuri.org/"/>
<xsd:import schemaLocation="http://192.168.1.69:8000/CritiMon?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="ISoapInterface_testSoapFunction_InputMessage">
<wsdl:part name="parameters" element="tns:testSoapFunction"/>
</wsdl:message>
<wsdl:message name="ISoapInterface_testSoapFunction_OutputMessage">
<wsdl:part name="parameters" element="tns:testSoapFunctionResponse"/>
</wsdl:message>
<wsdl:portType name="ISoapInterface">
<wsdl:operation name="testSoapFunction">
<wsdl:input wsaw:Action="http://tempuri.org/ISoapInterface/testSoapFunction" message="tns:ISoapInterface_testSoapFunction_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/ISoapInterface/testSoapFunctionResponse" message="tns:ISoapInterface_testSoapFunction_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BasicHttpBinding_ISoapInterface" type="tns:ISoapInterface">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="testSoapFunction">
<soap:operation soapAction="http://tempuri.org/ISoapInterface/testSoapFunction" 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="SoapServer">
<wsdl:port name="BasicHttpBinding_ISoapInterface" binding="tns:BasicHttpBinding_ISoapInterface">
<soap:address location="http://192.168.1.69:8000/CritiMon/EmailServer"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
I found the issue finally.
It was an issue with the web service, I was assuming it was an issue with my implementation with Android. There was a type in the soap interface so the android call wasn't finding the function.
Thanks for the help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With