HttpClient快速入门

编程入门 行业动态 更新时间:2024-10-23 01:43:38

HttpClient快速<a href=https://www.elefans.com/category/jswz/34/1770026.html style=入门"/>

HttpClient快速入门

HttpClient-基本使用

httpGet和httpPost

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.HttpClients;import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;import java.io.IOException;
import java.util.ArrayList;public class HttpClientTest {@Testpublic void testGet() throws IOException {//1.需要HttpClient对象CloseableHttpClient httpClient = HttpClients.createDefault();//2.创建HttpGet请求并进行相关设置HttpGet httpGet = new HttpGet("/");httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36");//3.发起请求CloseableHttpResponse response = httpClient.execute(httpGet);//4.获取响应数据if (response.getStatusLine().getStatusCode() == 200){ //200表示响应成功String html = EntityUtils.toString(response.getEntity(), "UTF-8");System.out.println(html);}//5.关闭资源httpClient.close();response.close();}@Testpublic void testPost() throws IOException {//1.需要HttpClient对象CloseableHttpClient httpClient = HttpClients.createDefault();//2.创建HttpPost请求并进行相关设置HttpPost httpPost = new HttpPost("/");//准备集合用来存放参数ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("username","donglin"));UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");httpPost.setEntity(entity);httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36");//3.发起请求CloseableHttpResponse response = httpClient.execute(httpPost);//4.获取响应数据if (response.getStatusLine().getStatusCode() == 200){ //200表示响应成功String html = EntityUtils.toString(response.getEntity(), "UTF-8");System.out.println(html);}//5.关闭资源httpClient.close();response.close();}
}

HttpClient-连接池

    @Testpublic void testPool() throws IOException {//1.创建HttpClient连接管理器PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();//2.设置参数cm.setMaxTotal(200);  //设置最大连接数cm.setDefaultMaxPerRoute(20); //设置每个主机的最大并发//调用两次进行试验doGet(cm);doGet(cm);}private void doGet(PoolingHttpClientConnectionManager cm) throws IOException {//3.从连接池获取HttpClient对象CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();//4.创建HttpGet对象HttpGet httpGet = new HttpGet("/");//5.发送请求CloseableHttpResponse response = httpClient.execute(httpGet);//6.获取数据if (response.getStatusLine().getStatusCode() == 200){ //200表示响应成功String html = EntityUtils.toString(response.getEntity(), "UTF-8");System.out.println(html);}//7.关闭资源response.close();//httpClient.close();  //这里不用关闭,因为是连接池}

HttpClient-超时设置 & 添加代理

    @Testpublic void testConfig() throws IOException {//0.创建请求配置对象RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000) //设置连接超时时间.setConnectTimeout(10000) //设置创建连接超时时间.setConnectionRequestTimeout(10000) //设置请求超时时间.setProxy(new HttpHost("111.177.63.86",8888)).build();//1.创建HttpClient对象//CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();//2.创建HttpGet请求并进行相关设置HttpGet httpGet = new HttpGet("/");//3.发起请求CloseableHttpResponse response = httpClient.execute(httpGet);//4.获取响应数据if (response.getStatusLine().getStatusCode() == 200){ //200表示响应成功String html = EntityUtils.toString(response.getEntity(), "UTF-8");System.out.println(html);}//5.关闭资源httpClient.close();response.close();}

HttpClient-封装

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
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.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import sun.www.http.HttpClient;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;public abstract class HttpUtils {private static PoolingHttpClientConnectionManager cm; //声明httpClient管理器对象(连接处)private static List<String> userAgentList = null;private static RequestConfig config = null;//静态代码块会在类被夹在的时候执行static {cm = new PoolingHttpClientConnectionManager();cm.setMaxTotal(200);cm.setDefaultMaxPerRoute(20);config = RequestConfig.custom().setSocketTimeout(10000) //设置连接超时时间.setConnectTimeout(10000) //设置创建连接超时时间.setConnectionRequestTimeout(10000) //设置请求超时时间.setProxy(new HttpHost("111.177.63.86",8888)).build();userAgentList = new ArrayList<String>();userAgentList.add("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36");userAgentList.add("Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36");userAgentList.add("Mozilla/5.01 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36");userAgentList.add("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Shuame)");userAgentList.add("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.3.8126.400)");userAgentList.add("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; .NET CLR 1.1.4322)");userAgentList.add("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; InfoPath.3)");userAgentList.add("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)");userAgentList.add("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0");}public static String getHtml(String url){//1.从连接池中获取httpClient对象CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();//2.创建HttpGet对象HttpGet httpGet = new HttpGet(url);//3.设置请求配置对象和请求头httpGet.setConfig(config);httpGet.setHeader("User-Agent",userAgentList.get(new Random().nextInt(userAgentList.size())));//4.发起请求CloseableHttpResponse response = null;try {response = httpClient.execute(httpGet);//5.获取响应内容if (response.getStatusLine().getStatusCode() == 200){ //200表示响应成功String html = "";if (response.getEntity()!=null){html = EntityUtils.toString(response.getEntity(), "UTF-8");System.out.println(html);}return html;}} catch (IOException e) {throw new RuntimeException(e);}finally {try {response.close();} catch (IOException e) {throw new RuntimeException(e);}}return null;}public static void main(String[] args) {HttpUtils.getHtml("/");}}

更多推荐

HttpClient快速入门

本文发布于:2023-12-03 07:33:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1652713.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:入门   快速   HttpClient

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!