HttpClient测试接口的封装(get和post)

1、到官网下载对应的jar包 http://hc.apache.org/downloads.cgi

2、参考网站http://www.cnblogs.com/zhangfei/p/5099036.html

测试用的两个小东西

get和post



import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
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.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class Test {
	
	

	
	
	/** 
	 * post方式提交表单(模拟用户登录请求) 
	 * 调用例子:
	 * String url2 = "http://localhost:8080/XXXX/isExistUser"; 
	 * List formparams2 = new ArrayList();  
	 * formparams2.add(new BasicNameValuePair("username", "13692264659"));  
	 * postForm(url2,formparams2);
	 * 
	 */  
	@org.junit.Test
	public static void postTest(String url,List parameters) {  
	    // 创建默认的httpClient实例.    
	    CloseableHttpClient httpclient = HttpClients.createDefault();  
	    // 创建httppost    
	    HttpPost httppost = new HttpPost(url);  

	    UrlEncodedFormEntity uefEntity;  
	    try {  
	        uefEntity = new UrlEncodedFormEntity(parameters, "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();  
	        }  
	    }  
	}
	
	
    /** 
     * 发送 get请求 
     */  
	@org.junit.Test
    public static void get(String url) {  
        CloseableHttpClient httpclient = HttpClients.createDefault();  
        try {  
            // 创建httpget.    
            HttpGet httpget = new HttpGet(url);  
            System.out.println("executing request " + httpget.getURI());  
            

            // 执行get请求.    
            CloseableHttpResponse response = httpclient.execute(httpget);  
            try {  
                // 获取响应实体    
                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("------------------------------------");  
            } finally {  
                response.close();  
            }  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (ParseException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            // 关闭连接,释放资源    
            try {  
                httpclient.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
	
	
	
	
}







你可能感兴趣的:(JAVA基础)