httpclient传参调用服务端方法

来源:互联网 发布:见什么知什么 编辑:程序博客网 时间:2024/05/22 03:26
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.5public 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 blocke1.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 blocke.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 blocke.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 blocke.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 blocke1.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 blocke.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 0
原创粉丝点击