44import java .io .InputStreamReader ;
55import java .net .HttpURLConnection ;
66import java .net .MalformedURLException ;
7+ import java .net .ProtocolException ;
78import java .net .URL ;
89import java .util .HashMap ;
910import java .util .List ;
@@ -27,11 +28,16 @@ public static String getBundle(String urlMain) {
2728 try {
2829 URL url = new URL (urlMain );
2930 HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
31+ connection .setRequestProperty ("Content-Type" , "text/html" );
32+ connection .setRequestProperty ("Accept-Charset" , "utf-8" );
33+ connection .setRequestProperty ("Content-Type" , "utf-8" );
34+ connection .setRequestProperty ("Charset" , "utf-8" );
35+ connection .setRequestMethod ("GET" );
3036 InputStream inputStream ;
3137 BufferedReader bufferedReader = null ;
3238 if (connection .getResponseCode () == HttpURLConnection .HTTP_OK ) {
3339 inputStream = connection .getInputStream ();
34- bufferedReader = new BufferedReader (new InputStreamReader (inputStream ));
40+ bufferedReader = new BufferedReader (new InputStreamReader (inputStream , "UTF-8" ));
3541 String line = null ;
3642 while ((line = bufferedReader .readLine ()) != null ) {
3743 stringBuilder .append (line );
@@ -58,7 +64,7 @@ public static Map<String, String> getNotAvailableUrl(String urlMain) {
5864 threadPool .execute (new ItemThread (url , new ICallback () {
5965 @ Override
6066 public void finish (int code ) {
61- matterUrls .put (url , "BAD" );
67+ matterUrls .put (url , "BAD" + "[Code: " + code + "]" );
6268 }
6369
6470 @ Override
@@ -75,6 +81,7 @@ public void timeout() {
7581 }
7682 }
7783
84+
7885}
7986
8087class ItemThread implements Runnable {
@@ -89,25 +96,32 @@ public ItemThread(String url, ICallback callback) {
8996
9097 @ Override
9198 public void run () {
99+ HttpURLConnection connection = null ;
92100 try {
93101 URL tmp = new URL (url );
94- HttpURLConnection connection = (HttpURLConnection ) tmp .openConnection ();
102+ connection = (HttpURLConnection ) tmp .openConnection ();
95103 connection .setRequestProperty ("User-Agent" , "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36" );
96104 connection .setRequestMethod ("GET" );
97- connection .setConnectTimeout (1000 );
105+ connection .setReadTimeout (10000 );
106+ connection .setConnectTimeout (10000 );
98107 int responseCode = connection .getResponseCode ();
99108 if (responseCode == HttpURLConnection .HTTP_OK ) {
100109 System .out .println ("checking [" + url + "]" + "----->[OK]" );
101110 } else {
102111 System .out .println ("checking [" + url + "]" + "----->[BAD]" + "; Code--->" + responseCode );
103112 callback .finish (responseCode );
104113 }
105- connection .disconnect ();
106114 } catch (MalformedURLException e ) {
107115 e .printStackTrace ();
116+ } catch (ProtocolException e ) {
117+ e .printStackTrace ();
108118 } catch (IOException e ) {
109- System .out .println ("checking [" + url + "]" + "[Time Out]" );
119+ System .out .println ("checking [" + url + "]" + e . getMessage () );
110120 callback .timeout ();
121+ } finally {
122+ if (connection != null ) {
123+ connection .disconnect ();
124+ }
111125 }
112126 }
113127}
0 commit comments