private HttpURLConnection getConnectionOnce ( String uri , String postJson , String token ) throws IOException { URL url = new URL ( uri ) ; HttpURLConnection conn = ( HttpURLConnection ) url . openConnection ( ) ; if ( timeout > 0 ) { conn . setReadTimeout ( timeout ) ; conn . setConnectTimeout ( timeout ) ; } if ( postJson != null && postJson . equals ( "{}" ) ) postJson = null ; if ( postJson != null ) { conn . setDoOutput ( true ) ; // Triggers POST. } conn . setRequestProperty ( "Content-Type" , "application/json; charset=" + UTF8 . name ( ) ) ; conn . setRequestProperty ( "User-Agent" , "WePay Java SDK" ) ; if ( token != null ) { conn . setRequestProperty ( "Authorization" , "Bearer " + token ) ; } if ( postJson != null ) { OutputStreamWriter writer = new OutputStreamWriter ( conn . getOutputStream ( ) , UTF8 ) ; writer . write ( postJson ) ; writer . close ( ) ; } return conn ; }
Sets up the headers and writes the post data .