Code example for HttpsURLConnection

Methods: disconnect, getInputStream, getOutputStream, getResponseCode, setDoOutput, setInstanceFollowRedirects, setRequestMethod, setRequestProperty

0
		        sc.init(null, trustAllCerts, new SecureRandom());
		        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
		         
		        URL url = new URL("https://" + GO_HOST + "/authenticate"); 
		        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); 
		        connection.setDoOutput(true); 
		        connection.setInstanceFollowRedirects(false); 
		        connection.setRequestMethod("POST"); 
		        connection.setRequestProperty("Content-Type", "application/json"); 
		        connection.setRequestProperty("Accept", "application/json"); 
		         
		        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
		        out.write("{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}");
		        out.close();
 
		        if(connection.getResponseCode() == 203){
		        	logger.error("Access is denied.  Invalid credentials.");
		        } 
		        if(connection.getResponseCode() == 204){
		        	logger.error("Authentciation URL invalid.");
		        }       
		         
		        if (connection.getResponseCode() != 200){
		        	logger.error("Unable to Authenticate.  Error code " + connection.getResponseCode());
		        }else {        	 
		        	logger.info("Valid GO login details");
		        	 
		        	success = true;
			        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));		
			        String decodedString = in.readLine();
			         
			        System.out.println("Response data is " + decodedString);
			         
			        JSONObject json = new JSONObject(decodedString);
			        userFullname = (String)json.get("fullname");
			        userEmail = (String)json.get("email");
			        int index = userFullname.indexOf(" ");
			        if (index > 0){
			        	userFirstname = userFullname.substring(0,index);
			        	userLastname = userFullname.substring(index+1);
			        	userInstitution = (String)json.get("institution");
			        	userOrganization = (String)json.get("organization");
			        } 
			        json.put("firstname", userFirstname);
			        json.put("lastname", userLastname);
//			        return json; 
		        } 
		        connection.disconnect(); 
		    } catch (Exception e) {
		    	System.out.println(e);
		        return success;
		    } 
		    return success;