1. 程式人生 > >httpclient封裝別人介面

httpclient封裝別人介面

近期收到任務需要封裝下別的開發組寫的介面以實現許可權控制,於是想起了httpclient,直接上程式碼吧

    @Test
    public void post() {  
		// 建立預設的httpClient例項.    
        CloseableHttpClient httpclient = HttpClients.createDefault();
//        CookieStore cookieStore = new BasicCookieStore();
//        cookieStore.addCookie();
//		CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore ).build();
        // 建立httppost    
//       HttpPost httppost = new HttpPost("http://localhost:8080/api/v2/bi/report/getPersonas"); 
       HttpPost httppost = new HttpPost("http://10.0.4.62:8000/api/v1/car_income"); 
//        HttpPost httppost = new HttpPost("http://localhost:8080/api/v1/user/login"); 
        try {  
        	JSONObject jsonParam = new JSONObject();
//        	jsonParam.put("staffEname", "zeezhang");
//        	jsonParam.put("oaPwd", "7c4a8d09ca3762af61e59520943dc26494f8941b");
//        	jsonParam.put("statDim", "gender");
//        	jsonParam.put("groupby","all");
//        	jsonParam.put("filters","{}");
//        	jsonParam.put("start_date","2017-10-01");
//        	jsonParam.put("end_date","2017-10-17");
//        	jsonParam.put("dimensions","['company_code','last_expired_time']");
        	StringEntity entity = new StringEntity("{\"groupby\":\"all\",\"start_date\":\"2017-01-01\",\"end_date\":\"2017-10-19\",\"dimensions\":[],\"filters\":{}}", "utf-8");  
        	entity.setContentEncoding("UTF-8");    
            entity.setContentType("application/json");
//            httppost.addHeader("Cookie", "JSESSIONID=0d9fc7de-19ab-4a8e-85b4-fe3b5526a4c4");
            httppost.addHeader("Authorization", "Basic ZG1hcGlfdGVzdDpkbUAyMDE3MDkwNQ==");
            httppost.setEntity(entity);  
            System.out.println("executing request:==== " + httppost.getURI());  
            CloseableHttpResponse response = httpclient.execute(httppost); 
//            String value = response.getFirstHeader("Set-Cookie").getValue();
//            System.out.println(value+"====================");
            try {  
                HttpEntity entity1 = response.getEntity();  
                if (entity1 != null) {  
                    System.out.println("--------------------------------------"); 
                    System.out.println(entity1);
                    System.out.println("--------------------------------------"); 
                    System.out.println("Response content: " + EntityUtils.toString(entity1, "UTF-8"));  
                    System.out.println("--------------------------------------");  
                }  
            } finally {  
                response.close();  
            }  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (UnsupportedEncodingException e1) {  
            e1.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            // 關閉連線,釋放資源    
            try {  
                httpclient.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
上邊是json字串格式的請求引數針對post請求

/** 
     * post方式提交表單(模擬使用者登入請求) 
     */  
    @Test
    public void postForm() {  
        // 建立預設的httpClient例項.    
        CloseableHttpClient httpclient = HttpClients.createDefault();  
        // 建立httppost    
        HttpPost httppost = new HttpPost("http://localhost:8080/api/v1/user/login");  
        // 建立引數佇列    
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();  
        formparams.add(new BasicNameValuePair("oaPwd", "7c4a8d09ca3762af61e59520943dc26494f8941b"));  
        formparams.add(new BasicNameValuePair("staffEname", "aaa"));
        UrlEncodedFormEntity uefEntity;  
        try {  
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");  
            httppost.setEntity(uefEntity);  
            System.out.println("executing request " + httppost.getURI());  
            CloseableHttpResponse response = httpclient.execute(httppost);  
            try {  
                HttpEntity entity = response.getEntity();  
                if (entity != null) {  
                    System.out.println("--------------------------------------");  
                    System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));  
                    System.out.println("--------------------------------------");  
                }  
            } finally {  
                response.close();  
            }  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (UnsupportedEncodingException e1) {  
            e1.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            // 關閉連線,釋放資源    
            try {  
                httpclient.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
上邊是針對表單提交的post方式,get方式的就不再多說了