ksoap を使用して、Blackberry Java アプリから http リクエストを作成しています。
リクエストを作成してレスポンスを取得できました。しかし、Web サービスで、リクエスト時にセッション ID を使用してヘッダー Cookie を設定する必要がある場合。HTTPTransport を確認しましたが、android のようなヘッダー プロパティを設定する方法がありません。ksoap でこれを行う方法がわかりません。
現在使用している ksoap im は 2.1.2 J2ME コアです。HTTPTransportSE をサポートする android ksoap jar ファイルを追加しようとしましたが、preverify が成功しないため失敗しました。
Cookieセッションでヘッダープロパティを設定するために何ができるかアドバイスできますか?
ありがとうございました。
コードで更新されました。コードの下部にある質問を更新しました。
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以外の方法はありますか?ご意見をお聞かせください。
ありがとうございました。