Code example for DefaultHttpClient

Methods: execute, getParams

0
 
    private String sendToServer(String request) throws IOException {
        String result = null;
        maybeCreateHttpClient(); 
        HttpGet get = new HttpGet(request);
        HttpResponse resp = httpClient.execute( get );
 
        // Execute the GET transaction and read the results 
        int status = resp.getStatusLine().getStatusCode();
        if( status != HttpStatus.SC_OK )
                throw new IOException( "HTTP status: "+Integer.toString( status ) );
        DataInputStream is = new DataInputStream( resp.getEntity().getContent() );
        result = is.readLine();
        return result;
 
    } 
 
        private void maybeCreateHttpClient() { 
            if ( httpClient == null) {
                httpClient = new DefaultHttpClient();
                HttpParams params = httpClient.getParams();
                HttpConnectionParams.setConnectionTimeout(params, REGISTRATION_TIMEOUT);
                HttpConnectionParams.setSoTimeout(params, REGISTRATION_TIMEOUT);
                ConnManagerParams.setTimeout(params, REGISTRATION_TIMEOUT);
            } 
    }