如何在Android中使用WCF.SVC

如何在Android中使用WCF.SVC,android,.net,wcf,web-services,ksoap2,Android,.net,Wcf,Web Services,Ksoap2,我查阅了很多文档,但是我不能使用这个.svc文件。 我对.ASMX文件没有问题。只有SVC我不能消费。压力很大。。。。!我无法修改WCF部件。 我发布WSDL文件并尝试使用它: WSDL <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsu="http://docs.oasis-open.

我查阅了很多文档,但是我不能使用这个.svc文件。 我对.ASMX文件没有问题。只有SVC我不能消费。压力很大。。。。!我无法修改WCF部件。 我发布WSDL文件并尝试使用它:

WSDL

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
 xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" 
 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
 xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" 
 xmlns:tns="http://tempuri.org/" 
 xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" 
 xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" 
 xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" 
 xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" 
 xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" 
 xmlns:wsa10="http://www.w3.org/2005/08/addressing" 
 xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" name="ValidateUser_wcf" 
 targetNamespace="http://tempuri.org/">
<wsp:Policy wsu:Id="WSHttpBinding_IValidateUser_wcf_policy">
<wsp:ExactlyOne>
<wsp:All>
<wsaw:UsingAddressing/>
</wsp:All>
</wsp:ExactlyOne>
</wsp:Policy>
<wsdl:types>
<xsd:schema targetNamespace="http://tempuri.org/Imports">
<xsd:import schemaLocation="http://URL/WebServices/validateuser.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
<xsd:import schemaLocation="http://URL/WebServices/validateuser.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="IValidateUser_wcf_ValidateUser_InputMessage">
<wsdl:part name="parameters" element="tns:ValidateUser"/>
</wsdl:message>
<wsdl:message name="IValidateUser_wcf_ValidateUser_OutputMessage">
<wsdl:part name="parameters" element="tns:ValidateUserResponse"/>
</wsdl:message>
<wsdl:portType name="IValidateUser_wcf">
<wsdl:operation name="ValidateUser">
<wsdl:input wsaw:Action="http://tempuri.org/IValidateUser_wcf/ValidateUser" message="tns:IValidateUser_wcf_ValidateUser_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IValidateUser_wcf/ValidateUserResponse" message="tns:IValidateUser_wcf_ValidateUser_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="WSHttpBinding_IValidateUser_wcf" type="tns:IValidateUser_wcf">
<wsp:PolicyReference URI="#WSHttpBinding_IValidateUser_wcf_policy"/>
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="ValidateUser">
<soap12:operation soapAction="http://tempuri.org/IValidateUser_wcf/ValidateUser" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ValidateUser_wcf">
<wsdl:port name="WSHttpBinding_IValidateUser_wcf" binding="tns:WSHttpBinding_IValidateUser_wcf">
<soap12:address location="http://URL/WebServices/validateuser.svc"/>
<wsa10:EndpointReference>
<wsa10:Address>
http://URL/WebServices/validateuser.svc
</wsa10:Address>
</wsa10:EndpointReference>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
但我将xml字符串请求放在验证xml应用程序中,这样就可以了:

public String validateUser_WCF(String username, String password){

     String enctoken = TOKEN;
     String testtoken = TestTOKEN;      

     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);

         Element[] header = new Element[2];  
         header[0] = new Element().createElement(NAMESPACE, "To");
         header[0].setName("a1");                 
         header[0].addChild(Node.TEXT, VALIDATION_URI);

         header[1] = new Element().createElement(NAMESPACE_INFOCAD, "a1");                
         header[1].addChild(Node.TEXT, testtoken);

         envelope.headerOut = header;                                 

         httpTransportSE = new HttpTransportSE(VALIDATION_URI);

         httpTransportSE.debug = true;
         //httpTransportSE.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

         httpTransportSE.call(NAMESPACE+SOAP_ACTION_VALIDATION, envelope);

         String response = (String) envelope.getResponse();
         Log.i("RESPONSE", response);
         return response;  

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
             Log.i(getClass().getSimpleName(),"requestDump : "+httpTransportSE.requestDump);
             Log.i(getClass().getSimpleName(),"responseDump : "+httpTransportSE.responseDump);
        }
