Java HTTP Read getInputStreamFromURL(String urlname)

Here you can find the source of getInputStreamFromURL(String urlname)

Description

get Input Stream From URL

License

LGPL

Parameter

Parameter Description
urlname a parameter

Declaration

public static InputStream getInputStreamFromURL(String urlname)
        throws Exception 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;

public class Main {
    /**/* w w  w .  j a  va  2s .  c o  m*/
     * 
     * @param urlname
     * @return
     */
    public static InputStream getInputStreamFromURL(String urlname)
            throws Exception {
        InputStream stream = null;

        try {
            URL url = new URL(urlname);
            java.net.HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();

            if (connection.getResponseCode() != 200
                    && connection.getResponseCode() != 404
                    && connection.getResponseCode() != 202) {
                throw new IOException(connection.getResponseMessage());
            }
            stream = connection.getInputStream();
        } catch (MalformedURLException mue) {
            System.err.println("Error: Could create URL object!");
            throw new Exception();
        } catch (IOException e) {
            System.err.println("Error: Could not open URL connection!");
            System.err.println(urlname);
            throw new Exception();
        }

        return stream;
    }

    /**
     * 
     * @param urlname
     * @return
     */
    public static InputStream getInputStreamFromURL(String urlname,
            Object proxyServer, Object proxyPort) throws Exception {
        InputStream stream = null;

        try {
            URL url = new URL(urlname);
            java.net.HttpURLConnection connection = null;
            Proxy proxy = null;
            try {
                if (proxyServer != null && proxyPort != null) {
                    proxy = new Proxy(Proxy.Type.HTTP,
                            new InetSocketAddress((String) proxyServer,
                                    (Integer) proxyPort));
                }
            } catch (Exception e1) {
                System.err.println("Could not set proxy. Check settings.");
            }

            if (proxy == null)
                connection = (HttpURLConnection) url.openConnection();
            else
                connection = (HttpURLConnection) url.openConnection(proxy);

            if (connection.getResponseCode() != 200
                    && connection.getResponseCode() != 404
                    && connection.getResponseCode() != 202) {
                throw new IOException(connection.getResponseMessage());
            }
            stream = connection.getInputStream();
        } catch (MalformedURLException mue) {
            System.err.println("Error: Could create URL object!");
            throw new Exception();
        } catch (IOException e) {
            System.err.println("Error: Could not open URL connection!");
            System.err.println(urlname);
            throw new Exception();
        }

        return stream;
    }
}

Related

  1. getHtmlContent(String url)
  2. getInputStream(HttpURLConnection conn)
  3. getInputStream(HttpURLConnection connection)
  4. getInputStream(String myUrl)
  5. getInputStream(String url)
  6. getInputStreamFromUrl(String urlString)
  7. getInputStreamReader(String httpUrl, int timeout)
  8. getJsonFromUrlWithJsonParameter(String url, String jsonRequest)
  9. getJsonString(String url)