Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
    public static int timeout = 1000 * 10;

    public static String getHtml(String getUrl, String charsetName) {
        String html = "";
        URL url;
        try {
            url = new URL(getUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("User-Agent", userAgent);
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Cache-Control", "no-cache");
            connection.setReadTimeout(timeout);
            connection.setFollowRedirects(true);
            connection.connect();
            InputStream inStrm = connection.getInputStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(inStrm, charsetName));
            String temp = "";

            while ((temp = br.readLine()) != null) {
                html = html + (temp + '\n');
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return html;
    }
}