Java调用第三方厂商提供了http接口
在多人开发项目中,我们经常要在自己程序内部(不是在jsp中)调用别人的http请求接口,主要通过流的方式进行调用
例如:http://www.baidu.com/query.jsp?param1=value2¶m2=value2
这个接口会返回xml格式的输出结果,例如:
<?xml version="1.0" encoding="UTF-8" ?>
<result>
<item id="0">
<id>20100318001</id>
<name>张三</name>
<sex>男</sex>
</item>
</result>
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.baidu.com/query.jsp?param1=value2¶m2=value2"); /* 网络的url地址 */
BufferedReader in = new BufferedReader(new InputStreamReader(yahoo.openStream(),"UTF-8"));
String inputLine;
StringBuffer sb = new StringBuffer();
while ((inputLine = in.readLine()) != null)
sb.append(inputLine);
in.close();
}
}