2010-11-26 67 views
0

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

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(); 

回答

1

您是否确实建立了连接?您的代码显示您设置了一个请求属性,然后立即尝试查找一个标头值,但没有指出该请求实际上已经发送。

如果你这样做(在这种情况下,更全面的代码将受到欢迎),你应该使用Wireshark或类似的东西,找出了网络流量实际上看起来像。

0

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

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

+0

我正在使用Set-Cookie。它仍然显示为空。我知道我想要从服务器获得一个cookie,但我只是没有得到它。 – JohnDoe4136 2010-11-26 08:18:43