原创作者: jeekchen
阅读:1203次
评论:0条
更新时间:2011-05-26
参考了sun的一篇文章
http://developers.sun.com/mobility/midp/articles/sessions/
1、在http response的时候获取cookie
java 代码
- // Query the server and retrieve the response.
- HttpConnection hc= (HttpConnection)Connector.open(url);
- InputStream in = hc.openInputStream();
- // Read the session ID from a cookie in the response headers.
- String cookie = hc.getHeaderField("Set-cookie");
- if (cookie != null) {
- int semicolon = cookie.indexOf(';');
- mSession = cookie.substring(0, semicolon);
- }
- // Read the response from the server.
- // Clean up.
- in.close();
- hc.close();
2、在发送http请求的时候设置cookie
java 代码
- // Query the server and retrieve the response.
- HttpConnection hc = (HttpConnection)Connector.open(url);
- if (mSession != null)
- hc.setRequestProperty("Cookie", mSession);
- InputStream in = hc.openInputStream();
- // Read the session ID from a cookie in the response headers.
- String cookie = hc.getHeaderField("Set-cookie");
- if (cookie != null) {
- int semicolon = cookie.indexOf(';');
- mSession = cookie.substring(0, semicolon);
- }
- // Read the response from the server.
- // Clean up.
- in.close();
- hc.close();
评论 共 0 条 请登录后发表评论