How to consume WCF .SVC in Android

后端 未结 2 1715
庸人自扰
庸人自扰 2021-02-11 01:46

i red a lot of documents about, but i can\'t consume this .svc file. I\'ve no problem with .ASMX file. Only SVC i\'m not able to consume. It\'s very very very stressfull....! I

2条回答
  •  盖世英雄少女心
    2021-02-11 01:53

    I solved by myself (WCF binding has to be basicHttpBinding, otherwise it doesn't work):

    private static final String NAMESPACE = "http://tempuri.org/";
    private static String URL="your url";
    
    private static final String SOAP_ACTION_VALIDATION = "IValidateUser_wcf/ValidateUser";
    private static final String VALIDATION_METHOD = "ValidateUser";
    
    public boolean validateUser_WCF(String username, String password){
    
        SoapSerializationEnvelope envelope = null;
        SoapObject request = null;
        HttpTransportSE httpTransportSE = null;
    
        try {
            request = new SoapObject(NAMESPACE, VALIDATION_METHOD);
            request.addProperty("username", username);
            request.addProperty("password", password);
    
            envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true; 
            envelope.setOutputSoapObject(request);
    
            //////////////////////////////                               
            // here you can add a HEADER element if you want
            Element[] header = new Element[1];  
    
            header[0] = new Element().createElement(NAMESPACE_INFOCAD, "a1");                
            header[0].addChild(Node.TEXT, "HeaderTextContent");
    
            envelope.headerOut = header;
            //////////////////////////////                               
    
            httpTransportSE = new HttpTransportSE(URL+VALIDATION_URI, 10*10000); // second parameter is timeout
            httpTransportSE.debug = true;
            httpTransportSE.setXmlVersionTag("");
            httpTransportSE.call(NAMESPACE+SOAP_ACTION_VALIDATION, envelope);
    
            // if response is a simple text result, you can call SoapPrimitive, if not, you have to call SoapObject result and navigate in response's tree like an xml file
            SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
    
            //To get the data.
            String textResult = result.toString();
            Log.i("textResult", textResult); 
    
            return true;
    
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
                    // here you can see in LOG what is request you send and what is response received
                    Log.i(getClass().getSimpleName(),"requestDump : "+httpTransportSE.requestDump);
                    Log.i(getClass().getSimpleName(),"responseDump : "+httpTransportSE.responseDump);
        }
    
        return false;
    }
    

提交回复
热议问题