httpclient工具使用(org.apache.httpcomponents.httpclient)
The introduction of dependence
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.5</version> </dependency>
get request
public static void main(String[] args) throws Exception { // Create an object httpclient CloseableHttpClient httpClient = HttpClients.createDefault(); // Create Http get request HttpGet httpGet = new HttpGet("http://www.xxx.com/rest/content?categoryId=4&page=1&rows=20"); // receive the return value CloseableHttpResponse response = null; try { // request execution response = httpClient.execute(httpGet); if(response.getStatusLine().getStatusCode()==200){ String content = EntityUtils.toString(response.getEntity(), "utf-8"); System.out.println("--------->" + content); } }finally{ if(response!=null){ response.close(); } httpClient.close(); }
get request parameters
public static void main(String[] args) throws Exception{ // Create an object httpClient CloseableHttpClient httpClient = HttpClients.createDefault(); // http get parameter definitions URI uri = new URIBuilder("http://www.xxxx.com/rest/content").setParameter("categoryId", "4") .setParameter("page", "1").setParameter("rows", "30").build(); System.out.println(uri); // Create a http get requests HttpGet httpGet = new HttpGet(uri); // define the request to accept the return of CloseableHttpResponse response = null; try { // get execution request response = httpClient.execute(httpGet); if(response.getStatusLine().getStatusCode()==200){ String content = EntityUtils.toString(response.getEntity(), "utf-8"); System.out.println(content); } }finally{ if(response!=null){ response.close(); } httpClient.close(); } }
http post request
public static void main(String[] args) throws Exception { // Create an object Httpclient CloseableHttpClient httpclient = HttpClients.createDefault(); // Create a http POST request HttpPost httpPost = new HttpPost("http://www.oschina.net/"); // disguised browser httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36"); CloseableHttpResponse response = null; try { // execution request response = httpclient.execute(httpPost); // determines whether the state is returned 200 if (response.getStatusLine().getStatusCode() == 200) { String content = EntityUtils.toString(response.getEntity(), "UTF-8"); System.out.println(content); } } finally { if (response != null) { response.close(); } httpclient.close(); } }
http post request parameters
public static void main(String[] args) throws Exception{ // Create httpclient CloseableHttpClient httpClient = HttpClients.createDefault(); // Create http post HttpPost httpPost = new HttpPost("http://www.oschina.net/search"); // Simulate browser setting head httpPost.setHeader( "User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36"); // setting request List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("scope", "project")); params.add(new BasicNameValuePair("q", "java")); params.add(new BasicNameValuePair("fromerr", "7nXH76r7")); // Build a form UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params); // request into the expression httpost httpPost.setEntity(formEntity); // return type CloseableHttpResponse response = null; try { response = httpClient.execute(httpPost); String content = EntityUtils.toString(response.getEntity(), "utf-8"); System.out.println(content); }finally{ if(response!=null){ response.close(); } httpClient.close(); } }