Java - Cookies - Login, Get Person, Logout
Java - Login for cookie, send cookie to get/person call, logout
public static void main(String[] args) throws Exception { //don't forget to URLEncode your arguments String address = "http://infinite.ikanow.com/api/auth/login/sterling_archer@ikanow.com/WZRHGrsBESr8wYFZ9sx0tPURuZgG2lmzyvWpwXPKz8U%3D"; String loginresult = sendRequest(address); //Our data objects can be used by importing infinit.e.data_model.jar and gson.jar ResponsePojo response = ResponsePojo.fromApi(loginresult, ResponsePojo.class); if ( response.getResponse().isSuccess()) { //send next request String personAddress = "http://infinite.ikanow.com/api/social/person/get"; String personresult = sendRequest(personAddress); //We need to convert the result object into a response with a person object in it response = ResponsePojo.fromApi(personresult, ResponsePojo.class, PersonPojo.class, new PersonPojoApiMap()); if ( response.getResponse().isSuccess() ) { PersonPojo personResult = (PersonPojo)response.getData(); System.out.println(personResult.getEmail()); } } else { System.out.println("error logging in: " + response.getResponse().getMessage()); } //logout when we are done, this will deactivate our cookie sendRequest("http://infinite.ikanow.com/api/auth/logout"); } private static String cookie = null; public static String sendRequest(String urlAddress ) throws Exception { URL url = new URL(urlAddress); URLConnection urlConnection = url.openConnection(); if ( cookie != null ) //add cookie to request if we have one urlConnection.setRequestProperty("Cookie", cookie); ((HttpURLConnection)urlConnection).setRequestMethod("GET"); //read back result BufferedReader inStream = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); StringBuilder strBuilder = new StringBuilder(); String buffer; while ( (buffer = inStream.readLine()) != null ) { strBuilder.append(buffer); } inStream.close(); //save cookie if cookie is null if ( cookie == null ) { String headername; for ( int i = 1; (headername = urlConnection.getHeaderFieldKey(i)) != null; i++ ) { if ( headername.equals("Set-Cookie") ) { cookie = urlConnection.getHeaderField(i); break; } } } return strBuilder.toString(); }