httpclient模拟浏览器

package com.java.httpclient;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
 * HttpClient设置请求头消息User-Agent模拟浏览器 setheader key value来设置
 * @author nidegui
 *
 */
public class Helloword3 {
	public static void main(String[] args) throws IOException {
		CloseableHttpClient  httpclient=HttpClients.createDefault(); //创建浏览器实列
		HttpGet httpget=new HttpGet("http://www.tuicool.com"); //创建get实列
		httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
		CloseableHttpResponse response=null;
		response=httpclient.execute(httpget);
        HttpEntity entity=response.getEntity();
		System.out.println("输出实体:"+EntityUtils.toString(entity,"utf-8"));
		response.close();
		httpclient.close();
	}
}

  

posted on 2019-05-20 16:12  nidegui  阅读(959)  评论(0编辑  收藏  举报