当前位置: 首页 > news >正文

织梦网站可以微信登录吗杭州谷歌推广

织梦网站可以微信登录吗,杭州谷歌推广,网络推广的策略,库存管理项目中经常会用到向第三方系统发送请求来传递数据或者获得信息&#xff0c;一般用的比较多的为HttpClient 和 HttpRequest&#xff0c;这里简要总结一下 HttpClient 和 HttpRequest 的用法 一、HttpClient 1. 发送get请求 public static String get(String url, Map<Stri…

项目中经常会用到向第三方系统发送请求来传递数据或者获得信息,一般用的比较多的为HttpClient 和 HttpRequest,这里简要总结一下 HttpClient 和 HttpRequest 的用法

一、HttpClient

1. 发送get请求

public static String get(String url, Map<String, String> headMap, int timeout) {String result = null;CloseableHttpClient httpclient = HttpClients.createDefault();CloseableHttpResponse response = null;try {HttpGet httpGet = new HttpGet(url);RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).build();httpGet.setConfig(requestConfig);setHead(httpGet, headMap);response = httpclient.execute(httpGet);HttpEntity entity = response.getEntity();result = EntityUtils.toString(entity, "UTF-8");EntityUtils.consume(entity);} catch (Exception e) {LOG.error("HttpGet [{}] error: {}", url, e.getMessage(), e);throw new RuntimeException(e.getMessage(), e);} finally {try {if (null != response) {response.close();  }httpclient.close();} catch (IOException e) {LOG.error("HttpGet [{}] httpclient close error: {}", url, e.getMessage(), e);}}return result;
}

1. 发送post请求

public static String postJson(String url, String body, Map<String, String> headMap, int timeout) {CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = null;try {HttpPost httpPost = new HttpPost(url);httpPost.addHeader("Content-Type", "application/json;charset=utf-8");setHead(httpPost, headMap);RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).build();httpPost.setConfig(requestConfig);httpPost.setEntity(new StringEntity(body, "UTF-8"));response = httpClient.execute(httpPost);HttpEntity entity = response.getEntity();String responseContent = EntityUtils.toString(entity, "UTF-8");EntityUtils.consume(entity);return responseContent;} catch (Exception e) {LOG.error("HttpPostJson [{}] error: {}", url, e.getMessage(), e);throw new RuntimeException(e.getMessage(), e);} finally {try {response.close();httpClient.close();} catch (IOException e) {LOG.error("HttpPostJson [{}] httpclient close error: {}", url, e.getMessage(), e);}}
}
private static void setHead(HttpRequestBase httpRequestBase, Map<String, String> headMap) {if (headMap != null && headMap.size() > 0) {Set<String> keySet = headMap.keySet();for (String key : keySet) {httpRequestBase.addHeader(key, headMap.get(key));}}
}

优点:连接池(复用HTTP连接)、重试、代理机制,社区活跃,性能更加优异

缺点:配置和API使用较为复杂

二、HttpRequest

1. 发送get请求

public static String get(String methodName, String url, Map<String, Object> paramMap, Map<String, String> headers){return HttpRequest.get(url).addHeaders(headers).form(paramMap).execute().body();
}

2. 发送post请求

public static String postForm(String methodName, String url, Map<String, Object> paramMap, Map<String, String> headers, String paramStr){return HttpRequest.post(url).addHeaders(headers).form(paramMap).body(paramStr).execute().body();
}

优点:功能简单,整体比较易用

缺点:性能一般,社区支持较弱

三、对比

hutool 的 HttpRequest 底层是基于 HttpClient 的,是对其进行了封装,提供了更加简便的API请求,使用 HttpRequest 可在一行代码中实现发送简单的HTTP请求,如果需要使用 HttpClient 更加复杂的操作,则可以通过自定义 HttpClient 并通过 setHttpClient() 方法传入 HttpClient

例如通过自定义Httpclient实现设置最大连接数量

// 创建连接池管理器
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(100); // 设置最大连接数
connectionManager.setDefaultMaxPerRoute(20); // 设置每个路由的最大连接数// 创建自定义的HttpClient
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setConnectionManager(connectionManager);// 使用 Hutool 的 HttpRequest 发送请求
HttpResponse response = HttpRequest.get("https://example.com").timeout(5000) // 设置超时时间.setHttpClient(httpClientBuilder.build()) // 设置自定义的HttpClient.execute();

总结

  1. 小型简单项目可使用 HttpRequest 快速请求,较大型复杂项目适合使用 HttpClient
  2. HttpClient 引入 httpclient 依赖,HttpRequest 引入 hutool 依赖
  3. 如果有连接池、重试、代理等机制的需求则可使用 HttpClient
  4. 高性能,高并发,有复杂Http操作的场景则可使用 HttpClient
http://www.hrbkazy.com/news/24893.html

相关文章:

  • 怎样做网站推广整站seo排名
  • 国外政府网站设计seo 优化教程
  • 台前做网站的公司线上推广工作内容
  • 手机如何做微商城网站设计seo排名哪家有名
  • 开通公司网站怎么开通百度精准引流推广
  • 淘宝优惠劵做网站模版推广普通话手抄报内容大全资料
  • 石家庄网站建设案例杭州网站优化推荐
  • html5手机网站制作教程资源网站优化排名软件
  • 微网站模板建设的选择济南百度开户电话
  • 2023年房地产会暴涨吗无锡百度seo优化
  • 廊坊盘古网站建设长沙seo优化排名推广
  • 国外 wordpress模板下载地址富阳seo关键词优化
  • 毕设做网站些什么比较简单西安抖音seo
  • 广州学生做网站黑科技引流工具
  • 网站名称没有排名2023全民核酸又开始了
  • 政府网站建设回顾公司查询
  • 做网站文章要一篇一篇的写吗优秀网页设计作品
  • 找公司做网站多少钱武汉网站seo公司
  • 旧域名怎么做新网站什么时候网络推广
  • 福州seo按天付费网站建设推广优化
  • 做网站的一般步骤seo快速排名关键词
  • wordpress adminajax.php搜索引擎优化的基本方法
  • 自己做网站系统必应搜索引擎首页
  • 做网站需要学会哪些北京网站优化排名
  • 企业网站优化设计应该把什么放在首位seo网站优化培训价格
  • 重庆电商网站友缘在线官网
  • wordpress安装完成访问不了seo标签怎么优化
  • 深圳定制建设网站微博营销成功案例8个
  • 有经验的南昌网站制作外贸网络营销推广
  • 实验室网站建设的意义网络营销的五大优势