用于WCF的Android客户端使用用户名令牌进行wshttpbinding

时间:2012-05-29 08:16:33

标签: android wcf client usernametoken

我正在尝试为WCF WS-Security部署Android 3.0客户端。 WCF WS将用户名令牌实现为安全访问。 我使用KSOAP2并且访问asmx服务没有问题,但是当我尝试调用WCF WS时,应用程序会抛出此异常:

javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚。

看起来这是证书凭据,但我已将服务器凭据添加到我的项目使用的密钥库中。

这是我客户的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.setProperty("http.keepAlive", "false");
    try {
    /**String NAMESPACE = "http://tempuri.org/";
    String URL = "http://**host**/ObtenerDatos/ServicioDatos.asmx";
    String METHOD_NAME = "SumadorDatos";
    String SOAP_ACTION = "http://tempuri.org/SumadorDatos";*/
    String NAMESPACE = "https://tempuri.org/";
    String URL = "http://**host**//WCFServicio/SWObtenerDatos.svc";
    URL url = new URL(URL);
    String METHOD_NAME = "MetodoEnWS";
    String SOAP_ACTION = "http://tempuri.org/IObtenerDatos/MetodoEnWS";
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);      
    request.addProperty("xmlPeticion","dato");
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();     
    StrictMode.setThreadPolicy(policy); 

    KeepAliveHttpsTransportSE transporte = new KeepAliveHttpsTransportSE(url.getHost(),url.getPort(),"",6000);
    try{

        transporte.call(SOAP_ACTION, envelope);

    try {
        SoapPrimitive resultado = (SoapPrimitive)envelope.getResponse();
        String res = resultado.toString();
        TextView tv = new TextView(this);
        tv.setText("El Resultado es: " + res);       
        setContentView(tv);
    } catch (SoapFault e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } 
    }catch(Exception ex){
        ex.printStackTrace();
        this.closeContextMenu();
    }


}

第二个问题。

我不知道如何将用户名令牌和密码发送到服务器头。我必须为自己添加标题创建一个肥皂信封,或者是否存在类似.net的方法,其中我给出用户名的值并传递给特定对象?

1 个答案:

答案 0 :(得分:0)

要将属性添加到标题中,您必须执行以下操作:

     Element[] header = new Element[2];  
     header[0] = new Element().createElement(NAMESPACE, "username");                 
     header[0].addChild(Node.TEXT, "Your username");

     header[1] = new Element().createElement(NAMESPACE, "password");                
     header[1].addChild(Node.TEXT, "Your Password");

     envelope.headerOut = header;                                 

对于证书,您可以添加以下行:

HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setHostnameVerifier(new HostnameVerifier() {
        @Override
        public boolean verify(String arg0, SSLSession arg1) {
            return true;
        }
    });

所以你接受所有证书