android


android basic auth okhttpclient


I am connecting to my rest api using retrofit and okHttp client. When I disable Basic authentication on Tomcat everything works flawlessly.
When Basic Auth is enabled on Tomcat I get 404 Page not found.
Here is my authentication and error output.
okHttpClient.setAuthenticator(new Authenticator() {
#Override
public Request authenticate(Proxy proxy, Response response) throws IOException {
String credential = Credentials.basic(rest_user, rest_pw);
return response.request().newBuilder().header("Authorization", credential).build();
}
#Override
public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
return null;
}
});
Error log
D/YOUR_LOG_TAG: <--- HTTP 404 https://myserver:8443/RestWS/objects/barcodes (4462ms)
D/YOUR_LOG_TAG: Server: Apache-Coyote/1.1
D/YOUR_LOG_TAG: Cache-Control: private
D/YOUR_LOG_TAG: Expires: Thu, 01 Jan 1970 01:00:00 CET
D/YOUR_LOG_TAG: Set-Cookie: JSESSIONID=*********************; Path=/; Secure; HttpOnly
D/YOUR_LOG_TAG: Content-Type: text/html;charset=ISO-8859-1
D/YOUR_LOG_TAG: Content-Length: 127
D/YOUR_LOG_TAG: Date: Tue, 26 Apr 2016 09:31:22 GMT
D/YOUR_LOG_TAG: OkHttp-Selected-Protocol: http/1.1
D/YOUR_LOG_TAG: OkHttp-Sent-Millis: 1461663081883
D/YOUR_LOG_TAG: OkHttp-Received-Millis: 1461663081973
D/YOUR_LOG_TAG: <html>
D/YOUR_LOG_TAG: <head>
D/YOUR_LOG_TAG: <title>404-Page Not Found</title>
D/YOUR_LOG_TAG: </head>
D/YOUR_LOG_TAG: <body> The requested URL was not found on this server. </body>
D/YOUR_LOG_TAG: </html>
D/YOUR_LOG_TAG: <--- END HTTP (127-byte body)
Problem solved using RequestInterceptor.
restAdapter = new RestAdapter.Builder()
.setConverter(new GsonConverter(gson))
.setRequestInterceptor(new RequestInterceptor() {
#Override
public void intercept(RequestFacade request) {
String credentials = DownloadService.rest_user + ":" + DownloadService.rest_pw;
String base64EncodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
request.addHeader("Authorization", "Basic " + base64EncodedCredentials);
}
})
.setEndpoint(DownloadService.URL)
.setClient(new OkClient(okHttpClient))
.build();

Related Links

ANDROID - RatingBar in ListView example required for send data into MySQL
how can I get an android vertical grid view in my app?
DatabaseWrapper cannot be converted to SQLiteDatabase
Firebase Database is taking too much time
Getting lint error on travis but not locally
Shared Preferences to be able to transfer between checking and saving account
QCAT tool for log analysis
Android - Access Root Directory
Cast Dialog not showing artist value for MEDIA_TYPE_MUSIC_TRACK type
Why can't I use Sensor.TYPE_TILT_DETECTOR?
Android: iText lib:parse PDF to String
Playing Video from RAM in Android
Android icon in navigation bar
Android WebView loses AppBar
Android Searchview back button color change
Detect common mimetype for file sharing

Categories

HOME
shell
go
c++11
string
pda
jmx
toolbox
solaris
graphics
newrelic
yandex
alsa
zip
trac
flume
project
google-chrome-app
endeca
mel
activecollab
x-cart
internationalization
mathprog
foreign-keys
boost-icl
rubymotion
spring-async
systemtime
postscript
displaytag
perlbrew
siri
inversion-of-control
spotipy
tpm
subclass
errbot
boost-asio
mobx
git-squash
login-script
contacts
installshield-le
yowsup
sim-toolkit
vmware-fusion
sharp-snmp
avcapturedevice
flask-login
rfc3339
openresty
ntvs
vugen
launchctl
custom-font
otp
payload
janus
d3-force-directed
py2neo
testrail
rocks
flex-monkey
xcode-server
lrs
installshield-2010
expectations
refinerycms
stream-processing
kiosk
character-replacement
dynatree
java-money
lazybones
jdi
fuelcms
coda
sony-lifelog-api
viola-jones
typemock
snackbar
kubuntu
phpldapadmin
gamekit
mass-assignment
raygun
encryption-asymmetric
trdion2011
cuba
mintty
parallel-testing
ray
draw2d-js
vsx
xmltodict
hibernate-entitymanager
c++builder-xe5
code-duplication
away3d
ice-cube
modular
facebook-timeline
node-blade
traceability
cascadingdropdown
twisted.web
onmousemove
adobe-media-server
josql
promotion-code
filemerge
lr
twitterizer
presentation-layer
globals
openfaces
tui
wordprocessingml

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App