我正在开发 android 项目,我正在尝试实现 KSoap 库。
我创建了一个托管 C# WCF 肥皂服务的 C# 控制台应用程序,我正试图让 android 与肥皂服务对话。
下面是我的 C# WCF 服务。
class SoapServer : ISoapInterface
{
public string testSoapFunction()
{
return "Hello World";
}
}
下面是肥皂界面
[ServiceContract]
public interface ISoapInterface
{
[OperationContract]
string testSoapFunction();
}
下面是soap服务是如何启动的
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();
下面是我如何通过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();
当我运行上面的代码时,我得到以下响应:
java.io.IOException:HTTP 请求失败。HTTP 状态:500。
在肥皂服务器中,我还触发了未知消息接收事件处理程序,其中包含
<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>
以下是来自的 WSDLhttp://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>