HttpClient的get应用

来源:互联网 发布:mysql 数据库合并 编辑:程序博客网 时间:2024/04/29 18:20

public static String getHtml()
{
 StringBuffer sb = new StringBuffer();
 HttpClient client = new DefaultHttpClient();
 HttpGet httpGet = new HttpGet("http://localhost:8080/atur");
 try {
  HttpResponse response = client.execute(httpGet);
  HttpEntity entry = response.getEntity();
  
  if(entry  != null)
  {
   InputStreamReader ir = new InputStreamReader(entry.getContent());
   BufferedReader in = new BufferedReader(ir);
   String str = null;
   while((str = in.readLine()) != null)
   {
    sb.append(str.trim());
   }
   in.close();
  }
  
 } catch (ClientProtocolException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 System.out.println(sb.toString());
 return sb.toString();
}

导入:commons-logging-1.1.jar,httpclient-4.0-beta2.jar和httpcore-4.1-alpha1.jar架包