com.rubtsov.pageLoader.PageLoader.java Source code

Java tutorial

Introduction

Here is the source code for com.rubtsov.pageLoader.PageLoader.java

Source

package com.rubtsov.pageLoader;

/*
 * #%L
 * youtubeConnector
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2013 rubtsov
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

/**
 * web page loader - loading web page by url
 * @author rubtsov
 */
public class PageLoader {
    /**
     * function returns string with loaded by url web page
     * @param url url  of page to load
     * @return string with loaded by url web page
     * @throws ClientProtocolException if error on page get occured
     * @throws IOException if error on to string conversion occured
     */
    public String getPage(String url) throws ClientProtocolException, IOException, Exception {
        url = url.trim();
        if ((url == null) || (url.isEmpty())) {
            throw new IllegalArgumentException("url parameter is empty");
        }

        if (!url.startsWith("http://")) {
            throw new Exception("url must start with http://");
        }

        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = httpclient.execute(httpGet);
        try {
            response.getStatusLine();
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity);

        } finally {
            httpGet.releaseConnection();
        }
    }
}