问题描述
远程地址: http://192.168.2.1:1080/web_xml_logon/user_logon.xml?logonName=logonName&passWord=passWord响应XML: <?xml version="1.0" encoding="gb2312" ?> <logon_success <?xml version="1.0" encoding="GBK" ?> <logon_successsessionId="0000000000000000000000000000007"userId="0000000000000000000000000000011" /> ============= 我想在后台取得http://192.168.2.1:1080/web_xml_logon/user_logon.xml?logonName=logonName&passWord=passWord的表内容, 然后取得logonName这个属性于后台使用? 怎么做呢?最好有个例子。 thanks! 问题补充:牟盖东 写道
解决方案
import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.util.Scanner;/*远程地址: http://192.168.2.1:1080/web_xml_logon/user_logon.xml?logonName=logonName&passWord=passWord 响应XML: <?xml version="1.0" encoding="gb2312" ?> <logon_success <?xml version="1.0" encoding="GBK" ?> <logon_success sessionId="0000000000000000000000000000007" userId="0000000000000000000000000000011" /> ============= 我想在后台取得http://192.168.2.1:1080/web_xml_logon/user_logon.xml?logonName=logonName&passWord=passWord的表内容, 然后取得logonName这个属性于后台使用? */public class XmlDownload {// 下面是你试图打开的网站地址private String urlTarget;//构造方法public XmlDownload(String urlTarget){this.urlTarget=urlTarget;}//下载函数public void download() {try {URL url = new URL(this.urlTarget);URLConnection conn = url.openConnection();// 设置超时时长。conn.setConnectTimeout(5000);Scanner scan = new Scanner(conn.getInputStream());while (scan.hasNextLine()) {String line = scan.nextLine();// 在控制台输出网页资源内容System.out.println(line);}} catch (MalformedURLException ex) {System.out.println(this.urlTarget + "不是有效的网站地址!");} catch (IOException ex) {ex.printStackTrace();}}//测试代码public static void main(String[] args) {XmlDownload wd=new XmlDownload("http://www.baidu.com/");wd.download();}}