Java Utililty Methods URL Connection

List of utility methods to do URL Connection

  1. HOME
  2. Java
  3. U
  4. URL Connection

Description

The list of methods to do URL Connection are organized into topic(s).

Method

void addHeaders(Map headers, URLConnection c)
add Headers
if (headers != null) {
 for (Entry<String, String> e : headers.entrySet()) {
 c.setRequestProperty(e.getKey(), e.getValue());
void addHeaders(URLConnection conn)
adds headers to the url to make it look less like a bot
conn.addRequestProperty("User-Agent", userAgent);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.5");
conn.addRequestProperty("Accept-Encoding", "gzip,deflate");
conn.addRequestProperty("DNT", "1");
void addProperty(URLConnection connection)
add Property
connection.addRequestProperty("Accept",
 "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-silverlight, */*");
connection.setRequestProperty("Referer", "https://9.186.10.56:8443/index.jsp");
connection.setRequestProperty("Accept-Language", "zh-cn");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
connection.setRequestProperty("User-Agent",
 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Foxy/1; .NET CLR 2.0.50727;MEGAUPLOAD 1.0)");
...
URL byteArrayToURL(final byte[] bytes)
byte Array To URL
try {
 return new URL(null, "foobar://foo/bar", new URLStreamHandler() {
 @Override
 protected URLConnection openConnection(final URL u) throws IOException {
 final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
 return new URLConnection(null) {
 @Override
 public void connect() throws IOException {
...
void closeURLClassLoader(URLClassLoader clazzLdr)
This method is designed to clear out classloader file locks in windows.
HashSet<String> closedFiles = new HashSet<>();
try {
 Object obj = getFieldObject(URLClassLoader.class, "ucp", clazzLdr);
 ArrayList loaders = (ArrayList) getFieldObject(obj.getClass(), "loaders", obj);
 for (Object ldr : loaders) {
 try {
 JarFile file = (JarFile) getFieldObject(ldr.getClass(), "jar", ldr);
 closedFiles.add(file.getName());
...
URLConnection createImgUrlConnection(String url)
create Img Url Connection
URLConnection conn = new URL(url).openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
conn.setRequestProperty("Accept", "image/png,*/*;q=0.5");
conn.setRequestProperty("Accept-Language", "en-gb,en;q=0.5");
conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
return conn;
URLClassLoader createURLClassLoader(String dirPath)
create URL Class Loader
String path;
File file;
URL appRoot;
URL classesURL;
if (!dirPath.endsWith("/") && !dirPath.endsWith(".war") && !dirPath.endsWith(".jar")) {
 dirPath += File.separator;
String separator = (System.getProperty("os.name").toLowerCase().startsWith("win") ? "/" : "//");
...
URLConnection createURLConnection(String url)
create URL Connection
try {
 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;
...
String createWebStartDirectory(String name, String jarUrl)
create Web Start Directory
String deploymentUserTmp = System.getProperty("deployment.user.tmp");
JarFile jar = getJar(jarUrl);
String dir = createDirectory(deploymentUserTmp, name);
extract(jar, dir);
return dir;
String doGet(String targetURL)
Retrieve file contents
URLConnection connection = null;
try {
 URL url = new URL(targetURL);
 connection = url.openConnection();
 BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
 StringBuilder response = new StringBuilder();
 String line;
 while ((line = br.readLine()) != null) {
...


AltStyle によって変換されたページ (->オリジナル) /