我正在研究android项目,我正在尝试实现KSoap库。
我创建了一个托管C#WCF soap服务的C#Console应用程序,我试图让android与soap服务交谈。
以下是我的C#WCF服务。
class SoapServer : ISoapInterface
{
public string testSoapFunction()
{
return "Hello World";
}
}
下面是肥皂界面
[ServiceContract]
public interface ISoapInterface
{
[OperationContract]
string testSoapFunction();
}
以下是肥皂服务的启动方式
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。
在soap服务器中,我还会触发包含
的未知消息接收事件处理程序<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>
以下是来自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>
答案 0 :(得分:1)
我终于找到了问题。
这是Web服务的一个问题,我认为这是我使用Android实现的一个问题。 soap接口中有一个类型,因此android调用没有找到该函数。
感谢您的帮助。
答案 1 :(得分:0)
尝试添加以下内容
soapEnvelope.dotNet = true;
如果它不起作用,你可以发布你的WSDL文件吗?
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
[OperationContract]
string testSoapFunction();
}
final String NAMESPACE = "http://Microsoft.ServiceModel.Samples";
final String SOAP_ACTION = "http://Microsoft.ServiceModel.Samples/ICalculator/testSoapFunction";
final String METHOD_NAME = "testSoapFunction";
httpTransport = new HttpTransportSE(url, 2000);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
httpTransport.debug = true;
//soapEnvelope.implicitTypes = true;
soapEnvelope.dotNet = true;
SoapObject soapReq = new SoapObject(NAMESPACE, METHOD_NAME);
soapEnvelope.setOutputSoapObject(soapReq);
try {
httpTransport.call(SOAP_ACTION, soapEnvelope);
Log.d("REQ" , httpTransport.requestDump);
Log.d("RES" , httpTransport.responseDump);
Object retObj = soapEnvelope.bodyIn;
Log.d("TEST" , retObj.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Soap Req:
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header />
<v:Body>
<testSoapFunction xmlns="http://Microsoft.ServiceModel.Samples" id="o0" c:root="1" />
</v:Body>
</v:Envelope>