Cookie issue with Java client using HttpURLConnection + Node.js / Express
webserver
I have a Java client trying to connect to an Express.js webserver serving
content with cookies. The express server works fine on the browser (it
receives session cookies), but not with my Java client. The Java client
shows that the Set-Cookie field on the response header is null.
It's almost certainly a Java problem. I have inspected the network traffic
(using wireshark) and it shows that the Express.js webserver is serving
cookies as expected.
I have the following Java code:
package mine;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpsClient {
public static void main(String[] args) throws Exception {
CookieHandler.setDefault(new CookieManager());
URL obj = new URL("http://localhost:8888");
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
String headerName=null;
for (int i=1; (headerName = conn.getHeaderFieldKey(i)) != null;
i++) {
System.out.println(headerName + ": " + conn.getHeaderField(i));
}
}
}
The output from the above code:
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 4535
ETag: "309029400"
Set-Cookie: null
Date: Wed, 28 Aug 2013 04:40:25 GMT
Connection: keep-alive
Below is the express.js / node.js code:
var express = require('express');
var app = express();
var mongoStore = require('connect-mongo')(express);
app.configure(function(){
app.use(express.logger());
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.errorHandler());
app.use(express.session({
store: new mongoStore({
url: 'mongodb://localhost:27017/sessions'
}),
secret: 'asdasdfasdfasdfasdfa',
maxAge : new Date(Date.now() + 24*3600*1000 * 365),
expires : new Date(Date.now() + 24*3600*1000 * 365)
}));
app.use(express.methodOverride());
});
app.get('/', function(req, res) {
res.send('ok', 200);
});
var http = require('http');
http.createServer(app).listen(8888, function(){
console.log('Express server listening on port ' + 8888);
});
Am I missing something here? Does Java have a different way of handling
session cookies?
No comments:
Post a Comment