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 getHtml(String getUrl, int outtime, String charsetName) {
        String html = "";
        URL url;
        try {
            url = new URL(getUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("User-Agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; CIBA)");
            // connection.setRequestProperty("Connection", "Keep-Alive");
            // connection.setRequestProperty("Cache-Control", "no-cache");
            connection.setConnectTimeout(outtime);
            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');
            }
            try {
                br.close();
            } catch (Exception e) {
                // TODO: handle exception
            }
            try {
                connection.disconnect();
            } catch (Exception e) {
                // TODO: handle exception
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return html;
    }
}