首页 > 代码库 > HttpClient

HttpClient

  最近写了些接口,使用到了HttpClient,这里留作复习

  HttpClient有新旧之分org.apache.commons.httpclient.HttpClient和org.apache.http.client.HttpClient

  org.apache.http.client.HttpClient 取代了org.apache.commons.httpclient.HttpClient

  这里就不探讨org.apache.commons.httpclient.HttpClient的用法了。

  首先是get方法

  

public static void get(){        /**         * org.apache.commons.httpclient.HttpClient还是class         * org.apache.http.client.HttpClient已经是interface了         */        CloseableHttpClient httpclient = HttpClients.createDefault();        try {            // 创建httpget.                HttpGet httpget = new HttpGet("http://localhost:8080/test/MyTestServlet?paramter=111");            System.out.println("executing request " + httpget.getURI());            // 执行get请求.                //要是不加httpcore-4.4.4.jar这边编译不过去,有空看看            CloseableHttpResponse response = httpclient.execute(httpget);            // 获取响应实体                HttpEntity entity = response.getEntity();            System.out.println("--------------------------------------");              // 打印响应状态                System.out.println(response.getStatusLine());            if (entity != null) {                  // 打印响应内容长度                    System.out.println("Response content length: " + entity.getContentLength());                  // 打印响应内容                    System.out.println("Response content: " + EntityUtils.toString(entity));              }              System.out.println("------------------------------------");                     } catch (ClientProtocolException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally{            try {                httpclient.close();            } catch (IOException e) {                e.printStackTrace();            }          }    }

  普通的post请求

技术分享
public static void commonPost(){        // 创建默认的httpClient实例.         CloseableHttpClient httpClient = HttpClients.createDefault();        // 创建httppost            HttpPost httppost = new HttpPost("http://localhost:8080/test/MyTestServlet");        // 创建参数队列            List<NameValuePair> formparams = new ArrayList<NameValuePair>();          formparams.add(new BasicNameValuePair("paramter", "hou12312se"));        UrlEncodedFormEntity uefEntity;          try {            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");            httppost.setEntity(uefEntity);              System.out.println("executing request " + httppost.getURI());              CloseableHttpResponse response = httpClient.execute(httppost);            HttpEntity entity = response.getEntity();              if (entity != null) {                  System.out.println("--------------------------------------");                  System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));                  System.out.println("--------------------------------------");              }         } catch (UnsupportedEncodingException e) {            e.printStackTrace();        } catch (ClientProtocolException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                httpClient.close();            } catch (IOException e) {                e.printStackTrace();            }          }            }
View Code

  Content-Type为application/x-www-form-urlencoded的post请求

public static void specialPost(){        CloseableHttpClient httpClient = HttpClients.createDefault();        HttpPost httpPost = new HttpPost("http://localhost:8081/test/doPostTestServlet");        List<NameValuePair> list = new ArrayList<NameValuePair>();        list.add(new BasicNameValuePair("UserNumber", "13173001830"));        list.add(new BasicNameValuePair("corp_id", "qd007"));        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");        try {            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list);            httpPost.setEntity(urlEncodedFormEntity);            CloseableHttpResponse response = httpClient.execute(httpPost);            HttpEntity entity = response.getEntity();             System.out.println(EntityUtils.toString(entity, "utf-8"));        } catch (ClientProtocolException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }

  Content-Type为application/json的post请求

技术分享
public static void specialPost1(){        CloseableHttpClient httpClient = HttpClients.createDefault();        HttpPost httpPost = new HttpPost("http://localhost:8081/test/doPostTestServlet");        JSONObject jsonObject = new JSONObject();        jsonObject.put("name", "vincent");        jsonObject.put("age", "24");//换成int型的有区别吗?        jsonObject.put("height", "182");        System.out.println("jsonObject大小:" + jsonObject.size());        httpPost.setHeader("Content-Type", "application/json");        StringEntity entity = new StringEntity(jsonObject.toString(), "utf-8");        httpPost.setEntity(entity);        try {            CloseableHttpResponse response = httpClient.execute(httpPost);            HttpEntity httpEntity = response.getEntity();            System.out.println(EntityUtils.toString(httpEntity, "utf-8"));        } catch (ClientProtocolException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }
View Code
这里有2种方式

 

HttpClient