如何在黑莓java上维护http头会话cookie?

时间:2013-09-23 11:50:20

标签: java cookies blackberry header ksoap

我正在使用kso​​ap从我的黑莓java应用程序发出http请求。

我能够发出请求并检索回复。但是当Web服务要求我的应用程序在发出请求时使用sessionID设置标头cookie。我检查了HTTPTransport,但没有办法像android一样设置头部属性。我不知道怎么用ksoap这样做。

目前使用的ksoap是2.1.2 J2ME核心。我甚至尝试添加支持HTTPTransportSE的android ksoap jar文件但由于preverify没有成功而失败。

有人可以建议我用cookie会话设置header属性吗?

谢谢。

已更新代码。在代码底部更新了问题。

SoapManager.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;
    }
}

SoapProperty.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标头cookie?或除了ksoap之外的任何其他方法?请指教。

谢谢。

0 个答案:

没有答案