Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 26
Registered: ‎04-09-2013
My Device: not specified
My Carrier: not speciefied

How can I get cookies from Server in httpConection class

I am writing a browser appliacation and would need to

 

1) pass authentication to the server

2) use the BrowserField class to extract information form the server

 

Ther server needs authetication.

 

I thought to implement this with a cookie, but I cannot get a cookie from the server using the httpConnetion class.

 

After authentication I would use the cookie in the requests of the BrowserField class.

 

Here is the relevant code:

 

HttpConnection httpConn = (HttpConnection)Connector.open(_theUrl);
        	httpConn.setRequestProperty("Authorization", "Basic " + new String(encoded));
        	int  status = httpConn.getResponseCode();
        	
        	
        	

        	String cookie=""; 
        	
        	String headerName;
        	for (int i=1; (headerName = httpConn.getHeaderFieldKey(i))!=null; i++) {
        	 	if (headerName.equals("Set-Cookie")) {                  
        		 cookie = httpConn.getHeaderField(i);  
        	 	}
        	}
        	 //
        	
   
        	
        	
        	
        	BrowserField browserField=new BrowserField();
             
             BrowserFieldRequest request=new BrowserFieldRequest(_theUrl);
             BrowserFieldConnectionManager local=browserField.getConnectionManager();
             BrowserFieldCookieManager local_cookie=browserField.getCookieManager();
             local_cookie.setCookie(_theUrl, cookie);
             //local_cookie.addCookiesToRequest(_theUrl, httpConn);
             String cookie_2; 
             cookie_2=local_cookie.getCookie(_theUrl);
        			
            //BrowserFieldRequest request = new BrowserFieldRequest("http://192.168.0.110/Heizung.htm;deviceside=true");
            //browserField.requestContent(URI.create("http://search.yahoo.com/search?p=" + query).toString());
            _browserScreen = new BrowserFieldScreen(request, true);
            extendJavaScript(_browserScreen.getBrowserField());
            pushScreen(_browserScreen);        
        }

 

I find only one header in the loop, and it does not contain cookies.

 

I get a 401 error message from the BrowserField after the request because the authentication which works, I get a 200 response code is not passed to the BrowserField.

 

How can I get a cookie from httpConnection ??

 

The BrowserField class works nicely, I tested it for sites that do not need authentication.

 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: How can I get cookies from Server in httpConection class

If you are using Blackberry infrastructure (i.e. BIS or BES), the infrastructure manages the cookies for you, so you won't see them.

 

However, you *will* see the cookies if you are using a direct connection like WAP2 or maybe a CDMA carrier like Verizon in the USA.

 

In my implimentations that require session management, we always try to manage the cookies because we don't know what type of infrastructure is on the customer's phone. In the cases where the cookies are managed by the infrastructure, we just simply never see them and everything works fine. 

 

Contributor
Posts: 26
Registered: ‎04-09-2013
My Device: not specified
My Carrier: not speciefied

Re: How can I get cookies from Server in httpConection class

I am not using BES, or anything like this. I want to know specifically how I can get a cookie from a httpConnection.  to pass it to a BroweserField object.

 

The cookie is not passed automatically otherwise  Iw uld not get a 401 answer.

 

Is there a Javascript command, is there a http command, do I have to serch the header in more detail to find the cookie. There isa lot of confusing information, unfortunately nothing of it worked.

Contributor
Posts: 26
Registered: ‎04-09-2013
My Device: not specified
My Carrier: not speciefied

WHY is error 401 appearing ??

I proceeded further:

 

1) I set the cookie in the httpconnection

2) I varified the cookie with http.getProperties, it´s there

3) I do the authentication and get response code 200

4) I open a BrowserField

5) I open the Cookie Manager

6) I varified the cookie with the Cookie Manager, it´s also there

7) I pass the request and still get an error 401: Authentication required WHY ?? Smiley Mad

 

Has anyone got an idea ?? What am I doing wrong.

 

I am thankful for good suggestions.

 

httpConn.setRequestProperty("Authorization", "Basic " + new String(encoded));
        	
        	String cookie="cookie1";
        	 String cookie_2=""; 
        	httpConn.setRequestProperty("Cookie",cookie);
        	cookie_2=httpConn.getRequestProperty("Cookie");
        	BrowserField browserField=new BrowserField();
            
            BrowserFieldRequest request=new BrowserFieldRequest(_theUrl);
            BrowserFieldConnectionManager local=browserField.getConnectionManager();
            BrowserFieldCookieManager local_cookie=browserField.getCookieManager();
            cookie_2="";
            cookie_2=local_cookie.getCookie(_theUrl);
            
            local_cookie.addCookiesToRequest(_theUrl, httpConn);
            
        	int  status = httpConn.getResponseCode();
        	

        	String headerName;
        	for (int i=1; (headerName = httpConn.getHeaderFieldKey(i))!=null; i++) {
        	 	if (headerName.equals("Set-Cookie")) {                  
        		 cookie = httpConn.getHeaderField(i);  
        	 	}
        	}
        	 
            
             cookie_2=local_cookie.getCookie(_theUrl);
             
        			
           
            _browserScreen = new BrowserFieldScreen(request, true);

 

Contributor
Posts: 26
Registered: ‎04-09-2013
My Device: not specified
My Carrier: not speciefied

Re: How can I get cookies from Server in httpConection class

There is an older thread on this topic, unfortunately I could not access the link given in the reply

 

http://supportforums.blackberry.com/t5/Web-and-WebWorks-Development/HTTP-Authentication-in-browser-f...

 

 

The link that is not working:

 

How To – Enable cookies in BrowserField
Article Number: DB-00675

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800440/How_To_...

Mark Sohm
BlackBerry Development Advisor
 
Could this link be  made accesable ??
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How can I get cookies from Server in httpConection class

I think this may the article being referenced.

http://supportforums.blackberry.com/t5/Java-Development/Enable-cookies-in-BrowserField/ta-p/445032

 

However I am not sure it will be application since the BrowserField referenced in here might not be the BrowserField2 that is used in more current OS's. 

Contributor
Posts: 26
Registered: ‎04-09-2013
My Device: not specified
My Carrier: not speciefied

authentication with Browserfiled class

I am still struggling with the same problem as in the last thread,  I changed the strategy and try to use the BrowserField class.

 

This is a lovely class it is easy to handle, if no authentication is required from the server.  However with a server requiring authentication I understood that you have to work with cookies, this is hard. Could you confirm this ?

 

 

0) I set a cookie

1) I open the http connection

2) I set the authentication

3) I get response 200

4) I check the cookie with the BroserField tools

5) Isend the request with BrowserField tools

 

But I still get error 401.(Authentication). I gave the detailed code in one of the previous posts.

 

Is that procedure at all useful, or should I return and restrict my self to httpConnection  alone. forgetting about BroserField. Your advice would be very much appreciated.

 

I get the same kind of error message on the real BB device, using the standard browser. What am I doing wrong ?? In the standard Browser I have "cookies" enabled and "Javascript" enabled.

 

I learned to manage the cookies with the cookieManager in the BrowserFiled class. Still Iget the same result, I see that there is a cookie but I still get the 401 error message