Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

public class Main {
    private static final int HTTP_STATUS_OK = 200;

    protected static synchronized InputStream getAWebPage(URL url) throws ClientProtocolException, IOException {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet(url.toString());
        try {
            //do the request
            HttpResponse response = httpClient.execute(httpGet, localContext);
            StatusLine status = response.getStatusLine();
            if (status.getStatusCode() != HTTP_STATUS_OK) {
                throw new IOException("Invalid response from the IKSU server! " + status.toString());
            }
            //InputStream ist = response.getEntity().getContent();

            return response.getEntity().getContent();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ClientProtocolException("Protocol Exception! " + e.toString());
        }
    }
}