要求:访问指定的网址xml,解析并读取里面的(天文台)信息;
网址: http://xml2.weather.gov.hk/V2/bulletins/CurrentWeather_iso-8859-1.xml
demo网址: http://demo.xml.weather.gov.hk/V2/bulletins/CurrentWeather_iso-8859-1.xml.1
现在遇到问题,此网址当用IE去打开时,会弹出对话框需要输入用户名和密码,才能进去;
那么我在用Document解析时,报错了“401错误”,我想是如何在代码里实现用户的登录,后在解析?
- Java code
try { String url = "http://xml2.weather.gov.hk/V2/bulletins/CurrentWeather_iso-8859-1.xml"; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(url); //报错}catch(Exception e){}
------解决方案--------------------------------------------------------
网址: http://xml2.weather.gov.hk/V2/bulletins/CurrentWeather_iso-8859-1.xml
这个网址打开是需要输入用户名密码的:打开环境:Safari
demo网址: http://demo.xml.weather.gov.hk/V2/bulletins/CurrentWeather_iso-8859-1.xml.1
这个网址打开不需要输入用户名与密码。