HTTP 标头中没有 cookie

发布于 2024-10-04 23:30:46 字数 3417 浏览 0 评论 0原文

我在 BlackBerry 中创建了 HttpConnection。它返回给我一个成功的登录信息,但我无法检索 cookie,因为标头中没有 cookie。有谁知道我怎样才能得到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 )

I have made a HttpConnection in BlackBerry. It has return me a successful login but I am unable to retrieve the cookie as there isn't one in the header. Does anyone know how I can get the cookie?

This is the code..

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 )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘味果▽酱 2024-10-11 23:30:46

仔细检查服务 API。也许您期望的东西不存在(服务不使用 cookie),或者您没有按预期使用 API(服务支持 cookie,但服务请求没有要求)。

或者,但服务 API 应该告诉它,cookie 是使用 javascript 在本地生成的。在这种情况下,您将找不到标头条目,并且必须执行脚本代码来设置 cookie。

Double check the service API. Maybe you're expecting something that doesn't exist (service doesn't use cookie) or maybe you didn't use the API as expected (service supports cookie but service request didn't ask for one).

Or, but the service API should tell it, the cookie is generate locally with javascript. In this case, you won't find a header entry and you'll have to execute the script code to set the cookie.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文