How Do I Send a Basic Password to a Web Page?

Article ID: 1074
Last updated: 01 Feb, 2008
Article ID: 1074
Last updated: 01 Feb, 2008
Revision: 1
Views: 3823
Posted: 22 Jun, 1999
by Dean J.
Updated: 01 Feb, 2008
by Dean J.
Problem


I'm attempting to access a secure web page (basic password protection) with the RWIHttpClient, but there doesn't seem to be mechanism for sending the password to the server. How can I access this page using the Rogue Wave classes?


Cause


Tools.h++ Professional's RWIHttpClient class was designed to provide the basic tools for connecting and accessing an HTTP server. Because of this, a specific mechanism for passing password information to an HTTP server is not provided. The RWIHttpClient class, however, was designed to be extensible so that it could support other user-defined requirements as necessary.


Action


The following class is an extension to RWIHttpClient which implements the ability to add a Basic Authentication Header to a RWIHttpClient request.

 

 class RWINETExport RWKBIHttpBasicAuthorizationHeader : public RWIHttpHeaderBase  {   public: 

// default constructor RWKBIHttpBasicAuthorizationHeader(void) : RWIHttpHeaderBase('Authorization'), user_(), pass_() {}

// constructs an RWKBIHttpBasicAuthorizationHeader object, with // label as Authorization, and it's associated username // and password RWKBIHttpBasicAuthorizationHeader(RWCString user, RWCString pass) : RWIHttpHeaderBase('Authorization'), user_(user), pass_(pass) {}

// sets the internal user value void setUser(RWCString user) { user_ = user; }

// sets the internal password value void setPassword(RWCString pass) { pass_ = pass; }

// returns internal value as string RWCString value(void) const;

protected:

// encodes str as a Base64 string (used w/ user:password value) RWCString encodeBase64(RWCString str) const;

private:

RWCString user_; RWCString pass_; };

RWCString RWKBIHttpAuthorizationHeader::value() const { return 'Basic '+encodeBase64(user_+':'+pass_); }

RWCString RWKBIHttpAuthorizationHeader::encodeBase64(RWCString str) const { RWCString tstr(str); RWCString ret; tstr += '1'; int index = 0; int element = 0;

while (index < str.length()) { char t; switch (element) { case 0: // first 6 bits of char 1 t = tstr[index] >> 2; break; case 1: // last 2 of char 1, first 4 of char 2 t = (tstr[index] & 0x03) << 4; t | = (tstr[index+1] & 0xF0) >> 4; ++index; break; case 2: // last 4 of char 2, first 2 of char 3 t = (tstr[index] & 0x0F) << 2; t | = (tstr[index+1] & 0xC0) >> 6; ++index; break; case 3: // last 6 of char 3 t = tstr[index] & 0x3F; ++index; break; } element = (element + 1) % 4; if (t < 26) ret + = (char)(t + 65); else if (t < 52) ret + = (char)(t + 71); else if (t < 62) ret += (char)(t - 4); else if (t == 62) ret += '+'; else if (t == 63) ret += '/'; }

return ret; }

The above class is provided as an example of how the Basic Authentication Header could be implemented. It has not been thoroughly tested by Rogue Wave Software for robustness or completeness.

 

This article was:   Helpful | Not helpful
Report an issue
Article ID: 1074
Last updated: 01 Feb, 2008
Revision: 1
Views: 3823
Posted: 22 Jun, 1999 by Dean J.
Updated: 01 Feb, 2008 by Dean J.

Others in this category