private HttpURLConnection postQuery ( ) throws HelloSignException { logger . debug ( this . method + ": " + url ) ; HttpURLConnection connection ; try { connection = getProxiedConnection ( url ) ; if ( ! this . method . equals ( "POST" ) ) { connection . setRequestMethod ( method ) ; } } catch ( Exception e ) { throw new HelloSignException ( e ) ; } connection . setDoOutput ( true ) ; // sets POST method connection . setRequestProperty ( "user-agent" , USER_AGENT ) ; connection . setRequestProperty ( "accept-encoding" , DEFAULT_ENCODING ) ; auth . authenticate ( connection , url ) ; StringBuffer sb = new StringBuffer ( ) ; if ( fields != null ) { Iterator < String > keys = fields . keySet ( ) . iterator ( ) ; while ( keys . hasNext ( ) ) { String key = keys . next ( ) ; Serializable val = fields . get ( key ) ; if ( val == null ) { throw new HelloSignException ( "POST value for key '" + key + "' is null" ) ; } String value ; String encodedKey ; try { value = URLEncoder . encode ( val . toString ( ) , DEFAULT_ENCODING ) ; encodedKey = URLEncoder . encode ( key , DEFAULT_ENCODING ) ; } catch ( UnsupportedEncodingException e ) { throw new HelloSignException ( e ) ; } logger . debug ( "\t" + key + " = " + val . toString ( ) ) ; sb . append ( encodedKey + "=" + value ) ; if ( keys . hasNext ( ) ) { sb . append ( "&" ) ; } } } try { OutputStream output = connection . getOutputStream ( ) ; try { output . write ( sb . toString ( ) . getBytes ( DEFAULT_ENCODING ) ) ; } finally { try { output . close ( ) ; } catch ( IOException logOrIgnore ) { } } } catch ( Exception ex ) { ex . printStackTrace ( ) ; throw new HelloSignException ( ex . getMessage ( ) ) ; } return connection ; }
Helper method to make an HTTP POST request .