解析url 得到返回值,java

来源:百度知道 编辑:UC知道 时间:2024/05/19 00:17:23
小弟不太熟悉web service怎么用,比如我现在有个接口http://www.youdao.com/smartresult-xml/search.s?type=ip&q=127.0.0.1

地址栏中输入这个,IE会返回给一个XML文件,如:
<?xml version="1.0" encoding="gbk"?>
<smartresult>
<product type="ip">
<ip>127.0.0.1</ip>
<location>本机地址</location>
</product>
</smartresult>

现在我想做一个方法,通过有道给的这个接口来做得到城市所在地。

如:
public Vector getCountryCityByIp(String ip) {

log.info("ip::::" + ip);
String url = "";// 提供接口的地址
String soapaction = ""; // 域名,这是在server定义的
Service service = new Service();
String city = ..;// 这里返回所在城市

return city;
}

try{

// URL url=new URL("http://www.youdao.com/smartresult-xml/search.s?type=ip&q=58.214.5.162");
URL url=new URL("http://whois.pconline.com.cn/ip.jsp?ip="+ip);
HttpURLConnection connect=(HttpURLConnection)url.openConnection();
InputStream is = connect.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buff = new byte[256];
int rc = 0;
while ( (rc = is.read(buff, 0, 256)) > 0) {
outStream.write(buff, 0, rc);
}
byte[] b = outStream.toByteArray();
//关闭
outStream.close();