通过java.net.url提取网页数据但是一直在执行却始终执行不完?

Benjamin_whx 2014-02-18 10:04:46

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


//TODO:根据ip获得域名id
public class getDomainMessage {
private final String IP_SEARCH_URL = "http://bgp.he.net/" ;
DataBaseConn dbc = new DataBaseConn() ;
public void getIpInfo(String ip){
URL url ;
HttpURLConnection conn ;
int responseCode ;
BufferedReader reader ;
String txt = "" ;
String line = "" ;
String errorRegex = "<div id='error' class='tabdata'>(.*?)</div>" ;
String correctRegex = "<a href=\"/net(.*?)>(.*?)</a>" ;
List<String> list = new ArrayList<String>() ;
try {
url = new URL(IP_SEARCH_URL + "ip/" + ip) ;
conn = (HttpURLConnection)url.openConnection() ;
responseCode = conn.getResponseCode() ;
if(responseCode==200){
reader = new BufferedReader(new InputStreamReader(conn.getInputStream())) ;
txt = reader.readLine() ;
//TODO:获取到bgp.he.net网页中对应ip出来的内容line
while(txt!=null){
line += txt ;
txt = reader.readLine() ;
}
Pattern pattern = Pattern.compile(errorRegex) ;
Matcher matcher = pattern.matcher(line) ;
Pattern pattern2 = Pattern.compile(correctRegex) ;
Matcher matcher2 = pattern2.matcher(line) ;
while(matcher.find()){
System.out.println("该网页没有dns");
}
while(matcher2.find()){
getNetMessage(matcher2.group(2).trim()) ;
//list.add(matcher2.group(2).trim()) ;
}
//TODO:遍历ip,调用getNetMessage方法对传进去的ip操作得到dns
/*for(String ss:list){
//System.out.println(ss);
getNetMessage(ss) ;
}*/
}else{
System.out.println("获取不到网页的源码,服务器响应代码为:"+responseCode);
}
} catch (Exception e) {
e.printStackTrace() ;
}finally{
System.out.println("执行完毕");
}
}
//TODO:根据域名ip获得域名
public void getNetMessage(String str){
URL url ;
HttpURLConnection conn ;
int responseCode ;
BufferedReader reader ;
String htmlTxt = "" ;
String result = "" ;
try {
url = new URL("http://bgp.he.net/net/" + str) ;
conn = (HttpURLConnection)url.openConnection() ;
responseCode = conn.getResponseCode() ;
if(responseCode==200){
reader = new BufferedReader(new InputStreamReader(conn.getInputStream())) ;
htmlTxt = reader.readLine() ;
while(htmlTxt!=null){
result += htmlTxt ;
htmlTxt = reader.readLine() ;
}
System.out.println(result);
}else{
System.out.println("获取不到网页的源码,服务器响应代码为:"+responseCode);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace() ;
}
}
public static void main(String[] args) {
new getDomainMessage().getIpInfo("27.115.0.0") ;
}
}
为什么我的程序连接url到指定网页时,程序不报错,也不执行完,就是空白的控制台,始终拿不到数据,怎么处理啊,急死我了!
...全文
256 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Benjamin_whx 2014-02-19
  • 打赏
  • 举报
回复
我超时设置了,网站也存在,就是10次1次能快速访问,很多次都访问的特别慢,我要拿几十万条数据区数据库怎么解决啊
可乐罐 2014-02-18
  • 打赏
  • 举报
回复
首先Timeout的超时时间设置了没? 然后,你确定你要访问的这个http://bgp.he.net/net/27.115.0.0是存在的?

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