httpclient测试例子

package com.fly.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import cn.ixdf.model.Department;
import cn.ixdf.util.Constants;
import cn.ixdf.util.JsonJacksonUtil;
import cn.ixdf.util.QYWeiXinAPI;

import com.fasterxml.jackson.databind.ObjectMapper;

public class Test {

	public static void main(String[] args) throws Exception{
		httpPostTest();
	}
	
	//httpclient test -- post
	public static void httpPostTest(){
		//实例化httpclient
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try{
			//创建httppost
			HttpPost httpPost = new HttpPost("http://www.xxx.com/login");
			//http 表单 | 参数队列
			List nvps = new ArrayList();
			nvps.add(new BasicNameValuePair("username","name"));
			nvps.add(new BasicNameValuePair("password","password"));
			// post参数,无需表单用法如下:
			// post.setEntity(new StringEntity(str1, encoding));
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
			//参数软如post方法内
			httpPost.setEntity(entity);
			//执行请求
			CloseableHttpResponse response = httpclient.execute(httpPost);
			//服务器响应
			HttpEntity httpEntity = response.getEntity();
			if(httpEntity != null){
				//
				String content = EntityUtils.toString(httpEntity);
				System.out.println(content);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally {  
            // 关闭连接,释放资源    
            try {  
                httpclient.close();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
		
		
	}
	
	//httpclient test
	public static void httpGetTest(){
		HttpClient httpClient = new DefaultHttpClient(); 
		DefaultHttpClient httpclient = new DefaultHttpClient();//已不推荐
		
		
		try {
			//创建httpget
			//HttpGet httpget = new HttpGet("http://i.xdf.cn/home");
			HttpGet httpget = new HttpGet(QYWeiXinAPI.getTokenUrl(Constants.sCorpID,Constants.corpsecret));
			//执行get请求
			HttpResponse httpResponse = httpClient.execute(httpget);
			//获得相应实体
			HttpEntity entity = httpResponse.getEntity();
			System.out.println("==============================");
			System.out.println(httpResponse.getStatusLine());
			if(entity!=null){
				//打印响应长度
				System.out.println("contentLength:"+entity.getContentLength());
				String content = EntityUtils.toString(entity);
				System.out.println(content);
			}
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			 //关闭连接,释放资源  
		    httpClient.getConnectionManager().shutdown();  
		}
		
	}
	
	//jackson test
	public static void jsonTest() throws Exception{
		Department d = new Department();
		d.setName("bumen");
		d.setOrder(2);
		d.setParentid(1);
		ObjectMapper mapper = new ObjectMapper();
		
		System.out.println(mapper.writeValueAsString(d));
		Map map = mapper.readValue(mapper.writeValueAsString(d), Map.class);
		System.out.println(map);
		JsonJacksonUtil js = JsonJacksonUtil.getInstance();
		System.out.println(js.obj2jsonNotNULL(d));
	}
}


 
 

你可能感兴趣的:(转载自用)