URLConnection connect - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for URLConnection connect method.

This code example shows how to use the following methods:getResponseCode, getResponseMessage, setRequestMethod, connect, getOutputStream
 
  private String makeRequest(String urlFragment, String method, String body) throws IOException {
    HttpsURLConnection connection = getConnection(urlFragment, method);
 
    if (body != null) {
      connection.setDoOutput(true);
    } 
 
    connection.connect();
 
    if (body != null) {
      Log.w("GcmSocket", method +  "  --  " + body);
      OutputStream out = connection.getOutputStream();
      out.write(body.getBytes());
      out.close();
    } 
 
    if (connection.getResponseCode() != 200) {
      throw new IOException("Bad response: " + connection.getResponseCode() + " " + connection.getResponseMessage());
    } 

8
CodeRank
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());
8
CodeRank
This code example shows how to use the following methods:connect, getInputStream, setDoInput
This code example shows how to use the following methods:addRequestProperty, connect, setConnectTimeout, setReadTimeout, setRequestProperty
        URLConnection connection = openConnection(url);
        if(connection instanceof HttpURLConnection){
            ((HttpURLConnection) connection).setRequestMethod("GET");
        } 
 
        connection.connect();
 
        return new SimpleHTTPResponse(connection);
    } 
 
    /** 
     * @see org.geotools.data.ows.HTTPClient#post(java.net.URL, java.io.InputStream, 
     *      java.lang.String) 
     */ 
    public HTTPResponse post(final URL url, final InputStream postContent,
            final String postContentType) throws IOException {
 
        URLConnection connection = openConnection(url);
        if (connection instanceof HttpURLConnection) {
            ((HttpURLConnection) connection).setRequestMethod("POST");

7
CodeRank
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 { 
8
CodeRank
This code example shows how to use the following methods:connect, getInputStream, setDoInput
            URL url;
 
            url = new URL( urlstr );
 
            HttpURLConnection c = ( HttpURLConnection ) url.openConnection();
            c.setDoInput( true );
            c.connect();
            InputStream is = c.getInputStream();
            Bitmap img;
            img = BitmapFactory.decodeStream( is );
            return img;
        } 
        catch ( MalformedURLException e )
        { 
            Log.d( "RemoteImageHandler", "RemoteImageWorker passed invalid URL: " + urlstr );
        } 
        catch ( IOException e )
        { 
            Log.d( "RemoteImageHandler", "RemoteImageWorker IO exception: " + e );
        } 
7
CodeRank
This code example shows how to use the following methods:getJarFile, connect, setAllowUserInteraction, setDoInput, setDoOutput
log.debug("JarHolder: attempting to connect to " + urlpath);
            } 
            URL url = new URL( urlpath );
            conn = (JarURLConnection) url.openConnection();
            conn.setAllowUserInteraction(false);
            conn.setDoInput(true);
            conn.setDoOutput(false);
            conn.connect();
            theJar = conn.getJarFile();
        } 
        catch (IOException ioe)
        { 
            String msg = "JarHolder: error establishing connection to JAR at \""
                         + urlpath + "\"";
            log.error(msg, ioe);
            throw new VelocityException(msg, ioe);
        } 
    } 
 
7
CodeRank
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
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"));
7
CodeRank
This code example shows how to use the following methods:disconnect, setRequestMethod, connect, getInputStream, setConnectTimeout
6
CodeRank

Related URLConnection connect Questions & Answers:

Other URLConnection connect Examples

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();
103:

Read More

22:		            URLConnection conection = url.openConnection();
23:		            conection.connect();
24:		            // getting file length
25:		            int lenghtOfFile = conection.getContentLength();
26:

Read More

connect Method Overview
Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.
See Code Examples for other Java 8 URLConnection Methods: