|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStream; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.net.HttpURLConnection; |
| 6 | +import java.net.MalformedURLException; |
| 7 | +import java.net.URL; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +/** |
| 13 | + * Created by Troy Liu on 2015,九月,17, 22:16. |
| 14 | + */ |
| 15 | +public class NetUtils { |
| 16 | + |
| 17 | + public static final int TIME_OUT = 1000; |
| 18 | + |
| 19 | + public static String getBundle(String urlMain) { |
| 20 | + StringBuilder stringBuilder = null; |
| 21 | + try { |
| 22 | + URL url = new URL(urlMain); |
| 23 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 24 | + InputStream inputStream; |
| 25 | + stringBuilder = new StringBuilder(); |
| 26 | + BufferedReader bufferedReader = null; |
| 27 | + if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { |
| 28 | + inputStream = connection.getInputStream(); |
| 29 | + bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); |
| 30 | + String line = null; |
| 31 | + while ((line = bufferedReader.readLine()) != null) { |
| 32 | + stringBuilder.append(line); |
| 33 | + } |
| 34 | + } |
| 35 | + bufferedReader.close(); |
| 36 | + connection.disconnect(); |
| 37 | + } catch (MalformedURLException e) { |
| 38 | + e.printStackTrace(); |
| 39 | + } catch (IOException e) { |
| 40 | + e.printStackTrace(); |
| 41 | + } |
| 42 | + return stringBuilder.toString(); |
| 43 | + } |
| 44 | + |
| 45 | + public static Map<String, String> getNotAvailableUrl(String urlMain) { |
| 46 | + Map<String, String> matterUrls = new HashMap<String, String>(); |
| 47 | + String response = getBundle(urlMain); |
| 48 | + List<String> urlList = UrlMatcher.getUrlStr(response); |
| 49 | + for (String url : urlList) { |
| 50 | + checkUrl(url, matterUrls); |
| 51 | + } |
| 52 | + return matterUrls; |
| 53 | + } |
| 54 | + |
| 55 | + private static void checkUrl(String url, Map<String, String> map) { |
| 56 | + int responseCode = 0; |
| 57 | + try { |
| 58 | + System.out.print("checking [" + url + "]"); |
| 59 | + URL tmp = new URL(url); |
| 60 | + HttpURLConnection connection = (HttpURLConnection) tmp.openConnection(); |
| 61 | + 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"); |
| 62 | + connection.setRequestMethod("GET"); |
| 63 | + connection.setConnectTimeout(TIME_OUT); |
| 64 | + responseCode = connection.getResponseCode(); |
| 65 | + if (responseCode == HttpURLConnection.HTTP_OK) { |
| 66 | + System.out.println("----->[OK]"); |
| 67 | + } else { |
| 68 | + System.out.println("----->[BAD]" + "; Code--->" + responseCode); |
| 69 | + map.put(url, "BAD"); |
| 70 | + } |
| 71 | + connection.disconnect(); |
| 72 | + } catch (MalformedURLException e) { |
| 73 | + e.printStackTrace(); |
| 74 | + } catch (IOException e) { |
| 75 | + System.out.println("[Time Out]"); |
| 76 | + map.put(url, "TIMEOUT"); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments