0

我使用 ksoap 从我的黑莓 java 应用程序发出 http 请求。

我能够发出请求并检索响应。但是当网络服务要求我的应用程序在发出请求时使用 sessionID 设置标头 cookie 时。我检查了 HTTPTransport 但无法设置像 android 这样的标头属性。我不知道如何使用 ksoap 来做到这一点。

当前使用的 ksoap 是 2.1.2 J2ME 内核。我什至尝试添加支持 HTTPTransportSE 的 android ksoap jar 文件,但由于预验证不成功而失败。

任何人都可以建议我可以做些什么来设置带有 cookie 会话的 header 属性吗?

谢谢你。

更新了代码。更新代码底部的问题。

肥皂管理器.java

package myAppName.soap;

import myAppName.ObjectClass.AppSettings;
import java.io.IOException;
import javax.microedition.io.ConnectionNotFoundException;
import net.rim.device.api.system.DeviceInfo;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;
import org.xmlpull.v1.XmlPullParserException;

public class SoapManager {

    public static String exception = "";

    public static Object callSoap(SoapProperty soapProperty) throws IOException, XmlPullParserException{
        return call(soapProperty);
    }

    public static Object call(SoapProperty soapProperty) throws IOException {
        Object result = null;

        SoapSerializationEnvelope env = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        env.headerOut = buildHeader();

        env.bodyOut = soapProperty.getSoapObject();

        System.out.println("##########Created envelope =" + env.toString());

        HttpTransport http = new HttpTransport(soapProperty.getEndPointUrl());
        http.debug = false;

        System.out.println("##########Created httptransport =" + http.toString());

        //get from static variable
        AppSettings appSetting = new AppSettings();

        /*String sessionID;
        Object sessionObject = appSetting.get("sessionID");
        if (sessionObject != null) {
            sessionID = sessionObject.toString();
        }else {
            sessionID = "";
        }*/

        try {
            System.out.println("#######Performing http call");
            http.call(soapProperty.getEndPointUrl(), env);

            System.out.println("#%%#%%#%Getting response" + env.getResponse().toString());
            result = env.getResponse();

            /*
             * extract csrf_token from SOAP header
             */
            System.out.println("######Saving csrfToken from return header");
            Element[] elementReturned = env.headerIn;
            if(elementReturned[0].getChildCount() > 0) {
                appSetting.set("csrfToken", ((Element)elementReturned[0].getChild(0)).getText(0));
                appSetting.commit();
            }
            else {
                appSetting.set("csrfToken", "");
                appSetting.commit();
            }

        } catch(ConnectionNotFoundException ce) {
            exception = "connectException";
        } catch(NullPointerException npe) {
            exception = "nullPointerException";
        } catch(Exception e) {
            exception = e.getMessage();
            exception = "other";
        }

        return result;
    }

    private static Element[] buildHeader() {

        Element[] header = new Element[1];
        header[0] = new Element().createElement(null,"Security");

        Element appVersion = new Element().createElement(null, "appversion");
        appVersion.addChild(Node.TEXT, "1.0");
        header[0].addChild(Node.ELEMENT, appVersion);

        Element id = new Element().createElement(null, "id");
        id.addChild(Node.TEXT, "soapui491");
        header[0].addChild(Node.ELEMENT, id);

        Element make = new Element().createElement(null, "make");
        make.addChild(Node.TEXT, DeviceInfo.getManufacturerName());
        header[0].addChild(Node.ELEMENT, make);

        Element model = new Element().createElement(null, "model");
        model.addChild(Node.TEXT, DeviceInfo.getDeviceName());
        header[0].addChild(Node.ELEMENT, model);

        Element type = new Element().createElement(null, "type");
        type.addChild(Node.TEXT, "x");
        header[0].addChild(Node.ELEMENT, type);

        Element os = new Element().createElement(null, "os");
        os.addChild(Node.TEXT, DeviceInfo.getSoftwareVersion());
        header[0].addChild(Node.ELEMENT, os);

        Element csrf_token = new Element().createElement(null, "csrf_token");

        AppSettings appSetting = new AppSettings();
        String token;
        if (appSetting.get("csrfToken") != null) {
            token = appSetting.get("csrfToken").toString();
        }else {
            token = "";
        }

        csrf_token.addChild(Node.TEXT, token);
        header[0].addChild(Node.ELEMENT, csrf_token);

        return header;
    }
}

肥皂属性.java

package myAppName.soap;

import org.ksoap2.serialization.SoapObject;

public class SoapProperty {

    private String endPointUrl;
    private String soapAction;
    private SoapObject soapObject;

    public SoapProperty(String endPointUrl, String soapAction, SoapObject soapObject){
        setEndPointUrl(endPointUrl);
        setAction(soapAction);
        setSoapObject(soapObject);
    }

    //setter and getter
    public String getEndPointUrl(){
        return endPointUrl;
    }

    public void setEndPointUrl(String endPointUrl){
        this.endPointUrl = endPointUrl;
    }

    public String getAction(){
        return soapAction;
    }

    public void setAction(String soapAction){
        this.soapAction = soapAction;
    }

    public SoapObject getSoapObject(){
        return soapObject;
    }

    public void setSoapObject(SoapObject soapObject){
        this.soapObject = soapObject;
    }
}

SoapUtil.java

package myAppName.soap;

import org.ksoap2.serialization.SoapObject;

public class SoapUtil {

    private String methodName;
    private SoapObject soapObject ;
    private SoapProperty soapProperty;
    private String soapNameSpace = "http://xxxxxxxx/";
    private String soapUrl = "https://xx.xxx.xx.xxx:xxxx/xxxx/xxxxxx;interface=wifi";

    public SoapUtil(String methodName) {
        setMethodName(methodName);
        soapObject = new SoapObject(soapNameSpace, getMethodName());
    }

    public SoapObject SoapConnect() throws Exception {
        SoapObject resultSoap = null;
        try{
            if(soapProperty==null)
                soapProperty = new SoapProperty(soapUrl,
                        soapNameSpace+methodName, soapObject);
            else
                soapProperty.setSoapObject(soapObject);
            Object objResult =  SoapManager.callSoap(soapProperty);
            if(objResult!=null)
                resultSoap = (SoapObject) objResult;

        }catch (Exception e) {
            e.printStackTrace();
        }

        return resultSoap;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public void setSoapProperty(String param , Object value) {
        soapObject.addProperty(param, value);
    }
}

任何人都知道如何访问 ksoap HTTPTransport 的 HTTPConnection 以便我可以在那里检索或设置soap header cookie?还是ksoap以外的任何其他方法?请指教。

谢谢你。

4

0 回答 0