2010-11-26 71 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