java - HTTPS auth on Android -
i have code http auth android. doesn't work https auth. how can re-factor code under https authorization ?
code http auth below:
httpurirequest request = new httpget(host); string credentials = login + ":" + mpassword; authstring = "basic " + base64.encodetostring(credentials.getbytes(), base64.no_wrap); sharedpreferences.editor editor = prefs.edit(); editor.putstring("auth", authstring); editor.apply(); request.addheader("authorization", authstring); httpclient httpclient = new defaulthttpclient(); int result = 0; try { httpresponse response = httpclient.execute(request); result = response.getstatusline().getstatuscode(); system.out.println(response.getstatusline()); } catch (exception e) { result = -1; } return result;
the default behaviour of httpclient suitable uses, there aspects may want configure. common requirements customizing ssl are:
- ability accept self-signed or untrusted ssl certificates. highlighted sslexception message unrecognized ssl handshake (or similar) being thrown when connection attempt made.
- you want use third party ssl library instead of sun's default implementation.
you may need implementation of custom protocol:
protocol myhttps = new protocol("https", new mysslsocketfactory(), 443); protocol.registerprotocol("https", new protocol("https", new mysslsocketfactory(), 443)); httpclient httpclient = new httpclient(); getmethod httpget = new getmethod("https://www.whatever.com/"); try { httpclient.executemethod(httpget); system.out.println(httpget.getstatusline()); } { httpget.releaseconnection(); }
mysslsocketfactory
custom socket factory implements org.apache.commons.httpclient.protocol.secureprotocolsocketfactory
interface can reference link:mysslsocketfactory.java
Comments
Post a Comment