Basic Auth over HTTPS Java -
i have https page calls working fine , have http basic auth working fine can not basic auth running on https connection.
i attach code example doesn't work. keep getting 403. have checked username , password , has been successful when testing restclinet addon firefox.
any great. thanks.
private static void getapitest() throws malformedurlexception, ioexception { try { system.setproperty("javax.net.ssl.truststore","/home/user/certs/mytruststore"); system.setproperty("javax.net.ssl.truststorepassword", "password"); authenticator myauth = new authenticator() { @override protected passwordauthentication getpasswordauthentication() { return new passwordauthentication("username", "password".tochararray()); } }; authenticator.setdefault(myauth); string httpsurl = "https://someurlandapi.com"; url myurl = new url(httpsurl); httpsurlconnection con = (httpsurlconnection)myurl.openconnection(); //con.setrequestproperty("authorization", "basic " + authstring); con.setrequestproperty("content-type", "application/json; charset=utf-8"); con.setrequestproperty("accept", "application/json"); con.setrequestmethod("post"); inputstream ins = con.getinputstream(); inputstreamreader isr = new inputstreamreader(ins); bufferedreader in = new bufferedreader(isr); string inputline; while ((inputline = in.readline()) != null) { system.out.println(inputline); } ins.close(); in.close(); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } }
i don't think passwordauthentication doing want. basic auth easy handle yourself. if you're in java8 base64.getencoder().encodetostring( ("username" + ":" + "password").getbytes()).
Comments
Post a Comment