httpclient传参调用服务端方法

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
//4.5
public class HttpUtil {
	public static JSONObject get(String url,List<NameValuePair> params){
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String str=null;
		HttpGet httpget = new HttpGet(url);
		CloseableHttpResponse response=null;
		try {
			if(params!=null&¶ms.size()>0){
				str = EntityUtils.toString(new UrlEncodedFormEntity(params,"utf-8"));
				httpget.setURI(new URI(httpget.getURI().toString() + "?" + str));
				response = httpclient.execute(httpget);
			}else{
				httpget.setURI(new URI(httpget.getURI().toString()));
				response = httpclient.execute(httpget);
			}
			
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		StringBuilder sb = new StringBuilder();
		String jsonstr=null;
		InputStream instream=null;
		try {
		    HttpEntity entity = response.getEntity();
		    if (entity != null) {
		        try {
					instream = entity.getContent();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        	BufferedReader reader = new BufferedReader(new InputStreamReader(instream));   
	            String line = null;
		            try {
						while ((line = reader.readLine()) != null) {   
						    sb.append(line);
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            jsonstr=sb.toString();
	    	}
		}finally {
		    try {
		    	if(null!=instream){
		    		instream.close();
		    	}
		    	if(null!=response){
		    		response.close();
		    	}
		    	if(null!=httpclient){
		    		httpclient.close();
		    	}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(jsonstr!=null){
			return JSON.parseObject(jsonstr);
		}else{
			return null;
		}
	}
	
	public static JSONObject post(String url,List<NameValuePair> params){
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String str=null;
		HttpPost httppost = new HttpPost(url);
		if(params!=null&¶ms.size()>0){
			UrlEncodedFormEntity formentity = new UrlEncodedFormEntity(params, Consts.UTF_8);
			httppost.setEntity(formentity);
		}
		CloseableHttpResponse response=null;
		try {
			response = httpclient.execute(httppost);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		StringBuilder sb = new StringBuilder();
		String jsonstr=null;
		InputStream instream =null;
		try {
		    HttpEntity entity = response.getEntity();
		    if (entity != null) {
		        instream = entity.getContent();
	        	BufferedReader reader = new BufferedReader(new InputStreamReader(instream));   
	            String line = null;
	            while ((line = reader.readLine()) != null) {   
	                sb.append(line);
	            }
	            jsonstr=sb.toString();
		    }
		}catch(Exception e){
			e.printStackTrace();
		}finally {
		    try {
		    	if(null!=instream){
		    		instream.close();
		    	}
		    	if(null!=response){
		    		response.close();
		    	}
		    	if(null!=httpclient){
		    		httpclient.close();
		    	}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(jsonstr!=null){
			return JSON.parseObject(jsonstr);
		}else{
			return null;
		}
	}
	public static void main(String[] args) {
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("truename", "王"));
		nvps.add(new BasicNameValuePair("pageSize", "1"));
		nvps.add(new BasicNameValuePair("pageNum", "1"));
		System.out.println(post("http://127.0.0.1:10000/service/admin/user/list", nvps));
		System.out.println(get("http://127.0.0.1:10000/service/admin/user/list", nvps));
	}
}	

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值