httpclient的get方法的官方API

 
引用来自:http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/HttpClient.html

HttpClient httpclient = new DefaultHttpClient();

 // Prepare a request object
 HttpGet httpget = new HttpGet("http://www.apache.org/");

 // Execute the request
 HttpResponse response = httpclient.execute(httpget);

 // Examine the response status
 System.out.println(response.getStatusLine());

 // Get hold of the response entity
 HttpEntity entity = response.getEntity();

 // If the response does not enclose an entity, there is no need
 // to worry about connection release
 if (entity != null) {
     InputStream instream = entity.getContent();
     try {

         BufferedReader reader = new BufferedReader(
                 new InputStreamReader(instream));
         // do something useful with the response
         System.out.println(reader.readLine());
   /** self write arsenic pill add
 String result = "";
 String line = "";
 while((line=reader.readLine)!=null){
           //result就为所返回的内容
 result = result+line;
 }   

   */


     } catch (IOException ex) {

         // In case of an IOException the connection will be released
         // back to the connection manager automatically
         throw ex;

     } catch (RuntimeException ex) {

         // In case of an unexpected exception you may want to abort
         // the HTTP request in order to shut down the underlying
         // connection and release it back to the connection manager.
         httpget.abort();
         throw ex;

     } finally {

         // Closing the input stream will trigger connection release
         instream.close();

     }

     // When HttpClient instance is no longer needed,
     // shut down the connection manager to ensure
     // immediate deallocation of all system resources
     httpclient.getConnectionManager().shutdown();
 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值