使用HttpClient获得网页内容

一、需要的jar包



二、代码部分

package com.lei.httpclient;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class HttpClientTest {

	public static void main(String[] args) throws ClientProtocolException, IOException{

		httpGet("http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html");
	}

	public static String httpGet(String url) throws ClientProtocolException, IOException {
		String content = null;//网页内容

		int socketTimeout = 5000;//读取数据超时
		int connectTimeout = 5000;//链接超时

		RequestConfig config = RequestConfig.custom()
				.setConnectTimeout(connectTimeout)
				.setConnectionRequestTimeout(connectTimeout)
				.setSocketTimeout(socketTimeout).build();

		CloseableHttpClient httpClient = HttpClientBuilder.create()
				.setDefaultRequestConfig(config).build();

		HttpGet httpGet = new HttpGet(url);

		HttpResponse response = httpClient.execute(httpGet);

		HttpEntity entity = response.getEntity();
		if (entity!=null) {
			content = EntityUtils.toString(entity,"UTF-8");
			EntityUtils.consume(entity);//关闭内容流
		}
		//释放链接
		httpClient.close();

		return content;

	}

	public String httpPost() throws Exception {

		String content= null;
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();

		HttpPost httpost = new HttpPost("http://www.pibigstar.top");
		//设置参数
		List nvp = new ArrayList<>();
		nvp.add(new BasicNameValuePair("username", "admin"));
		nvp.add(new BasicNameValuePair("password", "123456"));
		//将参数提交到请求中 
		httpost.setEntity(new UrlEncodedFormEntity(nvp,HTTP.UTF_8));
		//执行请求
		HttpResponse response = httpClient.execute(httpost);
		HttpEntity entity = response.getEntity();
		if (entity!=null) {
			content = EntityUtils.toString(entity,"UTF-8");
			EntityUtils.consume(entity);//关闭内容流
		}
		//释放链接
		httpClient.close();

		return content;
	}
}



你可能感兴趣的:(爬虫相关)