httpcomponents-client 使用小结
来源:互联网 发布:pscc是什么软件 编辑:程序博客网 时间:2024/05/22 09:06
最近在搞远程访问数据的事,加载远程页面 ,开始想用jsp include加载页面会很方便 但是发现jsp的不论动态还是静态include都不可以加载远程数据,只能访问本站内部网页,
这个方案就被放弃了,然后是用js加载,也折腾了一天总算加载进来了,但是发现查看源码里面没有加载的页面内容只是把页面的现象加载进来了,实际内容没有,这不是我想要的,我要google能抓到我的加载的内容,所以js方法也被pass掉,最后用java读取远程文件吧内容放到前台,这样就可以了,不过开始用的httpurl这回出现乱码,也放弃了,最后有一个同事告诉我用httpcomponents-client 很好用,试了试,开始也是问题很多,比如说 因为版本冲突导致action出错,这些解决来之后显示很好,解决来我的问题。分享一下我的代码供大家参考,大家共同学习:
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpGet httpGet = new HttpGet("http://targethost/homepage");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpGet httpGet = new HttpGet("http://targethost/homepage"); CloseableHttpResponse response1 = httpclient.execute(httpGet); try { System.out.println(response1.getStatusLine());//请求是否成功 HttpEntity entity1 = response1.getEntity();//得到请求的尸体 InputStream is= entity1.getContent();//得到请求的内容 BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "/n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } finally { response1.close(); } }catch(Exception e){ e.printStackTrace(); }
sb即使请求页面的内容,可以找一个参数接收,然后传给前台。一定不要出现版本冲突,不然真的不知道错在哪里了,我现在用的都是4.5的包。一定要检查一下自己的包是否有冲突,提醒大家。希望能帮到大家大家
0 0
- httpcomponents-client 使用小结
- httpcomponents-client-4.3.4 HTTPS地址访问
- HttpComponents
- 使用HttpComponents抓取网页内容
- HttpComponents(Apache HttpComponents Client 4.1.3)通过代理访问网页的设置方法
- HttpComponents(Apache HttpComponents Client 4.1.3)通过Post方式访问网页或传输参数
- Httpclient4.3.5简单get,post实用(httpcomponents-client-4.3.5)
- httpcomponents-client-4.5.3 学习笔记(1) 1.1 发送请求
- httpcomponents-client-4.5.3 学习笔记(2) 1.2 HttpClient接口
- 使用Apache HttpComponents 实现模拟登录
- Apache HttpComponents(HTTPClient) Fluent API 使用
- 使用HttpComponents(即HttpClient)抓取数据
- Apache HttpComponents Client 4.0快速入门/升级-1.GET方法访问网页
- Apache HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页
- HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页
- File Upload With HttpComponents Client 4.0 (Successor of Commons HttpClient 3.x)
- httpcomponents-client-4.3.3和jdk1.6 httpserver通过http协议交互数据
- httpcomponents-client-4.3.5实现http的post请求和get请求方法
- Android 多媒体应用——SoundPool音频播放
- qtopia2.0桌面环境和qt4应用程序共存の法
- 函数参数的三种传值方式
- ping 探测路径mtu大小
- java设计模式第10弹--享元模式
- httpcomponents-client 使用小结
- 例题6-4 破损的键盘 UVa 11988
- C#多线程编程
- 面试记录
- 2015年9月15日工作日志-------------赵鑫
- Mysql命令行添加用户
- JAVA中复杂对象调用构造器顺序
- 安装MySql解压版
- Android——Carema的使用