Creates a new java.net.URLConnection object from the specified java.net.URL. - Java Network

Java examples for Network:Http

Description

Creates a new java.net.URLConnection object from the specified java.net.URL.

Demo Code


//package com.java2s;

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

public class Main {
    /**//from w w  w .  ja  v a  2 s  .co  m
     * Creates a new <code>java.net.URLConnection</code> object from the
     * specified <code>java.net.URL</code>. This is a convenience method which
     * will set the <code>doInput</code>, <code>doOutput</code>,
     * <code>useCaches</code> and <code>defaultUseCaches</code> fields to the
     * appropriate settings in the correct order.
     * 
     * @return a <code>java.net.URLConnection</code> object for the URL
     * @throws java.io.IOException
     *             on input/output errors
     */
    public static URLConnection createConnection(URL url)
            throws java.io.IOException {
        URLConnection urlConn = url.openConnection();
        if (urlConn instanceof HttpURLConnection) {
            HttpURLConnection httpConn = (HttpURLConnection) urlConn;
            httpConn.setRequestMethod("POST");
        }
        urlConn.setDoInput(true);
        urlConn.setDoOutput(true);
        urlConn.setUseCaches(false);
        urlConn.setDefaultUseCaches(false);
        return urlConn;
    }
}

Related Tutorials