基于httpclient-4.3 和 4.1的编写小例子

基于httpclient-4.3 和 4.1的编写小例子

 

 

/**
     * 基于httpclient-4.3.3.jar httpcore-4.3.2.jar的编写小例子
     * @param url
     * @throws Exception
     */
    private static void newgo(String url) throws Exception {  
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse httpResponse = client.execute(httpGet);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {  
            System.err.println("Method failed: " + statusCode);  
        }  else {
            HttpEntity entity = httpResponse.getEntity();
            System.out.println(EntityUtils.toString(entity));
            System.out.println("invoke success!");  
        }
    } 

 

 

 

 

/**
     * 基于httpclient-4.1.jar httpcore-4.1.jar
     * @param url
     * @throws Exception
     */
    private static void go(String url) throws Exception {  
        HttpClient client = new DefaultHttpClient(); 
        client.getParams().setIntParameter("http.socket.timeout", 10000);
        client.getParams().setIntParameter("http.connection.timeout", 5000);
        
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = client.execute(httpGet);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {  
            System.err.println("Method failed: " + statusCode);  
        }  else {
            HttpEntity entity = httpResponse.getEntity();
            System.out.println(EntityUtils.toString(entity));
            System.out.println("invoke success!");  
        }
    }  

  

 

 

 

 

 

 

你可能感兴趣的:(httpclient)