question: private HttpURLConnection doHTTPPostRequest ( String location , String clientSessionId , Map < String , String > metadata , String postBody ) throws CommunicationException { OutputStreamWriter writer = null ; try { URL url = new URL ( location ) ; HttpURLConnection connection = ( HttpURLConnection ) url . openConnection ( ) ; connection . setRequestMethod ( "POST" ) ; connection . addRequestProperty ( "Content-Type" , "application/json" ) ; if ( clientSessionId != null ) { connection . addRequestProperty ( HTTP_HEADER_SESSION_ID , "GCS v1Client:" + clientSessionId ) ; } if ( metadata != null ) { connection . addRequestProperty ( HTTP_HEADER_METADATA , GcUtil . getBase64EncodedMetadata ( metadata ) ) ; } if ( Constants . ENABLE_REQUEST_LOGGING ) { logRequest ( connection , postBody ) ; } connection . setDoOutput ( true ) ; writer = new OutputStreamWriter ( connection . getOutputStream ( ) , "UTF-8" ) ; writer . write ( postBody ) ; writer . flush ( ) ; if ( connection . getResponseCode ( ) != 200 ) { throw new CommunicationException ( "No status 200 received, status is :" + connection . getResponseCode ( ) ) ; } return connection ; } catch ( MalformedURLException e ) { Log . e ( TAG , "doHTTPPostRequest, Unable to parse url " + location ) ; throw new CommunicationException ( "Unable to parse url " + location ) ; } catch ( IOException e ) { Log . e ( TAG , "doHTTPPostRequest, IOException while opening connection " + e . getMessage ( ) ) ; throw new CommunicationException ( "IOException while opening connection " + e . getMessage ( ) , e ) ; } finally { if ( writer != null ) { try { writer . close ( ) ; } catch ( IOException e ) { Log . i ( TAG , "doHTTPPostRequest, IOException while closing connection " + e . getMessage ( ) ) ; } } } }
target:  Does a POST request with HttpClient