Java HttpURLConnection Create getHttpUrlConnection(URL url)

Here you can find the source of getHttpUrlConnection(URL url)

Description

Returns a HttpUrlConnection for a URL pointing to an HTTP resource.

License

Open Source License

Parameter

Parameter Description
url the url of a HTTP resource

Exception

Parameter Description
IOException an exception

Return

a HttpUrlConnection

Declaration

public static HttpURLConnection getHttpUrlConnection(URL url) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  w w w.ja v  a2s  .c  o m*/
 *    Webical - http://www.webical.org
 *    Copyright (C) 2007 Func. Internet Integration
 *
 *    This file is part of Webical.
 *
 *    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/>.
 */

import java.io.IOException;

import java.net.HttpURLConnection;

import java.net.URL;
import java.net.URLConnection;

public class Main {
    /**
     * Returns a HttpUrlConnection for a URL pointing to an HTTP resource. Make sure it does point to a HTTP resource or a IlliagalArgumentException is thrown
     * @param url the url of a HTTP resource
     * @return a HttpUrlConnection
     * @throws IOException
     */
    public static HttpURLConnection getHttpUrlConnection(URL url) throws IOException {
        URLConnection connection = url.openConnection();
        if (connection instanceof HttpURLConnection) {
            return (HttpURLConnection) connection;
        } else {
            throw new IllegalArgumentException("Url is not targeted at an HTTP resource" + url);
        }
    }
}

Related

  1. getHttpURLConnection(String uri, String soapAction, boolean soap12)
  2. getHttpUrlConnection(String url)
  3. getHttpURLConnection(String urlAsString)
  4. getHttpUrlConnection(String urlStr)
  5. getHttpURLConnection(URL url)