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
- httpclient传参调用服务端方法
- HttpClient跨项目(服务端)调用方法例子
- HttpClient 调用方法
- CXF生成webservice服务端,httpclient客户端调用webservice服务端
- 服务间调用方法:HttpClient
- httpClient Post传参方法
- AJAX下客户端调用服务端页面方法
- AJAX下客户端调用服务端页面方法
- AJAXRPC: javascript直接调用服务端方法
- AIDL基本使用1----调用服务端方法
- httpclient客户端与服务端
- httpclient 调用
- restful接口客户端和服务端开发,HttpURLConnection,HttpClient,post ,get方式调用
- Ajax客户端异步调用服务端方法(js调用cs文件)
- asp.net中js调用服务端方法,服务端调用JS,实现无刷新的倒计时
- httpclient 4.0 与服务端交互
- httpclient 4.0 与服务端交互
- HttpClient请求,服务端收到乱码
- 深入理解C++
- android 自定义打开文件的类型
- 使用百度地图API生成麻点图
- Light oj 1071 - Baker Vai(记忆化)
- 一起学 mybatis 基础教程
- httpclient传参调用服务端方法
- android studio 修改编码
- 守护线程(Daemon Thread)
- 找到带环单链表的连接点
- git submodule 实际使用
- 从相机拍摄和从相册读取图片,以及对图片的压缩的完全解决方案
- jQuery bootstrap和highcharts的入门
- c#的委托和事件的实例学习
- 双向BFS解八数码问题: POJ 1077