我在android項目上工作,我正在嘗試實現KSoap庫。KSoap HTTP 500錯誤
我創建了一個託管C#WCF soap服務的C#控制檯應用程序,我試圖讓android與soap服務進行通信。
以下是我的C#WCF服務。
class SoapServer : ISoapInterface
{
public string testSoapFunction()
{
return "Hello World";
}
}
下面是SOAP接口
[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();
下面是我如何打電話通過機器人的服務。
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>
如果你從瀏覽器調用ws,它工作嗎? –
是的,我可以成功地從瀏覽器訪問和使用VS WCF測試客戶端它將成功返回結果 – Boardy
如果你在你的WS做一個簡單的行動,它是否工作? –