博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用HttpGet 发送 json格式的参数
阅读量:2396 次
发布时间:2019-05-10

本文共 1595 字,大约阅读时间需要 5 分钟。

由于工作需要使用java实现http客户端发送get请求,且参数值为json格式,几经周折得以实现,现在记录分享如下:

hashMap 参数实例:

Map
indata = new HashMap(); indata.put("berthstatus", 1); indata.put("berthcode", "22333"); indata.put("parkcode", "6688"); Map
param = new HashMap(); param.put("indata",indata);
具体实现函数:

protected void requestService(Map
reqmap) throws Exception { JSONObject obj = new JSONObject(reqmap); String r = URLEncoder.encode(obj.toString(), "UTF-8"); String reqUrl = "http://192.168.17.35:8888/getParkInfo?jsonString=" + r; System.out.println("请求参数:" + reqUrl); CloseableHttpResponse response = null; CloseableHttpClient client = null; String res = null; HttpGet httpGet = new HttpGet(reqUrl); System.out.println("executing request" + httpGet.getRequestLine()); try { client = HttpClients.createDefault(); response = client.execute(httpGet); if (response.getStatusLine().getStatusCode() == 200) { String result = EntityUtils.toString(response.getEntity()); System.out.println("executing result---连接正常" + result); } else { System.out.println("executing result---服务器连接异常"); } } catch (Exception e) { System.out.println("Exception================" + e.toString()); } finally { if (response != null) { response.close(); } if (client != null) { client.close(); } } }

转载地址:http://ocfob.baihongyu.com/

你可能感兴趣的文章
简单分析shared pool(一)
查看>>
在oracle实践学习位运算 第一篇
查看>>
通过sql语句分析足彩
查看>>
java中的序列化
查看>>
使用ash分析ORA-01652问题
查看>>
生产环境sql语句调优实战第七篇
查看>>
一个oracle查询引起的bug
查看>>
通过shell来比较oracle和java中的字符串使用
查看>>
一条简单的sql语句导致的系统问题
查看>>
关于纠结的recycle pool的设置
查看>>
清华梦的粉碎读后感--论理想主义者王垠
查看>>
关于Oracle的技术问答
查看>>
增量数据丢失的原因分析(三)
查看>>
生活中的优化和向往(r11笔记第72天)
查看>>
最近的几个技术问题总结和答疑(七)
查看>>
每一次退缩之后的努力,都是极大的进步
查看>>
使用sysbench压力测试MySQL(一)(r11笔记第3天)
查看>>
MySQL 5.6, 5.7并行复制测试(r12笔记第9天)
查看>>
容灾切换中的数据库宕机问题简单分析(一)
查看>>
几年前的一次答疑解惑
查看>>