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

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

Authentication with BrowserField returns 401 error

I am struggling with getting authentication done with a server by the browserField2 class.


I used the the extended BlackBerry package for Authentication via BrowserFiled.




The relevant code segment is attached.

private HttpConnection requestContentWithCredentials(BrowserFieldRequest request, HttpConnection connection) throws Exception {
        // Put the original headers into a table.
        Hashtable headers = request.getHeaders().toHashtable();
        // Set some mostly random parameters.
        URI uri = URI.create(request.getURL());
         _authScheme.setParameter("method", (request.getPostData() == null) ? "GET" : "POST");
        _authScheme.setParameter("uri", uri.getPath());
        // Set the headers from the authorization scheme, and then build the new connection.
        String hilfe= _authScheme.getAuthResponse();
        headers.put( "Authorization", _authScheme.getAuthResponse() );
        BrowserFieldRequest newRequest = new BrowserFieldRequest(request.getURL(), request.getPostData(), new HttpHeaders(headers));
        HttpConnection newConnection = (HttpConnection)Connector.open(request.getURL());
        newConnection.setRequestProperty("Authorization", hilfe);
         int status=newConnection.getResponseCode();
         newConnection = (HttpConnection)_browserField.getConnectionManager().makeRequest(newRequest);
        // If the new connection is successful, save the credentials used.
        if (!requiresAuthentication(newConnection)) {
            String[] newUsernameAndPassword = {_authScheme.getParameter("username"), _authScheme.getParameter("password")};
            _credentials.put(_authScheme.getParameter("realm"), newUsernameAndPassword);
        return newConnection;


Authenitication works for this server,  if I do it directly with the httpConnection class:


1) openConnection

2) setRequestProperty("Authorization",...)


I get responseCode  200 in the example above, the relevant lines are:

 HttpConnection newConnection = (HttpConnection)Connector.open(request.getURL());
        newConnection.setRequestProperty("Authorization", hilfe);
         int status=newConnection.getResponseCode();

Note that I use exactly the same string information as for the BrowserField class


Authentication does not (!!) work,  if I do it with the BrowserField class, although I checked all the relevant details by using exactly the same string information for both.

 The relevant line for passing the credentials is

 newConnection = (HttpConnection)_browserField.getConnectionManager().makeRequest(newRequest);

It returned error 401, authentication required. The authentication information is passed to the server by the request, which conatins the headers and in the headers there is one containing the ceredntials.  I checked this header with the debugger, it looks fine. Exactly the same string I used for the httpConnection class and could directly connect.


************************Question *************************************************************

Can I look into the ConnectionManager in more detail, why it does not succeed to pass the authentication  data.