Java - httpclient

get方式


    CloseableHttpClient client = HttpClients.createDefault();
        
        URIBuilder uriBuilder = new URIBuilder("http://www.baidu.com/s");
        
        uriBuilder.addParameter("wd", "马士兵");
        
        
    
        
        HttpGet get = new HttpGet(uriBuilder.build());
        
        
        CloseableHttpResponse response = client.execute(get);
        
        HttpEntity httpEntity = response.getEntity();
        
        String string = EntityUtils.toString(httpEntity, "utf-8");
        
        System.out.println(string);
        
    }

post方式


CloseableHttpClient client = HttpClients.createDefault();
        
        
        HttpPost httpPost = new HttpPost("http://localhost/login");
        
        List list = new ArrayList();
        
        
        list.add(new BasicNameValuePair("username", "admin"));
        
        list.add(new BasicNameValuePair("password", "123451"));
        
        StringEntity strEntity = new UrlEncodedFormEntity(list,"utf-8");
        
        httpPost.setEntity(strEntity);
        
        CloseableHttpResponse response = client.execute(httpPost);
        
        String str = EntityUtils.toString(response.getEntity(),"utf-8");
        
        System.out.println(str);
        
        response.close();
        
        client.close();
        
        
        
        

你可能感兴趣的:(Java - httpclient)