Selected Reading

Java - URLConnection toString() Method with Examples



Description

The Java URLConnection toString() method returns a String representation of this URL connection.

Declaration

Following is the declaration for java.net.URLConnection.toString() method:

public String toString()

Parameters

NA

Return Value

a string representation of this URLConnection.

Exception

NA

Example 1

The following example shows the usage of Java URLConnection toString() method for a valid url with https protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using toString() method, we're getting the string representation and printed the same −

package com.tutorialspoint;

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

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com");
         URLConnection urlConnection = url.openConnection();

         System.out.println(urlConnection.toString());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

sun.net.www.protocol.https.DelegateHttpsURLConnection:https://www.tutorialspoint.com

Example 2

The following example shows the usage of Java URLConnection toString() method for a valid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using toString() method, we're getting the string representation and printed the same −

package com.tutorialspoint;

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

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.tutorialspoint.com");
         URLConnection urlConnection = url.openConnection();

         System.out.println(urlConnection.toString());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

sun.net.www.protocol.http.HttpURLConnection:http://www.tutorialspoint.com

Example 3

The following example shows the usage of Java URLConnection toString() method for a valid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using toString() method, we're getting the string representation and printed the same −

package com.tutorialspoint;

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

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.google.com");
         URLConnection urlConnection = url.openConnection();

         System.out.println(urlConnection.toString());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

sun.net.www.protocol.http.HttpURLConnection:http://www.google.com
java_urlconnection.htm
Advertisements