java如何用httpclient从webservice获取数据
1个回答
展开全部
下面是一个获取天气的例子:
import java.io.*;
import java.net.*;
public class test{
public static void main(String[] args) throws Exception {
URL url = new URL("http://www.webservicex.net/globalweather.asmx/GetWeather?CityName=guangzhou&CountryName=china");//要调用的url
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");//设置get方式获取数据
if (conn.getResponseCode() == 200) {//如果连接成功
BufferedReader br = new BufferedReader(new InputStreamReader(conn
.getInputStream()));//创建流
String lines = null;
while ((lines = br.readLine()) != null) {
System.out.println(lines.replaceAll("&[lg]t;",""));//将读取到的数据打印
}
br.close();
}
conn.disconnect();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询