Foreword:
Nowadays, the first wave of the Internet has faded away, and with it comes the Internet of Things era based on thousands of data. Therefore, data has become one of the important strategic resources of enterprises. one. Based on data capture technology, this article introduces Java-related capture tools and attaches demo source code for interested friends to test!
1) JDK comes with HTTP connection to get the page or Json
2) JDK comes with URL connection, get the page or Json
3) HttpClient Get tool, get the page or Json
4) commons-io tool, get the page or Json
5) Jsoup tool (usually used for html Field parsing), get the page, non-Json return format]
package com.yeezhao.common.http;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpMethod;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.io.IOUtils;import org.jsoup.Jsoup;/** * http工具对比 * * @author Administrator -> junhong * * 2016年12月27日 */public class HttpFetchUtil { /** * 获取访问的状态码 * @param request * @return * @throws Exception */ public static int getResponseCode(String request) throws Exception { URL url = new URL(request); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); return conn.getResponseCode(); } /** * 1)JDK自带HTTP连接,获取页面或Json * @param request * @param charset * @return * @throws Exception */ public static String JDKFetch(String request, String charset) throws Exception { URL url = new URL(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 = new StringBuffer(); BufferedReader reader = new BufferedReader(new InputStreamReader(input, charset)); String s; while ((s = reader.readLine()) != null) { sb.append(s + "\n"); } input.close(); conn.disconnect(); return sb.toString(); } return ""; } /** * 2) JDK自带URL连接,获取页面或Json * @param request * @param charset * @return * @throws Exception */ public static String URLFetch(String request, String charset) throws Exception { URL url = new URL(request); return IOUtils.toString(url.openStream()); } /** * 3)HttpClient Get工具,获取页面或Json * @param url * @param charset * @return * @throws Exception */ public static String httpClientFetch(String url, String charset) throws Exception { // GET HttpClient httpClient = new HttpClient(); httpClient.getParams().setContentCharset(charset); HttpMethod method = new GetMethod(url); httpClient.executeMethod(method); return method.getResponseBodyAsString(); } /** * 4)commons-io工具,获取页面或Json * @param url * @param charset * @return * @throws Exception */ public static String commonsIOFetch(String url, String charset) throws Exception { return IOUtils.toString(new URL(url), charset); } /** * 5) Jsoup工具(通常用于html字段解析),获取页面,非Json返回格式 * @param url * @return * @throws Exception */ public static String jsoupFetch(String url) throws Exception { return Jsoup.parse(new URL(url), 2 * 1000).html(); } }
The above is the detailed content of Several ways to implement http data grabbing in Java. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

Dreamweaver CS6
Visual web development tools