Grevius
Grevius

Reputation: 51

Base64 Encoded String doesn't matches

Im frustraded. I've got a Basic Http Authorization. For this I set a HttpGetHeader. This Header needs to be Base64 encoded. I do it like so:

String acc = uname + ":" + pword;
byte[] a = acc.getBytes();
String header = "Basic " + new String(Base64.encode(a, Base64.DEFAULT));

But this encoded String doesn't work. When I log the header, it prints out the same as I need. It looks the same as String h = "Basic c2NodWsZXI6aGVpbmNA=="; Which is the working one. But when I compare header.equals(h); or header==h theire both false. In the end when I set the header to headerit doesn't work, but when I'm using h it works. I guess its sometehing about String encoding but I tried different ways of .getBytes("UTF-8") and similiar (ASCII, UTF-16) but they worked neither. The username and password are normal chars and numbers.

Can anyone see the mistake? Thanks Grevius

Upvotes: 1

Views: 1874

Answers (2)

wns349
wns349

Reputation: 1306

header.equals(h) returning false indicates that the strings are not identical. header==h shall return false since they are not the same reference.

Empty spaces perhaps? try header.trim().equals(h.trim())

Upvotes: 3

forvaidya
forvaidya

Reputation: 3305

try using base64.encodestring(s)

Upvotes: 0

Related Questions