集册 Java实例教程 从指定的java.net.URL创建一个新的java.net.URLConnection对象。

从指定的java.net.URL创建一个新的java.net.URLConnection对象。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

487
从指定的java.net.URL创建一个新的java.net.URLConnection对象。
/* 
 来自 
*时 代 J a v a 公 众 号*/


//package com.nowjava;


import java.net.HttpURLConnection;

import java.net.URL;

import java.net.URLConnection;


public class Main {

    /**

     * 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(
展开阅读全文