java抓取http指定内容_Java实现多种方式的http数据抓取

packagecom.yeezhao.common.http;importjava.io.BufferedReader;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.net.HttpURLConnection;importjava.net.URL;importorg.apache.commons.httpclient.HttpClient;importorg.apache.commons.httpclient.HttpMethod;importorg.apache.commons.httpclient.methods.GetMethod;importorg.apache.commons.io.IOUtils;importorg.jsoup.Jsoup;/*** http工具对比

*

*@authorAdministrator -> junhong

*

* 2016年12月27日*/

public classHttpFetchUtil {/*** 获取访问的状态码

*@paramrequest

*@return*@throwsException*/

public static int getResponseCode(String request) throwsException {

URL url= newURL(request);

HttpURLConnection conn=(HttpURLConnection) url.openConnection();returnconn.getResponseCode();

}/*** 1)JDK自带HTTP连接,获取页面或Json

*@paramrequest

*@paramcharset

*@return*@throwsException*/

public static String JDKFetch(String request, String charset) throwsException {

URL url= newURL(request);

HttpURLConnection conn=(HttpURLConnection) url.openConnection();//模拟浏览器参数

conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36"

+ " (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");if (conn.getResponseCode() ==HttpURLConnection.HTTP_OK) {

InputStream input=conn.getInputStream();

StringBuffer sb= newStringBuffer();

BufferedReader reader= new BufferedReader(newInputStreamReader(input, charset));

String s;while ((s = reader.readLine()) != null) {

sb.append(s+ "\n");

}

input.close();

conn.disconnect();returnsb.toString();

}return "";

}/*** 2) JDK自带URL连接,获取页面或Json

*@paramrequest

*@paramcharset

*@return*@throwsException*/

public static String URLFetch(String request, String charset) throwsException {

URL url= newURL(request);returnIOUtils.toString(url.openStream());

}/*** 3)HttpClient Get工具,获取页面或Json

*@paramurl

*@paramcharset

*@return*@throwsException*/

public static String httpClientFetch(String url, String charset) throwsException {//GET

HttpClient httpClient = newHttpClient();

httpClient.getParams().setContentCharset(charset);

HttpMethod method= newGetMethod(url);

httpClient.executeMethod(method);returnmethod.getResponseBodyAsString();

}/*** 4)commons-io工具,获取页面或Json

*@paramurl

*@paramcharset

*@return*@throwsException*/

public static String commonsIOFetch(String url, String charset) throwsException {return IOUtils.toString(newURL(url), charset);

}/*** 5) Jsoup工具(通常用于html字段解析),获取页面,非Json返回格式

*@paramurl

*@return*@throwsException*/

public static String jsoupFetch(String url) throwsException {return Jsoup.parse(new URL(url), 2 * 1000).html();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值