android - 使用 ksoap2 在 android 中创建带有安全 header 的 soap 信封

标签 android wsdl ksoap2

我想使用 ksoap2 在 android 中创建带有安全 header 的 soap 信封。我的android代码是...

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SOP_WebService extends Activity
{

    private final String NAMESPACE = "http://xmlns.oracle.com/WorklistRetrival";
    private final String URL = "http://www.sample.com/orabpel/default/WorklistRetrival/1.0";
    private final String SOAP_ACTION = "process";
    private final String METHOD_NAME = "WorklistRetrievalREQ";  

    public void onCreate(Bundle savedInstanceState) 
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.soap_webservice);

         Button btnClick = (Button) findViewById(R.id.btnClick);
         btnClick.setOnClickListener(new OnClickListener()
         {
            @Override
            public void onClick(View v) 
            {
                callWebservice();
            }
        });
    }

    public void callWebservice()
    {

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        PropertyInfo pi = new PropertyInfo();
        pi.setNamespace(NAMESPACE);
        pi.setName("WorklistType"); 
        pi.setValue("PO_REQUISITION"); 
        request.addProperty(pi);

        PropertyInfo p2 = new PropertyInfo();
        p2.setNamespace(NAMESPACE);
        p2.setName("Status"); 
        p2.setValue("TODO"); 
        request.addProperty(p2);

        PropertyInfo p3 = new PropertyInfo();
        p3.setNamespace(NAMESPACE);
        p3.setName("Mode"); 
        p3.setValue(""); 
        request.addProperty(p3);


         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        Log.i("bodyout", "" + envelope.bodyOut.toString());

        try 
        {
            androidHttpTransport.debug = true;
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            Log.i("myApp", response.toString());
            Log.i("request", "" + envelope.bodyIn);
            Log.i("response", "" + envelope.bodyOut);
            Log.i("request", "" + androidHttpTransport.requestDump);
            Log.i("response", "" + androidHttpTransport.responseDump);
        } 
        catch (SoapFault e)
        {
            Log.d("soapFault", "soapFault");
            e.printStackTrace();
        }
        catch (Exception e) 
        {
            Log.d("Exception", "Exception");
            e.printStackTrace();
            Log.d("Exception Generated", ""+e.getMessage());
        }

    }

}

上面的代码在没有安全 header 的 soap 请求下创建。

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <soap:Header>
    </soap:Header>
    <soap:Body xmlns:ns1="http://xmlns.oracle.com/bpel/aubi/mobile/Worklist">
        <ns1:WorklistRetrievalREQ>
            <ns1:WorklistType>HR_OFFER</ns1:WorklistType>
            <ns1:Status>TODO</ns1:Status>
            <ns1:Mode/>
        </ns1:WorklistRetrievalREQ>
    </soap:Body>
</soap:Envelope>

但我需要使用安全 header 创建以下 soap 请求

<soap:Envelope 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">

    <soap:Header>
        <wsse:Security 
            xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
            xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
            xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" soap:mustUnderstand="1">
                <wsse:UsernameToken 
                    xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
                    xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
                            <wsse:Username>cbrown</wsse:Username>
                            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">welcome</wsse:Password></wsse:UsernameToken>
        </wsse:Security>
    </soap:Header>
    <soap:Body xmlns:ns1="http://xmlns.oracle.com/bpel/aubi/mobile/Worklist">
        <ns1:WorklistRetrievalREQ>
            <ns1:WorklistType>HR_OFFER</ns1:WorklistType>
            <ns1:Status>TODO</ns1:Status>
            <ns1:Mode/>
        </ns1:WorklistRetrievalREQ>
    </soap:Body>
</soap:Envelope>

请告诉我这段代码做了什么样的改动

最佳答案

我找到了问题的答案。我会把答案交给有用的人。

public class SOAP_WebService extends Activity
{

    private final String NAMESPACE = "http://ws.simple/";
    private final String URL = "http://10.0.2.2/SimpleWebservice/simple";
    private final String SOAP_ACTION = "http://ws.simple/getString";
    private final String METHOD_NAME = "getString";

    public void onCreate(Bundle savedInstanceState) 
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.soap_webservice);

         Button btnClick = (Button) findViewById(R.id.btnClick);
         btnClick.setOnClickListener(new OnClickListener()
         {
            @Override
            public void onClick(View v) 
            {
                callWebservice();
            }
        });
    }
    public void callWebservice()
    {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        PropertyInfo weightProp =new PropertyInfo();
        weightProp.name = "arg0";
        weightProp.setValue("rajan");
        request.addProperty(weightProp);


        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);


        // create header
        Element[] header = new Element[1];
        header[0] = new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd","Security");
        header[0].setAttribute(null, "mustUnderstand","1");

        Element usernametoken = new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "UsernameToken");
        usernametoken.setAttribute(null, "Id", "UsernameToken-1");
        header[0].addChild(Node.ELEMENT,usernametoken);

        Element username = new Element().createElement(null, "n0:Username");
        username.addChild(Node.IGNORABLE_WHITESPACE,"CBROWN");
        usernametoken.addChild(Node.ELEMENT,username);

        Element pass = new Element().createElement(null,"n0:Password");
        pass.setAttribute(null, "Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
        pass.addChild(Node.TEXT, "welcome");

        usernametoken.addChild(Node.ELEMENT, pass);


        // add header to envelope
        envelope.headerOut = header;


        Log.i("header", "" + envelope.headerOut.toString());


        envelope.dotNet = false;
        envelope.bodyOut = request;
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        Log.i("bodyout", "" + envelope.bodyOut.toString());

        try 
        {
            androidHttpTransport.debug = true;
            androidHttpTransport.call(SOAP_ACTION, envelope);

            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            Log.i("myApp", response.toString());
        } 
        catch (SoapFault e)
        {
            e.printStackTrace();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
            Log.d("Exception Generated", ""+e.getMessage());
        }

    }

}

关于android - 使用 ksoap2 在 android 中创建带有安全 header 的 soap 信封,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14196387/

相关文章:

android - ListView 仅显示最后一个元素

android - 从 Android 应用程序打开 yelp 深层链接

java - 需要使用 maven 从许多 .java 文件生成一个 WSDL 文件

android - 返回错误响应的 SOAP Web 服务

android - 没有硬编码 IP 地址的 KSOAP2 连接

java - 如何自定义TextView中的链接?

java - 保存下载的 XSD 和 WSDL

php - 通过 WSDL/SOAP 使用 Fedex Webservice 的问题

Android:调用Web服务时操作超时

android - 我可以以编程方式截取主屏幕的屏幕截图,而无需生根我的 android 设备吗?