Code example for HttpURLConnection

Methods: getOutputStream, getResponseCode, getResponseMessage, setChunkedStreamingMode, setDoOutput, setRequestProperty

0
     * @throws IOException if there was an error while connecting to the server. 
     * */ 
    private void initiateUrlConnection( 
            final HttpURLConnection urlConnection,
            final String httpParameters) throws IOException {
        urlConnection.setDoOutput(true);
        urlConnection.setChunkedStreamingMode(0);
 
        if (mSessionCookie != null && !(mSessionCookie.length() == 0)) {
            urlConnection.setRequestProperty("Cookie", mSessionCookie);
        } 
 
        DataOutputStream out =
                new DataOutputStream(urlConnection.getOutputStream());
        out.writeBytes(httpParameters);
        out.flush();
        out.close();
        Log.d(LOG_SERVER, "urlConnection output stream successfully created");
 
        Log.d(
                LOG_SERVER,
                "Received response with status code " 
                        + urlConnection.getResponseCode());
        Log.d(
                LOG_SERVER,
                "Received response with message " 
                        + urlConnection.getResponseMessage());
    } 
 
    /** 
     * treat HttpResponse from an urlConnection. 
     * @param urlConnection URL connection where to read the response.