Reputation: 51
Im frustraded. I've got a Basic Http Authorization. For this I set a HttpGet
Header.
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 header
it 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
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