您的位置:首页 > 理论基础 > 计算机网络

从网络上获取数据

2011-10-10 14:28 316 查看
一、 get方法

/**
* get方法从网络上获取数据
*/
public static String getData(String uri){
String result = "";
//生成一个请求对象
HttpGet httpGet = new HttpGet(uri);
//生成一个Http客户端对象
HttpClient httpClient = new DefaultHttpClient();

HttpResponse response = null;
HttpEntity httpEntity;
InputStream inputStream;
try {
response = httpClient.execute(httpGet);
httpEntity = response.getEntity();
inputStream = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while((line = reader.readLine()) != null){
result = result + line;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: