server cookie = null

时间:2010-11-26 07:43:30

标签: java blackberry

我正在进行身份验证并收到空cookie。我想存储这个cookie但是服务器没有给我一个cookie。但响应代码是200 ok。

httpConn.setRequestProperty(
    "Authorization", 
    "Basic " + Base64OutputStream.encodeAsString(
                   login.getBytes(), 0, login.getBytes().length, false, false)); 

String tmpCookie = httpConn.getHeaderField("set-cookie");

这是我的代码。

String login = username + ":" + password;

String base = "http://mysever/login";
HttpConnection httpConn = null; 
httpConn = (HttpConnection)Connector.open(base); 
// Setup HTTP Request to POST                
httpConn.setRequestMethod(HttpsConnection.POST);                
httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
httpConn.setRequestProperty("Accept",
    "text/html,application/xhtml+xml,application/xml,application/x-javascript,*/*;q=0.5 ");
//Add the authorized header.
httpConn.setRequestProperty("Authorization", 
    "Basic " + Base64OutputStream.encodeAsString(
                   login.getBytes(), 0, login.getBytes().length, false, false));

message = httpConn.getResponseMessage();
status = httpConn.getResponseCode();           
tmpCookie = httpConn.getHeaderField("Set-Cookie");

EventLogger.logEvent(guid, status);

if (status == HttpConnection.HTTP_OK) 
{
    String tmpCookie = httpConn.getHeaderField("set-cookie");
    authForm.append("\nConnected");
    authForm.append("\n\nLogin Response:" + message + 
                        "\nHTTP response code:" + status + "\nCookie: " 
                        + tmpCookie); 
    //getNewZipFile();
}                   
else if(status !=HttpConnection.HTTP_OK){
    throw new IOException("HTTP response code: " + status); 
}
httpConn.close(); 

2 个答案:

答案 0 :(得分:1)

你真的建立了联系吗?您的代码显示您设置了一个请求属性,然后立即尝试查找标头值,但没有表明该请求实际已被发送。

如果您 这样做(在这种情况下欢迎更全面的代码),您应该使用Wireshark或类似的东西来找出网络流量实际上是什么样的。

答案 1 :(得分:0)

请求授权标头如何与响应Cookie连接? 我想这绝不是。

可能是服务器没有返回任何cookie?或者“set-cookie”可能区分大小写,您必须使用“Set-Cookie”。