Java 8 URLConnection - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 URLConnection class.

This code example shows how to use the following methods: setRequestMethod, setRequestProperty, setSSLSocketFactory
      SSLContext context = SSLContext.getInstance("TLS");
      context.init(null, trustManagerFactory.getTrustManagers(), null);
 
      URL url = new URL(String.format("https://gcm.textsecure.whispersystems.org%s", urlFragment));
      HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
      connection.setSSLSocketFactory(context.getSocketFactory());
      connection.setRequestMethod(method);
      connection.setRequestProperty("Content-Type", "application/json");
 
      if (password != null) {
        System.out.println("Adding authorization header: " + getAuthorizationHeader());
        connection.setRequestProperty("Authorization", getAuthorizationHeader());
      } 
 
      return connection;
    } catch (NoSuchAlgorithmException e) {
      throw new AssertionError(e);
    } catch (KeyManagementException e) {
      throw new AssertionError(e);
    } catch (MalformedURLException e) {
Full Snippet Info
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: connect, getInputStream, setConnectTimeout
        URL url = new URL(urlString);
        URLConnection connection = null;
 
        try { 
            connection = url.openConnection();
            connection.setConnectTimeout(5000);
            connection.connect();
        } catch (Exception e) {
            throw new Exception("Cannot connect to " + urlString, e);
        } 
 
        InputStream inputStream = null;
        try { 
            inputStream = connection.getInputStream();
        } catch (Exception e) {
            throw new Exception("Cannot retrieve content from " + url, e);
        } 
 
        try { 
            ProjectManager.singleton.importProject(projectID, inputStream, !urlString.endsWith(".tar"));
Full Snippet Info
7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: disconnect, getResponseCode, setChunkedStreamingMode, setRequestMethod, getOutputStream
This code example shows how to use the following methods: getContentLength, getContentType, getInputStream
                contentType = encoding = null;
                capacityHint = (int)file.length();
                is = new FileInputStream(file);
            } else { 
                URLConnection uc = url.openConnection();
                is = uc.getInputStream();
                if(convertToString) {
                    ParsedContentType pct = new ParsedContentType(uc.getContentType());
                    contentType = pct.getContentType();
                    encoding = pct.getEncoding();
                } 
                else { 
                    contentType = encoding = null;
                } 
                capacityHint = uc.getContentLength();
                // Ignore insane values for Content-Length 
                if (capacityHint > (1 << 20)) {
                    capacityHint = -1;
                } 
            } 
Full Snippet Info
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: disconnect, getResponseCode, connect, setRequestProperty
    URLConnection conn = url.openConnection();
    if (!(conn instanceof HttpURLConnection)) {
      throw new IOException();
    } 
    HttpURLConnection connection = (HttpURLConnection) conn;
    connection.setRequestProperty("Accept", contentTypes);
    connection.setRequestProperty("Accept-Charset", "utf-8,*");
    connection.setRequestProperty("User-Agent", "ZXing (Android)");
    try { 
      try { 
        connection.connect();
      } catch (NullPointerException npe) {
        // this is an Android bug: http://code.google.com/p/android/issues/detail?id=16895 
        Log.w(TAG, "Bad URI? " + uri);
        throw new IOException(npe);
      } catch (IllegalArgumentException iae) {
        // Also seen this in the wild, not sure what to make of it. Probably a bad URL 
        Log.w(TAG, "Bad URI? " + uri);
        throw new IOException(iae);
      } 

10
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: connect, getHeaderField, getInputStream, setRequestProperty
m_shoutcast_file.done();
		} 
	} 
	 
	public String errors()
	{ 
		if (m_shoutcast_file != null)
		{ 
			return m_errors + m_shoutcast_file.errors();
		} 
		return m_errors;
	} 
	 
	public void run() 
	{ 
		Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
		try 
		{ 
			URLConnection connection = m_url.openConnection();

8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: connect, getInputStream
        return new ServerGroupDeploymentPlanBuilderImpl(this, newSet);
    } 
 
    private AddDeploymentPlanBuilder add(String name, String commonName, URL url) throws IOException, DuplicateDeploymentNameException {
        URLConnection conn = url.openConnection();
        conn.connect();
        InputStream stream = conn.getInputStream();
        try { 
            return add(name, commonName, stream);
        } 
        finally { 
            try { stream.close(); } catch (Exception ignored) {}
        } 
    } 
 
    private RemoveDeploymentPlanBuilder replace(String name, String commonName, URL url) throws IOException {
        URLConnection conn = url.openConnection();
        conn.connect();
        InputStream stream = conn.getInputStream();
        try { 
Full Snippet Info
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: getResponseCode, connect
            getAuthenticationHandlerConfiguration(true)); 
    auth.start();
    try { 
      URL url = new URL(auth.getBaseURL());
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.connect();
      Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
    } finally { 
      auth.stop();
    } 
  } 
 
  @Test 
  public void testAnonymousDisallowed() throws Exception { 
    AuthenticatorTestCase auth = new AuthenticatorTestCase();
    auth.setAuthenticationHandlerConfig(
            getAuthenticationHandlerConfiguration(false)); 
    auth.start();
    try { 
      URL url = new URL(auth.getBaseURL());
Full Snippet Info
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: setDoInput
        final URL url = new URL(spec); 
        Callable<String> task = new Callable<String>() { 
            @Override 
            public String call() throws Exception { 
                final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
                conn.setDoInput(true); 
                return processResponse(conn); 
            } 
        }; 
        return execute(task, timeout, unit); 
    } 
 
    /** 
     * Returns the URL response as a string. 
     * 
     * @param spec  URL spec 
     * @param waitUntilAvailableMs  maximum timeout in milliseconds to wait for the URL to return non 404 response 
     * @param responseTimeout  the timeout to read the response 
     * @param responseTimeoutUnit  the time unit for responseTimeout 
     * @return  URL response 
Full Snippet Info
7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: getResponseCode, getInputStream
     * the request fails. 
     */ 
    public List entries;
 
    public ListAllMyBucketsResponse(HttpURLConnection connection) throws IOException {
        super(connection);
        if (connection.getResponseCode() < 400) {
            try { 
                XMLReader xr = Utils.createXMLReader();
                ListAllMyBucketsHandler handler = new ListAllMyBucketsHandler();
                xr.setContentHandler(handler);
                xr.setErrorHandler(handler);
 
                xr.parse(new InputSource(connection.getInputStream()));
                entries = handler.getEntries();
            } catch (SAXException e) {
                throw new RuntimeException("Unexpected error parsing ListAllMyBuckets xml", e);
            } 
        } 
    } 
Full Snippet Info
7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java

Related Java 8 URLConnection Questions & Answers:

Other Java 8 URLConnection Examples

21:		            URL url = new URL(f_url[0]);
22:		            URLConnection conection = url.openConnection();
23:		            conection.connect();
24:		            // getting file length
25:		            int lenghtOfFile = conection.getContentLength();

Read More

98:		            URL url = new URL(f_url[0]);
99:		            URLConnection conection = url.openConnection();
100:		            conection.connect();
101:		            // this will be useful so that you can show a tipical 0-100% progress bar
102:		            int lenghtOfFile = conection.getContentLength();

Read More

See Code Examples for Java 8 URLConnection Methods: