httpclient-4.3과 4.1을 기반으로 한 작은 예 작성

2044 단어 httpclient
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!");  
        }
    }  

  
 
 
 
 
 

좋은 웹페이지 즐겨찾기