`

使用HttpComponents获取整个页面的内容

    博客分类:
  • Java
阅读更多

commons-httpclient已经不再更新了,

httpcomponents是commons-httpclient后继项目。

本方法的目的是使用httpcomponents-client-4.0.1获取整个页面的内容

稍微修改了一下examples中的ClientAbortMethod

【添加代码已用注释标注,就是读个输入流,也没啥的】

java 写道
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* .
*
*/

package org.apache.http.examples.client;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

/**
* This example demonstrates how to abort an HTTP method before its normal completion.
*/
public class MyClientAbortMethod {

public final static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();

HttpGet httpget = new HttpGet("http://www.apache.org/");

System.out.println("executing request " + httpget.getURI());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();

System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());

//start 读取整个页面内容
InputStream is = entity.getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line+"\n");
}
//end 读取整个页面内容



System.out.println(buffer.toString());
}
System.out.println("----------------------------------------");

// Do not feel like reading the response body
// Call abort on the request object
httpget.abort();

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

}


 
0
1
分享到:
评论

相关推荐

    httpcomponents

    httpcomponents-jar包;httpcomponents-jar包;httpcomponents-jar包;httpcomponents-jar包;httpcomponents-jar包;httpcomponents-jar包

    httpcomponents-client-4.5.5 jar包

    2. 配置请求,如设置URL、方法类型、请求头和实体内容。 3. 执行请求并获取响应,处理响应状态码和响应体。 示例代码如下: ```java CloseableHttpClient httpClient = HttpClients.createDefault(); ...

    httpcomponents-client-4.2.5-bin

    1. **网络爬虫**:在构建网络爬虫时,HTTPComponents Client可以帮助开发者高效地获取网页内容,处理登录、cookie、session等复杂情况。 2. **API接口调用**:对于需要调用RESTful API的应用,HTTPComponents ...

    httpcomponents-client包下载

    首先,我们来看HTTPComponents Client的核心组件——HttpClient。HttpClient是一个功能丰富的HTTP客户端,可以用来执行各种HTTP方法,如GET、POST、PUT等。它支持基本认证、代理设置、重试策略、连接管理和超时设置...

    httpcomponents-core--4.3.3-bin.rar

    - 处理响应:通过HttpResponse获取响应状态码和头部信息,以及通过HttpEntity获取响应实体内容。 总的来说,HttpComponents Core 4.3.3是Java和Android开发中处理HTTP请求的重要库,其强大的功能和灵活的API使其...

    httpcomponents.jar

    在使用LLRC时,`httpcomponents.jar`扮演了关键的角色。 `httpcomponents.jar`是Apache HttpComponents项目的一部分,主要包含两个核心组件:HttpClient和HttpCore。HttpClient是一个功能丰富的HTTP客户端编程工具...

    HttpComponents

    在使用HttpComponents时,首先需要创建一个HttpClient实例,然后配置必要的参数,如连接超时、重试策略等。接着,通过HttpRequest对象设置请求头和主体,然后调用HttpClient的execute方法发送请求。收到响应后,可以...

    使用apache httpcomponents 模拟html表单上传文件

    7. **处理响应**:从`HttpResponse`中获取状态码和响应内容。如果是成功状态,可以使用`EntityUtils.toString()`方法读取响应体。 8. **关闭资源**:执行完请求后,记得关闭HttpClient以释放资源。 在实际应用中,...

    httpcomponents-core-4.1.3-src.zip

    通过HttpClient,开发者可以方便地构建复杂的HTTP请求,如设置请求头、携带实体内容、使用Cookie、处理重定向等。 四、HttpCore NIO HttpCore NIO是HttpComponents Core的非阻塞I/O模块,基于Java NIO(Non-...

    httpcomponents jar包

    《深入理解HTTPComponents Jar包:构建高效网络通信的利器》 HTTPComponents是Apache软件基金会开发的一个Java库,它为开发者提供了强大的HTTP客户端和服务器端通信工具。这个库的核心在于其高度模块化的架构,使得...

    httpcomponents-client-4.0.1

    执行结果会返回一个HttpResponse对象,从中你可以获取响应的状态码、头信息和实体内容。 7. **处理响应**:HttpResponse对象的getStatusLine方法获取状态码,getEntity方法获取响应的HttpEntity。如果响应包含实体...

    HttpComponents.zip

    2. **爬虫开发**:在构建网络爬虫时,HttpClient可以用于获取网页内容,处理登录、cookie,以及模拟用户行为。 3. **自动化测试**:在自动化测试框架中,HttpClient可以用于模拟用户请求,验证服务端的响应,确保...

    httpcomponents 学习

    4. 执行请求:使用HttpClient的execute方法发送请求,并获取HttpResponse。 5. 处理响应:从HttpResponse中读取状态码、头部信息和响应体。 二、连接管理和重试策略 HttpClient提供了连接池管理器...

    httpcomponents-core-4.2.1 httpcomponents-client-4.2 httpcore4.2

    这些基本的示例展示了如何使用HttpComponents来执行HTTP请求并处理响应。在实际应用中,你可以根据需要配置HttpClient,例如设置超时、添加自定义拦截器等。 总之,Apache HttpComponents是一个强大的工具集,广泛...

    httpcomponents-client-4.5.12.zip

    1. **Web服务调用**:通过HttpClient,开发者可以轻松地向RESTful API发送请求,获取或更新数据。 2. **网页抓取**:HttpClient是网络爬虫的重要组成部分,可以高效地抓取和解析网页内容。 3. **代理服务器支持**...

    httpcomponents-client-4.5.6.rar

    标题中的"httpcomponents-client-4.5.6.rar"是一个压缩包文件,它是Apache HttpComponents项目的客户端组件的一个版本,具体是4.5.6版。HttpComponents是Java领域中用于处理HTTP协议的重要库,它提供了对HTTP协议的...

    httpcomponents-client-4.5.1

    在HttpComponents客户端4.5.1版本中,主要包含以下几个核心知识点: 1. **HttpClient**:HttpClient是HttpComponents库的核心组件,它提供了全面的HTTP方法支持,如GET、POST、PUT、DELETE等。HttpClient允许开发者...

    httpcomponents-client所有文件

    在本篇文章中,我们将深入探讨HTTPComponents客户端库的核心特性、主要组件以及如何使用。 一、核心特性 1. **性能优化**:HTTPComponents客户端库通过高效的连接管理策略和缓存机制,提高了HTTP请求的处理速度,...

    httpcomponents-client-4.5.3所需jar

    标题中的"httpcomponents-client-4.5.3所需jar"指的是Apache HttpComponents客户端库的一个特定版本,4.5.3。这个库是Java开发者用来构建HTTP客户端应用的重要工具,它提供了全面的功能来处理HTTP协议,包括GET、...

Global site tag (gtag.js) - Google Analytics