Java HTTP Read getInputStreamFromUrl(String urlString)

Here you can find the source of getInputStreamFromUrl(String urlString)

Description

get Input Stream From Url

License

Apache License

Declaration

private static InputStream getInputStreamFromUrl(String urlString) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2013 Johns Hopkins University
 * //from   w  w  w .j  a va2 s .c  o m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    private static InputStream getInputStreamFromUrl(String urlString) throws IOException {
        URL url = new URL(urlString);
        if (!url.getProtocol().toLowerCase().startsWith("http"))
            throw new UnsupportedOperationException(
                    "URL \"" + urlString + "\" has an unsupported protocol, \"" + url.getProtocol() + "\".");
        else {
            HttpURLConnection urlConn = (HttpURLConnection) new URL(urlString).openConnection();
            urlConn.setConnectTimeout(10000); // millis -- 10 seconds
            urlConn.setRequestMethod("GET");
            urlConn.connect();
            return urlConn.getInputStream();
        }
    }
}

Related

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