Java URLConnection Create createConnection(URL url)

Here you can find the source of createConnection(URL url)

Description

create Connection

License

Open Source License

Declaration

public static String createConnection(URL url) throws IOException 

Method Source Code


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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static String createConnection(URL url) throws IOException {
        URLConnection connection = url.openConnection();
        connection.setReadTimeout(5000);
        connection.setConnectTimeout(5000);
        connection.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36");
        connection.addRequestProperty("Content-Type", "application/json");
        connection.addRequestProperty("Content-Encoding", "gzip");
        connection.addRequestProperty("Accept", "application/json");
        connection.addRequestProperty("Connection", "close");
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line = null;//from  w  w  w.j  a v a 2  s  . c  o  m
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    }
}

Related

  1. createConnection(final URL url, final String authValue)
  2. createConnection(String method, String destinationUrl)
  3. createConnection(String processorName, String baseUrl)
  4. createConnection(URL url)
  5. createConnection(URL url)
  6. createConnection(URL url, Proxy proxy)
  7. getURLConnection(String uri)
  8. getUrlConnection(String urlString)
  9. getURLConnection(URL url)