集册 Java实例教程 提取HTTP数据

提取HTTP数据

欢马劈雪     最近更新时间:2020-01-02 10:19:05

747
提取HTTP数据
/* from N o w J a v a . c o m - 时代Java*/

import java.io.*;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.URLEncoder;

import java.net.URL;

import java.net.HttpURLConnection;

import java.io.DataInputStream;

import java.util.List;

import java.util.Collections;

import java.text.SimpleDateFormat;

import java.util.Date;


public class Main{

    public static String charset = java.nio.charset.StandardCharsets.UTF_8

            .name();

    private static String USER_AGENT = "Mozilla/5.0";//来自 时 代 J a v a 公 众 号 - N o w J a v  a . c o m

    public static String workingDirectory;

    /**

     * 

     * @param URL

     * @param query

     * @return

     * @throws IOException 

     */

    public static String fetchHTTPData(String URL, String query) {

        String response = "";

        int responseCode = 0;

        try {

            HttpURLConnection httpConn = (HttpURLConnection) new URL(URL

                    + "?" + query).openConnection();

            httpConn.setDoOutput(true); // Triggers POST.


            httpConn.setRequestProperty("Accept-Charset", charset);

            httpConn.setRequestProperty("User-Agent", USER_AGENT);

            responseCode = httpConn.getResponseCode();

            if (responseCode == 200) { //OK

                BufferedReader in = new BufferedReader(

                        new InputStreamReader(httpConn.getInputStream(),

                                "UTF-8"));

                String inputLine;

                StringBuffer responseBuffer = new StringBuffer();


                while ((inputLine = in.readLine()) != null) {

                    responseBuffer.append(inputLine);

                }

                in.close();

                response = responseBuffer.toString();

            }

        } catch (Exception ex) {

            HelperFunctions

                    .writeInformationIntoFile("Exception while fetching HTTP from URL:"

                            + URL

                            + "?"

                            + query

                            + "\r\nError stackTrace :"

                            + ex.getMessage());

        }

        return response;

    }

    /**

     * Write the information into the log file

     */

    public static void writeInformationIntoFile(String text) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(

                "yyyy_MM_dd_hh_mm_ss");

        try {

            String fileName = HelperFunctions.workingDirectory

                    + 
展开阅读全文