httpClient访问http的rest接口
来源:互联网 发布:电脑桌面设置软件 编辑:程序博客网 时间:2024/04/29 11:33
访问http的rest接口
1、get方式访问http的rest接口
/** * 以get的方式访问http的rest接口 * @param url * @return * @throws Exception */ public static String httpGet(String url) throws Exception { BufferedReader in = null; String content = null; try { // 定义HttpClient HttpClient client = new DefaultHttpClient(); // 实例化HTTP方法 HttpGet request = new HttpGet(); request.setURI(new URI(url)); HttpResponse response = client.execute(request); in = new BufferedReader(new InputStreamReader(response.getEntity() .getContent())); StringBuffer sb = new StringBuffer(""); String line = ""; String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line + NL); } in.close(); content = sb.toString(); } finally { if (in != null) { try { in.close();// 最后要关闭BufferedReader } catch (Exception e) { e.printStackTrace(); } } return content; } }
测试get方式的链接:http://localhost:7080/api/getUser?username=hello
public static void main(String[] args) throws Exception { String api = "http://localhost:7080/api"; String content = HttpClientUtil.httpGet(api + "/getUser?username=hello" ); System.out.println( content); }
2、post方式(json和表单)访问http的rest接口
1)json的形式post数据
Headers为:content-type:application/json
Body:{key1:value1, key2:value2, key3:value3}
/** * 以json的形式提交数据 * @param url * @param obj * @return */ public static String httpPost(String url, JSONObject obj) { String _encoding = "UTF-8"; StringBuffer sb = new StringBuffer(); BufferedReader in = null; HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); try { StringEntity entity = new StringEntity(obj.toString(),"utf-8");//解决中文乱码问题 entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); post.setEntity(entity); System.out.println("getRequestLine:" + post.getRequestLine()); // 执行请求 HttpResponse response = client.execute(post); in = new BufferedReader(new InputStreamReader(response.getEntity() .getContent(), _encoding)); String line = ""; String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line + NL); } } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e1) { } } // 关闭连接. try { client.getConnectionManager().shutdown(); } catch (Exception e1) { } } String str = ""; try { str = sb.toString(); } catch (Exception e) { } return str; }
测试post json数据的链接 : http://localhost:7080/api/token
将数据存于JSONObject中
public static void main(String[] args) throws Exception { String api = "http://localhost:7080/api"; String str = "{accessToken:'aa',expiresIn:7200,createTime:1234566,flag:1,appId:111111111}"; JSONObject obj = JSONObject.fromObject(str); String content = HttpClientUtil.httpPost1(api + "/token" , obj); System.out.println( content); }
2)表单的形式post数据
Headers为:content-type:application/x-www-form-urlencoded; charset=UTF-8
Body:key=value
注意:post方式默认以表单的形式提交数据
/** * 以表单的形式提交数据 * @param url * @param params * @return */ public static String httpPost(String url, List<NameValuePair> params) { String _encoding = "UTF-8"; StringBuffer sb = new StringBuffer(); BufferedReader in = null; HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); try { //设置字符集 post.setEntity(new UrlEncodedFormEntity(params,_encoding)); // 执行请求 HttpResponse response = client.execute(post); in = new BufferedReader(new InputStreamReader(response.getEntity() .getContent(), _encoding)); String line = ""; String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line + NL); } } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e1) { } } // 关闭连接. try { client.getConnectionManager().shutdown(); } catch (Exception e1) { } } String str = ""; try { str = sb.toString(); } catch (Exception e) { } return str; }
测试post表单数据的链接 : http://localhost:7080/api/getToken?appId=111111111
public static void main(String[] args) throws Exception { String api = "http://localhost:7080/api";//测试的url //将需要使用的表单数据存于List<NameValuePair> List<NameValuePair> list = new ArrayList<NameValuePair>(); list.add(new BasicNameValuePair("appId", "111111111")); String content = httpPost(api + "/getToken" , list); System.out.println( content); }
1 0
- httpClient访问http的rest接口
- 使用Apache Httpclient访问Spring rest接口下载文件
- httpclient 3.0 访问rest
- http跨平台访问接口的post,get,及文件上传方式工具类(httpClient)
- 访问rest接口
- Angular 4.3 HttpClient (Angular访问 REST Web 服务) 一、Http 请求示例(Get)
- Angular 4.3 HttpClient (Angular访问 REST Web 服务) 二、Http POST
- httpclient访问接口
- httpclient http接口调用
- Http请求接口HttpClient
- 关于HttpClient(调用openstack rest接口失败)
- 通过curl访问rest接口
- Angular 4.3 HttpClient (Angular访问 REST Web 服务) 二、Http 请求结果类型转换与错误处理
- httpclient提交表单 访问接口
- Android使用httpclient访问http
- 架构师之路-如何构建rest接口的安全性访问(dubbox+oatuh2+rest)
- APP server 使用httpclient调用第三方环信的server rest接口获取token
- 基于HttpClient的Http接口测试用例
- 3D数学基础
- undefined reference to `clock_gettime' 链接错误问题解决
- 转载--《怎样制作一款优秀的塔防游戏》
- 程序员最该看的30本书---------------------------国外编辑推荐
- C#中的委托和事件
- httpClient访问http的rest接口
- UGUI(五)动画系统
- 使用list.all(list1),如何在页面使用c:forEach遍历?
- 数据持久化方案
- lxc源码-- 序
- 在美国折腾护照的一星期
- maven中央仓库地址收集
- 关于VisualSVN和Tortoise的使用
- 使用NPAPI开发Fireforx/Chrome插件