MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainClass {

    public static void main(String[] args) throws Exception {
        URL u = new URL("http://www.java2s.com");
        HttpURLConnection uc = (HttpURLConnection) u.openConnection();
        int code = uc.getResponseCode();
        String response = uc.getResponseMessage();
        System.out.println("HTTP/1.x " + code + " " + response);
        for (int j = 1;; j++) {
            String header = uc.getHeaderField(j);
            String key = uc.getHeaderFieldKey(j);
            if (header == null || key == null)
                break;
            System.out.println(uc.getHeaderFieldKey(j) + ": " + header);
        }
        InputStream in = new BufferedInputStream(uc.getInputStream());

        Reader r = new InputStreamReader(in);
        int c;
        while ((c = r.read()) != -1) {
            System.out.print((char) c);
        }
    }

}