public static String httpPost ( final URL url , final String submission , int timeout , final Map < String , String > requestProps ) throws IOException , ServiceWaiter . TimeoutException { final ServiceWaiter < String > waiter = new ServiceWaiter < String > ( ( timeout < 0 ) ? ServiceWaiter . NO_TIMEOUT : timeout ) ; Thread tt = new Thread ( ) { @ Override public void run ( ) { try { HttpURLConnection conn = ( HttpURLConnection ) url . openConnection ( ) ; conn . setDoInput ( true ) ; conn . setDoOutput ( true ) ; conn . setUseCaches ( false ) ; for ( Map . Entry < String , String > entry : requestProps . entrySet ( ) ) { conn . setRequestProperty ( entry . getKey ( ) , entry . getValue ( ) ) ; } DataOutputStream out = new DataOutputStream ( conn . getOutputStream ( ) ) ; out . writeBytes ( submission ) ; out . flush ( ) ; out . close ( ) ; BufferedReader reader = new BufferedReader ( new InputStreamReader ( conn . getInputStream ( ) ) ) ; StringBuilder buf = new StringBuilder ( ) ; for ( String s ; null != ( s = reader . readLine ( ) ) ; ) { buf . append ( s ) ; } reader . close ( ) ; waiter . postSuccess ( buf . toString ( ) ) ; // yay } catch ( IOException e ) { waiter . postFailure ( e ) ; // boo } } } ; tt . start ( ) ; if ( waiter . waitForResponse ( ) ) { return waiter . getArgument ( ) ; } else { throw ( IOException ) waiter . getError ( ) ; } }
Return the results of a form post . Note that the http request takes place on another thread but this thread blocks until the results are returned or it times out .