在java中打开另一个http连接

在java中打开另一个http连接,java,Java,我尝试连接到一个网站,从中获取一些URL,然后连接到这些URL,获取一些信息。这是我的密码 URL url = new URL("http://www.nchmf.gov.vn/web/vi-VN/62/23/44/map/Default.aspx"); URLConnection con = url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(con.get

我尝试连接到一个网站,从中获取一些URL,然后连接到这些URL,获取一些信息。这是我的密码

    URL url = new URL("http://www.nchmf.gov.vn/web/vi-VN/62/23/44/map/Default.aspx");
    URLConnection con = url.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

    String l;
    String Area;

    Pattern Province = Pattern.compile("Thị xã/T.Phố :");
    Pattern City = Pattern.compile("<option(.*?)ue=\"(.*?)\">(.*?)</option>");


    while ((l=in.readLine())!=null) {
        Matcher ProvinceFound = Province.matcher(l);
        if (ProvinceFound.find()) {
            while((l=in.readLine())!=null
                  && !l.contains("</select></span>")){
                Matcher CityCode = City.matcher(l);
                if(CityCode.find()){
                    if(!"0".equals(CityCode.group(2))){
                        URL url1 = new URL("http://www.nchmf.gov.vn/web/vi-VN/62/23/44/map/Default.aspx");
                        URLConnection con1 = url1.openConnection();
                        BufferedReader in1 = new BufferedReader(new InputStreamReader(con1.getInputStream()));

                        while((Area=in1.readLine())!=null){
                            System.out.println(Area);
                            break;
                        }
                    }

                }
            }
        }
    }
} 
URL=新URL(“http://www.nchmf.gov.vn/web/vi-VN/62/23/44/map/Default.aspx");
URLConnection con=url.openConnection();
BufferedReader in=新的BufferedReader(新的InputStreamReader(con.getInputStream());
字符串l;
串区;
Pattern Province=Pattern.compile(“Thị xã/T.Phố :");
Pattern City=Pattern.compile((*?);
而((l=in.readLine())!=null){
匹配器ProvinceFound=省。匹配器(l);
if(ProvinceFound.find()){
而((l=in.readLine())!=null
&&!l.contains(“”){
Matcher CityCode=City.Matcher(l);
if(CityCode.find()){
如果(!“0.”等于(城市代码组(2))){
URL url1=新URL(“http://www.nchmf.gov.vn/web/vi-VN/62/23/44/map/Default.aspx");
URLConnection con1=url1.openConnection();
BufferedReader in1=新的BufferedReader(新的InputStreamReader(con1.getInputStream());
而((Area=in1.readLine())!=null){
系统输出打印LN(面积);
打破
}
}
}
}
}
}
} 
我得到的结果只是空行。如果我在第一个连接上输入“System.out.println(l);”,它仍然会打印一些东西,所以我认为问题出在第二个连接上


谁能告诉我我的代码有什么问题吗,非常感谢。

我猜您无法读取第二个URL,因为您正在阻止第一个URL。两个建议:

  • 为要读取的每个新URL启动一个新线程
  • 读取第一个URL中的所有数据,并将要处理的链接添加到列表中。然后,当您完成主URL的读取后,您可以一次读取另一个URL中的一个

  • 遵循标准Java命名约定。变量(省、市、ProvinceFound…)不应以大写字符开头。是否确定代码正在到达第二个连接?请调试它。