2013-07-15 167 views
3

我在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> 
+0

如果你從瀏覽器調用ws,它工作嗎? –

+0

是的,我可以成功地從瀏覽器訪問和使用VS WCF測試客戶端它將成功返回結果 – Boardy

+0

如果你在你的WS做一個簡單的行動,它是否工作? –

回答

1

我發現這個問題終於。

這是一個Web服務的問題,我認爲這是我的Android實現問題。在soap接口中有一個類型,所以android調用沒有找到函數。

感謝您的幫助。

0

嘗試WSDL添加以下

soapEnvelope.dotNet = true; 

如果它不工作,你可以發表y我們的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(); 
} 

肥皂所需物品:

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

感謝您的幫助,我已經嘗試過,但不幸的是得到相同的錯誤,我修改了這個問題,包括我試過的wsdl – Boardy

+0

,它爲我工作。 – naveejr