java构造http请求
来源:互联网 发布:微妙体育淘宝是真的吗 编辑:程序博客网 时间:2024/06/11 18:04
【在java后台调用接口,发送http请求获取数据–GET】
public static void initmap(){ //发送http请求获取用于初始化的数据 BufferedReader in = null; String result=""; try { System.out.println("正在获取初始化所需数据---"); URL url=new URL("http://localhost:1188/border/website/indexrecord"); // 打开和URL之间的连接 URLConnection connection = url.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 connection.connect(); // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } List<Website> websiteList= ResultContent.ResultStringtoList(result); System.out.println("数据获取成功-----正在初始化----"); for(int i=0;i<websiteList.size();i++){ map.put(websiteList.get(i).getKeynum(),websiteList.get(i).getCounts()); } System.out.println(map); System.out.println("计数线程初始化完毕"); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { System.out.println("请求初始化所需数据失败++"); e.printStackTrace(); }finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } }
【请求数据包含在url中】
public static void TimerUpdatemysql(Long counts,Long keynum){ //发送http请求获取用于初始化的数据 try { System.out.println("正在更新数据库---"); String updateurl="http://localhost:1188/border/website/updatecounts?counts="+counts+"&keynum="+keynum; System.out.println(updateurl); URL url=new URL(updateurl); // 打开和URL之间的连接 URLConnection connection = url.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 connection.connect(); System.out.println("数据已更新"); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { System.out.println("存储数据库失败++"); e.printStackTrace(); } }
阅读全文
0 0
- java构造http请求
- java 后台构造http转发请求
- java使用NIO构造http请求
- Fiddler 构造http请求
- python构造一个http请求
- Fiddler构造HTTP-GET请求
- php构造http头部发送请求
- php http扩展 构造请求 第一次实例
- 手动构造HTTP请求实例讲解
- java 发送http请求
- Java HTTP请求
- java 的http请求
- Java异步HTTP请求
- java实现Http请求
- java 发起HTTP请求
- JAVA发送HTTP请求
- java 请求 http
- Java发送Http请求
- 数据结构与算法C++描述(9)---字典
- android面试-个人面试总结(拿到offer)
- 阅读下面UML类图,给出C++形式的头文件
- 统计学习方法--朴素贝叶斯 python实现
- Spring中interceptor详解
- java构造http请求
- 微服务进展缓慢的5个难点
- 自定义TreeView,多列带有标头,支持多选绑定
- 网络购物车
- 进程间通信-Queue(python版)
- 连接远程数据库
- 回到顶部实例scrollTop
- Tomcat配置Https加密方法
- win10下配置opencv、zbar