Java URL Connection createURLConnection(String url)

Here you can find the source of createURLConnection(String url)

Description

create URL Connection

License

Open Source License

Declaration

public static URLConnection createURLConnection(String url) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static URLConnection createURLConnection(String url) {
        try {//from ww  w.  j a va2s.c  o  m
            final URL address = new URL(url);
            final URLConnection connection = address.openConnection();
            connection.addRequestProperty("User-Agent",
                    "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0");
            connection.setConnectTimeout(5000);
            connection.setRequestProperty("Content-Type", "image/png");
            return connection;
        } catch (IOException ex) {
            System.out.println("Error creating connection!");
            ex.printStackTrace();
        }
        return null;
    }
}

Related

  1. addProperty(URLConnection connection)
  2. byteArrayToURL(final byte[] bytes)
  3. closeURLClassLoader(URLClassLoader clazzLdr)
  4. createImgUrlConnection(String url)
  5. createURLClassLoader(String dirPath)
  6. createWebStartDirectory(String name, String jarUrl)
  7. doGet(String targetURL)
  8. doGet(String url, String charset)
  9. doPost(final URL url, final Map parameters, final boolean encode)