Java HttpURLConnection Create createHttpURLConnection(String urlString, int timeout)

Here you can find the source of createHttpURLConnection(String urlString, int timeout)

Description

Create an HTTP connection (GET).

License

Open Source License

Parameter

Parameter Description
urlString a parameter
timeout a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static HttpURLConnection createHttpURLConnection(String urlString, int timeout) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007 Exadel, Inc. and Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from w  w  w.j  a  va2  s.  c om*/
 *     Exadel, Inc. and Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

import java.io.IOException;

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

public class Main {
    /**
     * Create an HTTP connection (GET). Returns null if the URL does not use HTTP(S) protocol.
     * @param urlString
     * @param timeout
     * @return
     * @throws IOException
     */
    public static HttpURLConnection createHttpURLConnection(String urlString, int timeout) throws IOException {
        URL url = new URL(urlString);
        URLConnection connetion = url.openConnection();
        HttpURLConnection httpConnection = null;
        if (connetion instanceof HttpURLConnection) {
            httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setInstanceFollowRedirects(true);
            httpConnection.setRequestMethod("GET");
            httpConnection.setConnectTimeout(timeout);
        }
        return httpConnection;
    }
}

Related

  1. createHttpURLGetConnection(String url, int timeout)
  2. getHttpConnection(Proxy prx, URL u, int millis)
  3. getHttpConnection(String urlStr, Proxy proxy)
  4. getHttpConnection(String urlStr, String charSet, Map props)