输入URL,用户名,密码后,获取返回页面的数据

此方法是输入URL,用户名,密码后,获取返回页面的数据。

 1 public static String getInternetIP(String urlStr, String username, String password) {
 2 
 3         URL url = null;
 4 
 5 
 6         String result = "";
 7 
 8         Authenticator.setDefault(new TLR402Authenticatione(username, password));
 9         try {
10             url = new URL(urlStr);
11 
12             InputStream content = (InputStream) url.getContent();
13             BufferedReader in = new BufferedReader(new InputStreamReader(content));
14             int i = 0;
15             while ((result = in.readLine()) != null && i < 52) {
16                 i++;
17             }
18             in.close();
19         } catch (Exception e) {
20             e.printStackTrace();
21 
22         }
23 
24         int size = result.indexOf("</td><td>");
25         if (size > -1) {
26             result = result.substring(size + "</td><td>".length());
27             size = result.lastIndexOf("</td>");
28             if (size > -1) {
29                 result = result.substring(0, size);
30             }
31         }
32 
33         return result;
34 
35     }

TLR402Authenticatione是设置网络校验权限的数据类。

 1 final static class TLR402Authenticatione extends Authenticator {
 2         private String username;
 3         private String password;
 4 
 5         TLR402Authenticatione(String _username, String _password) {
 6             username = _username;
 7             password = _password;
 8         }
 9 
10         protected PasswordAuthentication getPasswordAuthentication() {
11             return new PasswordAuthentication(this.username, password.toCharArray());
12         }
13 }

 

 

 

posted @ 2012-05-15 12:11  崔宏  阅读(421)  评论(0)    收藏  举报