1
/**
2
* 判斷鏈接是否有效
3
*/
4
public static boolean isValidUrl(String strUrl) {
5
try {
6
URL url = new URL(strUrl);
7
HttpURLConnection huConn = (HttpURLConnection)url.openConnection();
8
huConn.setRequestMethod("HEAD");
9
String strMessage = huConn.getResponseMessage();
10
//if (strMessage.compareTo("Not Found") == 0) {
11
if (strMessage.equals("OK")) {
12
huConn.disconnect();
13
return true;
14
}
15
} catch (Exception e) {
16
return false;
17
}
18
return false;
19
}
第二個(gè),通過返回頁面的內(nèi)容來判斷
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

1
/**
2
* Get Remote Page content
3
* @param strURL
4
* @return String remote page content
5
* @throws Exception
6
*/
7
private String getRemotePage(String strURL) throws Exception{
8
if (!validation(strURL)) {
9
System.out.println(validation(strURL));
10
throw (new Exception("Error url"));
11
}
12
String strPageContent="";
13
StringBuffer strBuffer=new StringBuffer();
14
url=new URL(strURL);
15
huc=(HttpURLConnection)url.openConnection();
16
huc.connect();
17
BufferedReader reader=new BufferedReader(
18
new java.io.InputStreamReader(huc.getInputStream()));
19
String strLineContent="";
20
while((strLineContent=reader.readLine())!=null){
21
strBuffer.append(strLineContent);
22
}
23
strPageContent=strBuffer.toString();
24
return strPageContent;
25
}
以上都是從網(wǎng)上收集而來。
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25
