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
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: HttpConnection Credentials

Your Server is not using Basic Authentication, it is using Digest Authentication.  This is a stronger form of identification, for example it does not send the password in the clear.  There are, to my knowledge, no supplied samples that deal with this form of authentication, you will have to code this yourself.  You will find the specification for it here:

http://tools.ietf.org/html/rfc2617

 

Good luck, let us know how you get on with this.

Contributor
Posts: 29
Registered: ‎04-05-2010
My Device: Curve 8520
My Carrier: NI

Re: HttpConnection Credentials

[ Edited ]

 

Hi peter,
I´m working to do this connection, of course I will publish here,
One question more, Can I get, this values nonce, qop, etc.. from  
httpConn.getHeaderField("WWW-Authenticate");, separate?

 

 

 

thanks

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: HttpConnection Credentials

Look at the sample code you will find here:

http://supportforums.blackberry.com/t5/Java-Development/Sample-HTTP-Connection-code-and-BIS-B-Access...

 

You will find the following lines:

                    rc = _httpConnection.getResponseCode();
                    //---------------------------------------------------------------------------------------

                    // Log header info - can be very useful if there are problems
                    StringBuffer sb = new StringBuffer();
                    sb.append("Response: " + Integer.toString(rc) + ", " + _httpConnection.getResponseMessage() + "\n");
                    for ( int j = 0; j < 20; j++ ) {
                        // Limit to 20 headers, just to reduce data sent to log.
                        String key = _httpConnection.getHeaderFieldKey(j);
                        String field = _httpConnection.getHeaderField(j);
                        if ( key == null && j > 0 ) {
                            // Stop when there are no more!
                            break;
                        }
                        sb.append("Header: " + Integer.toString(j) + ", " +
                                             key + " : " + field + "\n");
                        if ( key.equalsIgnoreCase(HttpProtocolConstants.HEADER_LOCATION) ) {
                            // Save redirect location in case we are redirected
                            // Note potential bug, if we have more than 20 headers this will not be found!
                            redirect = field;
                        }
                    }
                    Logger.logEventInfo(sb.toString());

 

This code extracts the user headers and prints them out, and also detects and saves the HttpProtocolConstants.HEADER_LOCATION into a String variable.  You will need to something similar for the Authentication header, and then parse this header to extract the values you need.

 

I actually used this sample code to test your URL and this is the header I got back:

 

Header: 3, WWW-Authenticate : Digest realm="Restricted area",qop="auth",nonce="4d342405252c0",opaque="cdce8a5c95a1427d74df7acbf41c9ce0"

Highlighted
Contributor
Posts: 29
Registered: ‎04-05-2010
My Device: Curve 8520
My Carrier: NI

Re: HttpConnection Credentials

Ok!!!!

 

I´ve done it already!!!

 

Here is the solution!

 

 

s = (StreamConnection)Connector.open(url+";deviceside=true");
 httpConn = (HttpConnection)s;
String digest;
//strign digest  you have to implent as http://en.wikipedia.org/wiki/Digest_access_authentication
 while(keepGoing){
 
            int status = httpConn.getResponseCode();
            
            System.out.println("HTTP RESPONSE = " + status);
            switch (status){
            case (HttpConnection.HTTP_OK):
                   //Connection is 200 OK.
                   //Download and process data.
            InputStream out = httpConn.openInputStream();
            byte[] data = new byte[256];
            int len = 0;
        
            StringBuffer raw = new StringBuffer();
           while( -1 != (len = out.read(data))) {
               raw.append(new String(data, 0, len));
          }
          result = raw.toString();
          keepGoing = false;            
          break;
          case (HttpConnection.HTTP_UNAUTHORIZED):
               s.close();
               s = (StreamConnection)Connector.open(url+";deviceside=true");
               httpConn = (HttpConnection)s;
               httpConn.setRequestProperty("Authorization", digest);
               result +=  "HTTP = " + status;
                contador++;
          break;
          default:
                   //The connection failed for some other reason.
                   //Handle failed connection.
                   keepGoing = false;
          break;
           }
}
s.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}