Changeset 404:c71b73a9271b

Show
Ignore:
Timestamp:
09/17/09 10:36:43 (4 years ago)
Author:
tgambet
Branch:
default
convert_revision:
svn:cdcfb263-7567-472c-a848-e2c2df3466e7/trunk@405
Message:

test connection exceptions

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/org/w3c/unicorn/Test.java

    r223 r404  
    11package org.w3c.unicorn; 
    22 
    3 import java.util.Locale; 
     3import java.io.FileNotFoundException; 
     4import java.io.IOException; 
     5import java.net.ConnectException; 
     6import java.net.HttpURLConnection; 
     7import java.net.MalformedURLException; 
     8import java.net.SocketTimeoutException; 
     9import java.net.URL; 
     10import java.net.URLConnection; 
     11import java.net.UnknownHostException; 
     12 
     13import javax.net.ssl.SSLException; 
     14 
     15//import sun.net.www.protocol.http.HttpURLConnection; 
     16 
     17 
    418 
    519public class Test { 
     
    721        /** 
    822         * @param args 
     23         * @throws MalformedURLException  
    924         */ 
    1025        public static void main(String[] args) { 
     
    2742             //replaceAll(repl)*/ 
    2843                 
    29                 Locale loc = new Locale("sssqn"); 
    30                  
    31                 System.out.println("z" + loc.getLanguage()); 
     44                URL docUrl = null; 
     45                try { 
     46                        docUrl = new URL("http://www.w3.org:9788"); 
     47                        //docUrl = new URL("http://qa-dev.w3.org/app"); 
     48                        //docUrl = new URL("http://www.w3.org/Team"); 
     49                        //docUrl = new URL("http://snowball.w3.org/unicorn/observe?ucn_uri=beta.w3.org&ucn_task=conformance"); 
     50                        //System.out.println(docUrl.getContent().toString()); 
     51                        //System.out.println(docUrl2.getContent().toString().length()); 
     52                        URLConnection con = docUrl.openConnection(); 
     53                        System.out.println(con.getConnectTimeout()); 
     54                        con.setConnectTimeout(1000); 
     55                        con.setReadTimeout(1000); 
     56                        con.getContent(); 
     57                } catch (MalformedURLException e) { 
     58                        e.printStackTrace(); 
     59                } catch (ConnectException e) { 
     60                        e.printStackTrace(); 
     61                } catch (FileNotFoundException e) { 
     62                        e.printStackTrace(); 
     63                } catch (UnknownHostException e) {  
     64                        e.printStackTrace(); 
     65                } catch (SSLException e) { 
     66                        e.printStackTrace(); 
     67                } catch (SocketTimeoutException e) { 
     68                        e.printStackTrace(); 
     69                        if (e.getMessage().contains("Read timed out")) 
     70                                System.out.println("a"); 
     71                        if (e.getMessage().contains("connect timed out")) 
     72                                System.out.println("b"); 
     73                } catch (IOException e) { 
     74                        try { 
     75                                int responseCode; 
     76                                if (docUrl != null) { 
     77                                        responseCode = ((HttpURLConnection) docUrl.openConnection()).getResponseCode(); 
     78                                        switch (responseCode) { 
     79                                        case HttpURLConnection.HTTP_UNAUTHORIZED: 
     80                                                System.out.println("HTTP_UNAUTHORIZED"); 
     81                                                break; 
     82                                        } 
     83                                } 
     84                        } catch (Exception e2) { 
     85                                System.out.println("e2"); 
     86                                e2.printStackTrace(); 
     87                        } 
     88                        e.printStackTrace(); 
     89                } 
    3290        } 
    3391