httpclient使用実践

4228 ワード

httpclientは何ですかここでは詳しく説明しません(一番下のURLを参照できます).直接コードをつける;インタフェースはjson形式のデータを返します.
package com.httpclient.test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class ThttpClient {

	public static void main(String[] args) {
		String baseURL = "http://192.168.17.75:8080";
		String token = "66b5c33d-4d6b-49cd-bb54-303b6d0780b5";
		
		CloseableHttpClient httpclient = HttpClients.createDefault();  
		createFaciGraphSession(httpclient, baseURL, token);
		httpclient = HttpClients.createDefault();  
		String result = (String) getBlendingPlanList(httpclient, baseURL, token, 1L);
		System.out.println(result);
	}
	
	
	private static void createFaciGraphSession(CloseableHttpClient httpclient, String baseURL, String token) {
		 //      httpClient  .    
//	     CloseableHttpClient httpclient = HttpClients.createDefault();  
	     HttpGet httpget = new HttpGet(baseURL + "/system/sessionManage/createSession?_token="+token);  
	     //   get  .    
	     
	     try {  
	    	 CloseableHttpResponse res = httpclient.execute(httpget);  
//	         CloseableHttpResponse res = httpclient.execute(httppost);  
	         try {  
	             HttpEntity entity = res.getEntity();  
	             if (entity != null) {  
	                 System.out.println("----------------------------");  
	                 System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));  
	                 System.out.println("--------------------------------");  
	             }  
	         } finally {  
	         	res.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();  
	         }  
	     }  
	}
	
	public static Object getBlendingPlanList(CloseableHttpClient httpclient, String baseURL, String token, Long planId){
		String result = null;
//		HttpGet httpget = new HttpGet(baseURL + "/third/planInfo/getBlendingPlanIds?_token="+token+"&planId=" + planId);  
		HttpGet httpget = new HttpGet(baseURL + "/third/planInfo/getBlendingPlanList?_token="+token+"&planId=" + planId);  
	    //   get  .    						 
	    try {  
	    	 CloseableHttpResponse res = httpclient.execute(httpget);  
//	         CloseableHttpResponse res = httpclient.execute(httppost);  
	         try {  
	             HttpEntity entity = res.getEntity();  
	             if (entity != null) {  
	                 result = EntityUtils.toString(entity, "UTF-8");
	             }  
	         } finally {  
	         	res.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();  
	         }  
	     }  
		return result;
	}
}

参照先:http://blog.csdn.net/wangpeng047/article/details/19624529