Session 保持网络会话
我使用一个我首先需要登录的API,然后获得查看节点的权限。这是使用的两个URLSession 保持网络会话,session,blackberry,Session,Blackberry,我使用一个我首先需要登录的API,然后获得查看节点的权限。这是使用的两个URL http://thewebpage.com/login.php?user=123&pass=123 http://thewebpage.com/get_nodes.php 我用来连接的方法如下 StreamConnection s = (StreamConnection)Connector.open(url); InputStream input = s.openInputStream(); byte[]
http://thewebpage.com/login.php?user=123&pass=123
http://thewebpage.com/get_nodes.php
我用来连接的方法如下
StreamConnection s = (StreamConnection)Connector.open(url);
InputStream input = s.openInputStream();
byte[] data = new byte[256];
int len = 0;
StringBuffer raw = new StringBuffer();
while( -1 != (len = input.read(data))) {
raw.append(new String(data, 0, len));
}
response = raw.toString();
当我连接到login.php
时,我从我登录的服务器得到响应,但问题是当我调用get_nodes.php
时,我得到响应“please login”,因为会话没有存储
有没有办法解决这个问题以便我可以访问节点?
谢谢我最终解决了这个问题,保存了登录时的cookie数据,然后将cookie放入另一个http请求中,如下所示 获取cookie
HttpConnection connection = (HttpConnection)Connector.open(loginUrl);
String cookieData = connection.getHeaderField("Set-Cookie");
HttpConnection connection = (HttpConnection)Connector.open(nodeUrl);
connection.setRequestProperty("Cookie", cookieData);
设置cookie
HttpConnection connection = (HttpConnection)Connector.open(loginUrl);
String cookieData = connection.getHeaderField("Set-Cookie");
HttpConnection connection = (HttpConnection)Connector.open(nodeUrl);
connection.setRequestProperty("Cookie", cookieData);
我最终解决了这个问题,从登录中保存cookie数据,然后将cookie放入另一个http请求中,如下所示 获取cookie
HttpConnection connection = (HttpConnection)Connector.open(loginUrl);
String cookieData = connection.getHeaderField("Set-Cookie");
HttpConnection connection = (HttpConnection)Connector.open(nodeUrl);
connection.setRequestProperty("Cookie", cookieData);
设置cookie
HttpConnection connection = (HttpConnection)Connector.open(loginUrl);
String cookieData = connection.getHeaderField("Set-Cookie");
HttpConnection connection = (HttpConnection)Connector.open(nodeUrl);
connection.setRequestProperty("Cookie", cookieData);