码迷,mamicode.com
首页 > Web开发 > 详细

httpclien模拟浏览器

时间:2019-05-20 17:03:11      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:gen   agent   package   oid   exe   author   auth   exec   apache   

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();
	}
}

  

httpclien模拟浏览器

标签:gen   agent   package   oid   exe   author   auth   exec   apache   

原文地址:https://www.cnblogs.com/nidegui/p/10894729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!