使用GET与POST方式获取html数据
抓取网站数据解析的工作,其中,使用到GET和POST方法获取html数据。
使用GET方式:
- /**
- * 使用get方式获取html数据
- *
- * @param strURL(需要访问的网站)
- * @return
- * @throws Exception
- */
- public String getHTML(String strURL) throws Exception {
- //创建浏览器
- HttpClient httpClient = HttpClients.createDefault();
- String html = null;
- //预防网址链接中包含特殊字符,将url转为uri
- URL url = new URL(strURL);
- URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(),
- url.getQuery(), null);
- //使用get方式
- HttpGet request = new HttpGet(uri);
- HttpResponse response;
- try {
- //连接网址获取返回的数据
- response = httpClient.execute(request);
- //将返回的数据按照gbk的方式编码
- html = EntityUtils.toString(response.getEntity(), "GBK");
- } catch (IOException e) {
- e.printStackTrace();
- }
- //断开连接
- request.abort();
- //返回网址所发挥的html数据
- return html;
- }
使用该方法便可以获取得到网站所发挥的html数据。
使用POST方式:
- /**
- * 使用post方式获取html数据
- *
- * @param libraryUrl(需要访问的网站)
- * @param params(需要传入的参数)
- * @return
- * @throws Exception
- */
- public String postHTML(String strURL, List<NameValuePair> params)
- throws Exception {
- //创建浏览器
- HttpClient httpClient = HttpClients.createDefault();
- String html = null;
- //预防网址链接中包含特殊字符,将url转为uri
- URL url = new URL(strURL);
- URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(),
- url.getQuery(), null);
- //使用POST方式
- HttpPost request = new HttpPost(uri);
- //将参数封装进UrlEncodedFormEntity中
- UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params);
- request.setEntity(entity);
- HttpResponse response;
- try {
- //连接网址获取返回的数据
- response = httpClient.execute(request);
- //将返回的数据按照gbk的方式编码
- html = EntityUtils.toString(response.getEntity(), "GBK");
- } catch (IOException e) {
- e.printStackTrace();
- }
- //断开连接
- request.abort();
- //返回网址所发挥的html数据
- return html;
- }
其中,参数params的封装可以参照以下方式:
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- //以键值对的方式存储
- params.add(new BasicNameValuePair("format", "hitcount"));
使用GET与POST方式获取html数据的更多相关文章
- Http方式获取网络数据
通过以下代码可以根据网址获取网页的html数据,安卓中获取网络数据的时候会用到,而且会用Java中的sax方式解析获取到数据.(sax解析主要是解析xml)具体代码如下: package com.wy ...
- 基于uFUN开发板的心率计(一)DMA方式获取传感器数据
前言 从3月8号收到板子,到今天算起来,uFUN到手也有两周的时间了,最近利用下班后的时间,做了个心率计,从单片机程序到上位机开发,到现在为止完成的差不多了,实现很简单,uFUN开发板外加一个Puls ...
- IOS开发之Post 方式获取服务器数据
//1.创建post方式的 参数字符串url +(NSString *)createPostURL:(NSMutableDictionary *)params { NSString *postStri ...
- [NSURLSession/Delegate]用Post方式获取网络数据并把数据显示到表格
#pragma mark 实现NSURLSessionDataDelegate代理 @interface ViewController ()<UITableViewDataSource,UITa ...
- [NSURLConnection]分别用Post和Get方式获取网络数据并把数据显示到表格
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate> { UIButton* getButton; ...
- 用 get 同步/异步 方式获取网络数据并输出
//同步请求 //创建NSString用来存储请求的网址 NSString* str=@"http://v.juhe.cn/weather/index?format=2&cityna ...
- jQuery AJAX获取JSON数据解析多种方式示例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- J2EE Web开发入门—通过action是以传统方式返回JSON数据
关键字:maven.m2eclipse.JSON.Struts2.Log4j2.tomcat.jdk7.Config Browser Plugin Created by Bob 20131031 l ...
- android—获取网络数据
取网络数据主要靠发交易(或者说请求,接口等),而这些交易由java中的网络通信,HttpURLConnection和HttpClient实现,以下是具体例子. 大家都知道,网络通信,发送请求有两种 ...
随机推荐
- SPOJ 913 Query on a tree II
spoj题面 Time limit 433 ms //spoj的时限都那么奇怪 Memory limit 1572864 kB //1.5个G,疯了 Code length Limit 15000 B ...
- sh_08_打印分隔线
sh_08_打印分隔线 def print_line(char, times): print(char * times) print_line("hi ", 40)
- Vue CLI4.0版本正式发布了!一起来看看有哪些新的变化吧
Vue CLI4.0版本正式发布 这个主要的版本更新主要关注底层工具的必要版本更新.更好的默认设置和其他长期维护所需的微调. 我们希望为大多数用户提供平稳的迁移体验. Vue CLI v4提供了对Ni ...
- Java网络编程之Netty服务端ChannelOption.SO_BACKLOG配置
ChannelOption.SO_BACKLOG对应的是tcp/ip协议listen函数中的backlog参数,函数listen(int socketfd,int backlog)用来初始化服务端可连 ...
- Docker push image to Docker hub
1. Before push image to Docker Hub, register an account in https://hub.docker.com/ 2.Input "doc ...
- [LeetCode]-DataBase-Customers Who Never Order
Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL qu ...
- js运行原理
https://www.youtube.com/watch?v=8aGhZQkoFbQ
- ES6 暂时性死区
在ES6中,声明变量新增了两个关键字:let命令和const命令 如果在区块中存在let或者const命令时,任何变量都必须在声明之前使用,无论是区块外部的全局变量或者是区块内部的变量: /* 区块外 ...
- hook C++
Intercepting Calls to COM Interfaces(hook com接口) 通过COM组件IFileOperation越权复制文件 代码注入之远程线程篇 使用VC++通过远程进程 ...
- Text Classification
Text Classification For purpose of word embedding extrinsic evaluation, especially downstream task. ...