2010-11-30 95 views
1

我在BlackBerry中創建了一個HttpConnection。它已經返回我一個成功的登錄,但我無法檢索cookie,因爲標題中沒有一個。有誰知道我如何獲得cookie?HTTP標頭中沒有cookie

這是代碼..

private String login(String URL) 
{ 

    HttpConnection  httpConn = null; 
    DataInputStream  dis = null; 
    DataOutputStream dos = null; 
    StringBuffer  responseMessage = new StringBuffer(); 
    // the request body 

    //Encode the login information in Base64 format. 

    //String username = userName.getString(); 
    //String password = userPassWord.getString(); 
    // username = loginScreen.getUserId(); 
    //password = loginScreen.getPassword(); 
    try { 
     // an HttpConnection with both read and write access 
     net.rim.blackberry.api.browser.URLEncodedPostData login = new net.rim.blackberry.api.browser.URLEncodedPostData(null, false); 
     login.append("username"); //field name , value 
     login.append("password"); 
     httpConn = (HttpConnection)Connector.open(URL, Connector.READ_WRITE); 

     // set the request method to POST 
     httpConn.setRequestMethod(HttpConnection.POST); 
     httpConn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, HttpProtocolConstants.CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED); 
     httpConn.setRequestProperty("Accept", "text/html");  

     // obtain DataOutputStream for sending the request string 
     dos = httpConn.openDataOutputStream(); 
     byte[] request_body = login.getBytes(); 

     // send request string to server 
     for(int i = 0; i < request_body.length; i++) { 
      dos.writeByte(request_body[i]); 
     }//end for(int i = 0; i < request_body.length; i++)   

     for (int i=0; ; i++) { 
      String headerName = httpConn.getHeaderFieldKey(i); 
      String headerValue = httpConn.getHeaderField(i); 
      if (headerName == null && headerValue == null) { 
       // No more headers 
       break; 
      } else 
       responseMessage.append("headerName : " + headerName + ", headerValue : " + headerValue + "\n");     
     } 

     // obtain DataInputStream for receiving server response 
     dis = new DataInputStream(httpConn.openInputStream()); 

     // retrieve the response from server 
     int data;  
     tmpCookie = httpConn.getHeaderField("Set-Cookie"); 

     responseMessage.append("1st Cookie" + tmpCookie); 
     if (tmpCookie != null) { 
     int semicolon = tmpCookie.indexOf(';'); 
      cookie = tmpCookie.substring(0, semicolon); 
      responseMessage.append("Cookie" + cookie); 
      } 

     responseMessage.append("LOGIN RESPONSE :"); 
     while((data = dis.read()) != -1) { 
      responseMessage.append((char)data ); 

     }//end while((ch = dis.read()) != -1) {   

    } 

    catch(Exception e) 
    { 
     e.printStackTrace(); 
     responseMessage.append("ERROR"); 
    } 
    finally { 
     // free up i/o streams and http connection 
     try { 
      if(httpConn != null) httpConn.close(); 
      if(dis != null) dis.close(); 
      if(dos != null) dos.close(); 
     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     }//end try/catch 
    }//end try/catch/finally 
    return responseMessage.toString(); 
}//end sendHttpPost(String) 
+0

你是如何創建一個http連接的? – 2010-11-30 09:49:02

+0

http連接是什麼?也許該網站不會使用cookies進行會話? – 2010-11-30 09:49:03

回答

0

仔細檢查服務API。也許你期望的東西不存在(服務不使用cookie)或者你沒有按預期使用API​​(服務支持cookie,但服務請求沒有要求使用cookie)。或者,但服務API應該告訴它,cookie是使用javascript在本地生成的。在這種情況下,您將無法找到標題條目,並且必須執行腳本代碼才能設置Cookie。