public String validateUser\u WCF(字符串用户名、字符串密码){
字符串enctoken=TOKEN;
字符串testtoken=testtoken;
SoapSerializationEnvelope=null;
SoapObject请求=null;
HttpTransportSE HttpTransportSE=null;
试一试{
请求=新的SoapObject(名称空间、验证方法);
request.addProperty(“用户名”,用户名);
request.addProperty(“密码”,password);
信封=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(请求);
元素[]头=新元素[2];
header[0]=new-Element().createElement(命名空间“To”);
标头[0]。集合名(“a1”);
标头[0]。addChild(Node.TEXT,验证\u URI);
header[1]=newelement().createElement(名称空间_INFOCAD,“a1”);
头[1].addChild(Node.TEXT,testtoken);
envelope.headerOut=页眉;
httpTransportSE=新的httpTransportSE(验证URI);
httpTransportSE.debug=true;
//httpTransportSE.setXmlVersionTag(“”);
调用(名称空间+SOAP\u操作\u验证,信封);
字符串响应=(字符串)信封。getResponse();
Log.i(“响应”,响应);
返回响应;
}捕获(例外e){
//TODO:处理异常
e、 printStackTrace();
}最后{
Log.i(getClass().getSimpleName(),“requestDump:”+httpTransportSE.requestDump);
Log.i(getClass().getSimpleName(),“responseDump:”+httpTransportSE.responseDump);
}

我真的很希望有人能帮我。我到达了一个死胡同….

你能控制WCF部件吗

最简单的方法是更新WCF binding。目前它是
WSHttpBinding
,该绑定比*.asmx复杂得多

检查是否可以使用
basicHttpBinding
binding发布端点

该绑定与*.asmx兼容,您只需在java代码中更新URL即可使用它。

我自己解决了这个问题(WCF绑定必须是basicHttpBinding,否则它将无法工作):

private静态最终字符串命名空间=”http://tempuri.org/";
私有静态字符串URL=“您的URL”;
私有静态最终字符串SOAP\u ACTION\u VALIDATION=“IValidateUser\u wcf/ValidateUser”;
私有静态最终字符串验证\u METHOD=“ValidateUser”;
公共布尔validateUser_WCF(字符串用户名、字符串密码){
SoapSerializationEnvelope=null;
SoapObject请求=null;
HttpTransportSE HttpTransportSE=null;
试一试{
请求=新的SoapObject(名称空间、验证方法);
request.addProperty(“用户名”,用户名);
request.addProperty(“密码”,password);
信封=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(请求);
//////////////////////////////                               
//如果需要,可以在此处添加标题元素
元素[]头=新元素[1];
header[0]=new Element().createElement(名称空间_INFOCAD,“a1”);
header[0].addChild(Node.TEXT,“HeaderTextContent”);
envelope.headerOut=页眉;
//////////////////////////////                               
httpTransportSE=newhttptransportse(URL+VALIDATION_URI,10*10000);//第二个参数是timeout
httpTransportSE.debug=true;
httpTransportSE.setXmlVersionTag(“”);
调用(名称空间+SOAP\u操作\u验证,信封);
//若响应是一个简单的文本结果,则可以调用SoapPrimitive,若不是,则必须调用SoapObject结果并像xml文件一样在响应树中导航
SoapPrimitive结果=(SoapPrimitive)信封.getResponse();
//获取数据。
字符串textResult=result.toString();
Log.i(“textResult”,textResult);
返回true;
}捕获(例外e){
//TODO:处理异常
e、 printStackTrace();
}最后{
//在这里,您可以在日志中看到您发送的请求和收到的响应
Log.i(getClass().getSimpleName(),“requestDump:”+httpTransportSE.requestDump);
Log.i(getClass().getSimpleName(),“responseDump:”+httpTransportSE.responseDump);
}
返回false;
}

我不能修改WCF。只能尝试使用。顺便说一句,我不明白为什么人们不说为什么就不回答这个问题。我想我当时的问题很清楚,我要求设置basicHTTPbinding,为了我的运气,他们更改了绑定。现在一切都很好,但问题总是无法用WS-binding解决……你去了吗这不是你问题的解决方案。如果是,请帮助我。我也遇到了与我得到的解决方案相同的问题。问题出在绑定中。如果WCF绑定设置为WSHttpBinding,你不能使用它。更改为basicHttpBinding我解决了。顺便说一句,我会在你的帖子问题上发布我使用WCF服务的完整代码。给我一点时间,好吗k到你的帖子。我添加了我的工作代码。我可以将其中一个参数作为xml发送吗?作为xml?也许我不理解你的问题。SoapSerializationEnvelope对象已经构建了一个xml。给我一个例子,plsit构建了一个xml,但我的wcf服务方法类似于
Methodname(strinputXML,StroutXML)
。那么我如何设置soap对象呢
XmlPullParserException: unexpected type 
(position:END_DOCUMENT null@1:1 in 
java.io.InputStreamReader@blablabla)
public String validateUser_WCF(String username, String password){

     String enctoken = TOKEN;
     String testtoken = TestTOKEN;      

     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);

         Element[] header = new Element[2];  
         header[0] = new Element().createElement(NAMESPACE, "To");
         header[0].setName("a1");                 
         header[0].addChild(Node.TEXT, VALIDATION_URI);

         header[1] = new Element().createElement(NAMESPACE_INFOCAD, "a1");                
         header[1].addChild(Node.TEXT, testtoken);

         envelope.headerOut = header;                                 

         httpTransportSE = new HttpTransportSE(VALIDATION_URI);

         httpTransportSE.debug = true;
         //httpTransportSE.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

         httpTransportSE.call(NAMESPACE+SOAP_ACTION_VALIDATION, envelope);

         String response = (String) envelope.getResponse();
         Log.i("RESPONSE", response);
         return response;  

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
             Log.i(getClass().getSimpleName(),"requestDump : "+httpTransportSE.requestDump);
             Log.i(getClass().getSimpleName(),"responseDump : "+httpTransportSE.responseDump);
        }
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("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        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;
}