通过程序访问页面-HTTP请求
来源:互联网 发布:android网络框架 编辑:程序博客网 时间:2024/05/22 03:07
一般的情况下我们都是使用IE或者谷歌浏览器来访问一个WEB服务器,用来浏览页面查看信息或者提交一些数据等等。但某些情况下可能需要通过程序来访问这些页面,从别人的网页中”偷”一些数据。例如,我们想获取某一微信公众号的信息,但是我们自己又没有这样的数据,只好借助搜狗搜索引擎来完成,需要向网页提交公众号关键字并从返回的页面中解析我们想要的数据。
JDK的java.net包中已经提供了访问HTTP协议的基本功能。如果要访问的对象仅仅是一个简单的页面,JDK提供的HttpURLConnection完全可以应付。
但是考虑到一些服务授权的问题,很多公司提供的页面往往并不是可以通过一个简单的URL就可以访问的,有的需要用户登录后方可使用、有的需要认证、有的是通过加密方式传输,例如HTTPS。
这个时候就涉及到COOKIE问题的处理。我们知道目前流行的动态网页技术例如ASP、JSP无不是通过COOKIE来处理会话信息的。为了使我们的程序能使用别人所提供的服务页面,就要求程序首先登录后再访问服务页面,这过程就需要自行处理cookie,用java.net.HttpURLConnection来完成这些功能时是多么恐怖的事情!
HttpClient 是 Apache Jakarta Common 下的子项目,就是专门设计来简化HTTP客户端与服务器进行各种通讯编程。通过它可以让原来很头疼的事情现在轻松的解决,例如你不再管是HTTP或者HTTPS的通讯方式,告诉它你想使用HTTPS方式,剩下的事情交给httpclient替你完成。
HttpClient类
构造函数
public HttpClient(){ this(new HttpClientParams());}
// HttpConnectionManager是抽象接口public HttpClient(HttpClientParams params, HttpConnectionManager httpConnectionManager){ if (httpConnectionManager == null) { throw new IllegalArgumentException("httpConnectionManager cannot be null"); } if (params == null) { throw new IllegalArgumentException("Params may not be null"); } this.params = params; this.httpConnectionManager = httpConnectionManager; this.httpConnectionManager.getParams().setDefaults(this.params);// !!!将传入的params设置到httpConnectionManager}
// 只传入params时,默认ConnectionManager为SimpleHttpConnectionManagerpublic HttpClient(HttpClientParams params){ if (params == null) { throw new IllegalArgumentException("Params may not be null"); } this.params = params; this.httpConnectionManager = null; Class clazz = params.getConnectionManagerClass(); if (clazz != null) { try { this.httpConnectionManager = ((HttpConnectionManager)clazz.newInstance()); } catch (Exception e) { LOG.warn("Error instantiating connection manager class, defaulting to SimpleHttpConnectionManager", e); } } if (this.httpConnectionManager == null) { this.httpConnectionManager = new SimpleHttpConnectionManager(); } if (this.httpConnectionManager != null) this.httpConnectionManager.getParams().setDefaults(this.params);}
public HttpClient(HttpConnectionManager httpConnectionManager){ this(new HttpClientParams(), httpConnectionManager);}
HttpConnectionManager
读取网页(HTTP/HTTPS)内容
/** * 最简单的例子,通过GET/POST方法访问某个页面 * 该例子体现了HttpClient的基本流程 * @author: xuexuan * 2016-11-11 下午3:35:23 * @param host 代理服务IP * @param port 代理服务端口 * @param url 请求URL */public static String requestUrl(String host, int port, String url) { try { HttpClient client = new HttpClient();// 创建http客户端(HttpClient)实例 if (StringUtils.isNotBlank(host) && port != 0) { client.getHostConfiguration().setProxy(host, port);// 有时需要设置代理 } HttpMethod method = new GetMethod(url);// 使用get方法,url可以是HTTP连接,也可以是HTTPS连接 //HttpMethod method = new PostMethod(url);// 使用post方法 client.executeMethod(method);// 在http客户端实例上执行get方法 if (method.getStatusCode() != HttpStatus.SC_OK) return ""; return method.getResponseBodyAsString();// 获取返回内容 } catch (Exception e) { e.printStackTrace(); return ""; }}
以GET/POST方式向网页提交参数
上个例子介绍如何使用GET/POST方式请求一个页面,本例不同之处是多了提交时设定页面所需的参数。
若为GET请求,所有参数直接放在URL之后,每个参数用&隔开。例如:https://www.baidu.com/s?ie=UTF-8&wd=123
下面介绍POST方式
- 通过程序访问页面-HTTP请求
- 通过http请求访问webservice
- http java GET请求 访问页面
- 通过http请求获取指定url的页面
- 使用java程序模拟页面发送http的post请求
- 使用java程序模拟页面发送http的post请求
- java程序模拟页面发送http的post请求
- 使用java程序模拟页面发送http的post请求
- 通过http请求服务
- http请求与访问
- C#访问HTTP请求
- C/C++程序实现通过http代理访问网页内容
- C/C++程序实现通过http代理访问网页内容
- Http: 发送http访问请求
- 通过HTTP访问2000
- SSAS - 通过HTTP访问
- SSAS - 通过HTTP访问
- 通过Http访问cube
- token的生成和上传 (七牛 安卓版)
- iOS关于SDWebImage的缓存机制
- unity3D游戏开发十六之NGUI三
- HashMap工作原理
- linux启动流程
- 通过程序访问页面-HTTP请求
- meta中的viewport指令
- 一周总结
- java:xxx cannot be resolved to a type
- 总结iOS App开发中控制屏幕旋转的几种方式
- 第一次写博客
- PHP 加载一次页面时, 浏览器请求多次, 导致出错
- Shell中while循环的done 后接一个重定向<
- 一个小脚本----各种编译命令合入到同一个脚本