Java: Obtenir les information de connexion avec URLConnection

Author:

 int,string,char,static, url, socket, url, socket, java
Download

package je3.net;
import java.net.*;
import java.io.*;
import java.util.Date;
 
public class URLInfo {
 
    public static void printinfo(URL url) throws IOException {
        URLConnection c = url.openConnection( );  
        c.connect( );                            
 
        System.out.println("  Type de contenu: " + c.getContentType( ));
        System.out.println("  Encodage: " + c.getContentEncoding( ));
        System.out.println("  Taille de contenu: " + c.getContentLength( ));
        System.out.println("  Date: " + new Date(c.getDate( )));
        System.out.println("  Dernière modification: " +new Date(c.getLastModified( )));
        System.out.println("  Expiration: " + new Date(c.getExpiration( )));
 
        // Pour les connexions HTTP.
        if (c instanceof HttpURLConnection) {
            HttpURLConnection h = (HttpURLConnection) c;
            System.out.println("  Méthode POST/GET: " + h.getRequestMethod( ));
            System.out.println("  Réponse du serveur HTTP: " +h.getResponseMessage( ));
            System.out.println("  Code d'erreur: " + h.getResponseCode( ));
        }
    }
 
 
    public static void main(String[  ] args)throws Exception {
       printinfo(new URL("http://www.mesexemples.com/")); 
 
    }
}