欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-13 08:11:03
...

基于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