Session 将会话从HttpAdapter获取到基于Java的适配器
有没有办法将在httpAdapter中创建的会话放入适配器中的java类? 我正在使用httpadapter连接到,但是我现在想使用一些java代码连接到适配器中的相同url。所以我不知道如何访问已经创建的会话 这是我的连接代码:Session 将会话从HttpAdapter获取到基于Java的适配器,session,https,adapter,ibm-mobilefirst,Session,Https,Adapter,Ibm Mobilefirst,有没有办法将在httpAdapter中创建的会话放入适配器中的java类? 我正在使用httpadapter连接到,但是我现在想使用一些java代码连接到适配器中的相同url。所以我不知道如何访问已经创建的会话 这是我的连接代码: In javaScript, function login(){ var lpath = getLoginPath(); var input = { method : 'post', returnedContentType : 'html',
In javaScript,
function login(){
var lpath = getLoginPath();
var input = {
method : 'post',
returnedContentType : 'html',
path : lpath,
parameters : {
j_password : passwd,
j_username : username
}
};
var request = WL.Server.getClientRequest();
UserSession = request.getHeader("Cookie"); // here i am setting the value for a global variable UserSession.
return WL.Server.invokeHttp(input);
}
function getConnection(){
var instance = new com.worklight.customcode.PostRequest();
var rspath = getRootServicesPath();
var input = {
method : 'get',
returnedContentType : 'xml',
path : rspath
};
return {
result : instance.getXml(UserSession) // Here i am passing the value of session cookie into the java class
};}
在Java类中
class PostRequest{
public String getXml(String cookieString){
String params = "?offlineData={'projectAreaItemId':'_iTuLUmxfEeKR3t32SVbLKQ','executables':[{'terItemId':'_TNoyQW6qEeKR3t32SVbLKQ','scriptItemId':'_DF89AW6qEeKR3t32SVbLKQ'}]}";
String url = "https://jazz.net/sandbox02-qm/secure/service/com.ibm.rqm.execution.common.service.IOfflineExecutionRestService"
+ params;
URLConnection connection = new URL(url).openConnection();
connection = new URL(url).openConnection();
connection.setRequestProperty("User-Agent", "yes");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Accept-Encoding", "gzip");
connection.setRequestProperty("Accept", "application/xml");
connection.setRequestProperty("Cookie", cookieString);
connection.setDoInput(true);
connection.setDoOutput(true);
((HttpURLConnection) connection).setRequestMethod("POST");
connection.setDoOutput(true);
String result = "";
InputStream resp = connection.getInputStream();
StringBuilder s = inputStreamToString(resp);
return s;
}
}
您不需要访问会话ID,因为您已经“拥有它”。 您编写的Java类是适配器会话或作用域的一部分 您需要做的是在适配器中声明您的类。大概是这样的: 班级
Class myHTTPClient
// implementation code goes here - the java code to connect to the same URL as in the adapter XML.
适配器
var httpClient = new com.myHTTPClient
// your adapter code
这样,Java与适配器处于相同的作用域中,因此不需要“获取”会话ID
为了从Java代码(页面底部)在POST中传递JSESSIONID。在适配器JavaScript中,实现示例。。。response.responseHeader包含一个“Set Cookie”头,您可以从中提取JSSessionID,传递并使用它。但当我试图从java类中发布帖子时,它会询问用户凭据。我在登录httpAdapter时使用了这两行,其中Cookie是一个全局变量。我试图将其传递给java类。它只能正确通过。但是我不知道我在建立与injava类的联系时是否做错了什么。var request=WL.Server.getClientRequest();Cookies=request.getHeader(“Cookie”);不要对代码使用注释。编辑你的问题。添加java代码,添加javascript。另外,标题名是“Set Cookie”。我尝试过这样做,但收到的响应是错误的请求(状态代码400)