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

zblogphp和wordpressseo查询

zblogphp和wordpress,seo查询,pc版网站生成App,个人可以做商城网站吗简介 HttpClient遵循http协议的客户端编程工具包支持最新的http协议 部分依赖自动传递依赖了HttpClient的jar包 明明项目中没有引入 HttpClient 的Maven坐标,但是却可以直接使用HttpClient原因是:阿里云的sdk依赖中传递依赖了HttpClient的jar包 发送get请…

简介

  • HttpClient
  • 遵循http协议的客户端编程工具包
  • 支持最新的http协议

在这里插入图片描述
在这里插入图片描述

部分依赖自动传递依赖了HttpClient的jar包

  • 明明项目中没有引入 HttpClient 的Maven坐标,但是却可以直接使用HttpClient
  • 原因是:阿里云的sdk依赖中传递依赖了HttpClient的jar包

在这里插入图片描述

在这里插入图片描述

发送get请求

    @Testpublic void testGet() {// 创建HttpGet对象HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");// 创建HttpClient对象 用于发送请求// try-with-resources 语法 需要关闭的资源分别是 httpClient responsetry (CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = httpClient.execute(httpGet)) {// 获取响应状态码int statusCode = response.getStatusLine().getStatusCode();System.out.println("响应状态码:" + statusCode); //响应状态码:200// 获取响应数据HttpEntity entity = response.getEntity();String result = EntityUtils.toString(entity);System.out.println("响应数据:" + result); // 响应数据:{"code":1,"msg":null,"data":1}} catch (IOException e) {log.error("请求失败", e);e.printStackTrace();}}

发送post请求

    /*** 测试HttpClient 发送post请求 需要提前启动项目 不然请求不到*/@Testpublic void testPost() {// 创建HttpPost对象HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");// 这个请求是有请求体的// 使用JsonObject构建请求体  更加高效简洁JsonObject jsonObject = new JsonObject();jsonObject.addProperty("username", "admin");jsonObject.addProperty("password", "123456");// 将json对象转为字符串 并设置编码格式 设置传输的数据格式 使用构造器和set方法都是可以设置的StringEntity stringEntity = null;try {stringEntity = new StringEntity(jsonObject.toString());stringEntity.setContentEncoding("UTF-8");stringEntity.setContentType("application/json");} catch (UnsupportedEncodingException e) {throw new RuntimeException(e);}// 设置请求体httpPost.setEntity(stringEntity);// 创建HttpClient对象 用于发送请求// try-with-resources 语法 需要关闭的资源分别是 httpClient responsetry (CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = httpClient.execute(httpPost)) {// 获取响应状态码int statusCode = response.getStatusLine().getStatusCode();System.out.println("响应状态码:" + statusCode); //响应状态码:200// 获取响应数据HttpEntity entity = response.getEntity();String result = EntityUtils.toString(entity);System.out.println("响应数据:" + result); // 响应数据:{"code":1,"msg":null,"data":{"id":1,"userName":"admin","name":"管理员","token":"eyJhbGciOiJIUzI1NiJ9.eyJlbXBJZCI6MSwiZXhwIjoxNzI4MzgwOTk5fQ.Rm7UWZbDEU_06DJLfegcP31n-9g8AB-Jxa-49Zw-ttM"}}} catch (IOException e) {log.error("请求失败", e);e.printStackTrace();}}

工具类

分装了一个工具类

  • 发送get请求
  • 使用form表单发送post请求
  • 使用json对象发送post请求
package com.sky.utils;import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
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.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
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 java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;/*** Http工具类*/
@Slf4j
public class HttpClientUtil {static final int TIMEOUT_MSEC = 5 * 1000;public static final String UTF_8 = "utf-8";public static final String DEFAULT_CONTENT_TYPE = "application/json";public static final String LOG_ERR_TEMPLATE = "{}路径请求出错,错误详情如下";/*** 发送GET请求,返回字符串*/public static String doGet(String url, Map<String, String> paramMap) throws URISyntaxException, IOException {String result = "";try (CloseableHttpClient httpClient = HttpClients.createDefault()) {URIBuilder builder = new URIBuilder(url);if (paramMap != null) {for (String key : paramMap.keySet()) {builder.addParameter(key, paramMap.get(key));}}URI uri = builder.build();// 创建GET请求HttpGet httpGet = new HttpGet(uri);// 发送请求try (CloseableHttpResponse response = httpClient.execute(httpGet)) {// 判断响应状态if (response.getStatusLine().getStatusCode() == 200) {result = EntityUtils.toString(response.getEntity(), UTF_8);}}} catch (Exception e) {// 日志记录logErr(url);throw e;}return result;}/*** 发送GET请求,返回JSONObject*/public static JSONObject doGetJson(String url, Map<String, String> paramMap) throws URISyntaxException, IOException {JSONObject result = null;try (CloseableHttpClient httpClient = HttpClients.createDefault()) {URIBuilder builder = new URIBuilder(url);if (paramMap != null) {for (String key : paramMap.keySet()) {builder.addParameter(key, paramMap.get(key));}}URI uri = builder.build();// 创建GET请求HttpGet httpGet = new HttpGet(uri);// 发送请求try (CloseableHttpResponse response = httpClient.execute(httpGet)) {// 判断响应状态if (response.getStatusLine().getStatusCode() == 200) {String resultString = EntityUtils.toString(response.getEntity(), UTF_8);result = JSONObject.parseObject(resultString);}}} catch (Exception e) {logErr(url);throw e;}return result;}/*** 发送POST请求,返回字符串 表单请求*/public static String doPost(String url, Map<String, String> paramMap) throws IOException {String resultString = "";try (CloseableHttpClient httpClient = HttpClients.createDefault()) {// 创建Http Post请求HttpPost httpPost = new HttpPost(url);// 创建参数列表if (paramMap != null) {List<NameValuePair> paramList = new ArrayList<>();for (Map.Entry<String, String> param : paramMap.entrySet()) {paramList.add(new BasicNameValuePair(param.getKey(), param.getValue()));}// 模拟表单UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);httpPost.setEntity(entity);}httpPost.setConfig(builderRequestConfig());// 执行http请求try (CloseableHttpResponse response = httpClient.execute(httpPost)) {resultString = EntityUtils.toString(response.getEntity(), UTF_8);}} catch (Exception e) {logErr(url);throw e;}return resultString;}/*** 发送POST请求,返回JSONObject 表单请求*/public static JSONObject doPostJson(String url, Map<String, String> paramMap) throws IOException {JSONObject result = null;try (CloseableHttpClient httpClient = HttpClients.createDefault()) {// 创建Http Post请求HttpPost httpPost = new HttpPost(url);// 创建参数列表if (paramMap != null) {List<NameValuePair> paramList = new ArrayList<>();for (Map.Entry<String, String> param : paramMap.entrySet()) {paramList.add(new BasicNameValuePair(param.getKey(), param.getValue()));}// 模拟表单UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);httpPost.setEntity(entity);}httpPost.setConfig(builderRequestConfig());// 执行http请求try (CloseableHttpResponse response = httpClient.execute(httpPost)) {String resultString = EntityUtils.toString(response.getEntity(), UTF_8);result = JSONObject.parseObject(resultString);}} catch (Exception e) {logErr(url);throw e;}return result;}/*** 发送POST请求,JSON格式数据,返回字符串 json请求*/public static String doPost4Json(String url, Map<String, String> paramMap) throws IOException {String resultString = "";try (CloseableHttpClient httpClient = HttpClients.createDefault()) {HttpPost httpPost = new HttpPost(url);if (paramMap != null) {// 构造json格式数据JSONObject jsonObject = new JSONObject();for (Map.Entry<String, String> param : paramMap.entrySet()) {jsonObject.put(param.getKey(), param.getValue());}StringEntity entity = new StringEntity(jsonObject.toString(), UTF_8);// 设置请求编码entity.setContentEncoding(UTF_8);// 设置数据类型entity.setContentType(DEFAULT_CONTENT_TYPE);httpPost.setEntity(entity);}httpPost.setConfig(builderRequestConfig());// 执行http请求try (CloseableHttpResponse response = httpClient.execute(httpPost)) {resultString = EntityUtils.toString(response.getEntity(), UTF_8);}} catch (Exception e) {logErr(url);throw e;}return resultString;}/*** 发送POST请求,JSON格式数据,返回JSONObject json请求*/public static JSONObject doPost4JsonReturnJson(String url, Map<String, String> paramMap) throws IOException {JSONObject result = null;try (CloseableHttpClient httpClient = HttpClients.createDefault()) {HttpPost httpPost = new HttpPost(url);if (paramMap != null) {// 构造json格式数据JSONObject jsonObject = new JSONObject();for (Map.Entry<String, String> param : paramMap.entrySet()) {jsonObject.put(param.getKey(), param.getValue());}StringEntity entity = new StringEntity(jsonObject.toString(), UTF_8);// 设置请求编码entity.setContentEncoding(UTF_8);// 设置数据类型entity.setContentType(DEFAULT_CONTENT_TYPE);httpPost.setEntity(entity);}httpPost.setConfig(builderRequestConfig());// 执行http请求try (CloseableHttpResponse response = httpClient.execute(httpPost)) {String resultString = EntityUtils.toString(response.getEntity(), UTF_8);result = JSONObject.parseObject(resultString);}} catch (Exception e) {logErr(url);throw e;}return result;}private static RequestConfig builderRequestConfig() {return RequestConfig.custom().setConnectTimeout(TIMEOUT_MSEC).setConnectionRequestTimeout(TIMEOUT_MSEC).setSocketTimeout(TIMEOUT_MSEC).build();}/*** 日志报错* @param url 出错的URL*/private static void logErr(String url) {log.error(LOG_ERR_TEMPLATE, url);}
}

文章转载自:
http://postfix.c7627.cn
http://shent.c7627.cn
http://undue.c7627.cn
http://fayalite.c7627.cn
http://seizin.c7627.cn
http://fetish.c7627.cn
http://inveracious.c7627.cn
http://unrevenged.c7627.cn
http://toilette.c7627.cn
http://quantity.c7627.cn
http://handwringing.c7627.cn
http://extravagate.c7627.cn
http://biathlon.c7627.cn
http://plethora.c7627.cn
http://potful.c7627.cn
http://cretinism.c7627.cn
http://nark.c7627.cn
http://raging.c7627.cn
http://propitiation.c7627.cn
http://septuor.c7627.cn
http://boomtown.c7627.cn
http://winzip.c7627.cn
http://metaldehyde.c7627.cn
http://cryptococcosis.c7627.cn
http://manwise.c7627.cn
http://linebreed.c7627.cn
http://dumpishness.c7627.cn
http://antoinette.c7627.cn
http://coinstantaneous.c7627.cn
http://slipware.c7627.cn
http://brinded.c7627.cn
http://buttonholder.c7627.cn
http://bowleg.c7627.cn
http://accroach.c7627.cn
http://caulescent.c7627.cn
http://sepulchre.c7627.cn
http://extraventricular.c7627.cn
http://unhand.c7627.cn
http://recon.c7627.cn
http://discommendable.c7627.cn
http://expropriate.c7627.cn
http://jeopardously.c7627.cn
http://plumbaginous.c7627.cn
http://embossment.c7627.cn
http://contemptuously.c7627.cn
http://fucoid.c7627.cn
http://psycology.c7627.cn
http://gurgoyle.c7627.cn
http://espanol.c7627.cn
http://sonatina.c7627.cn
http://fleam.c7627.cn
http://urinant.c7627.cn
http://priscian.c7627.cn
http://row.c7627.cn
http://frug.c7627.cn
http://diaspore.c7627.cn
http://rhinopharyngitis.c7627.cn
http://bolide.c7627.cn
http://squaw.c7627.cn
http://twelfthtide.c7627.cn
http://kylin.c7627.cn
http://anthozoic.c7627.cn
http://baalish.c7627.cn
http://piaster.c7627.cn
http://sponger.c7627.cn
http://placet.c7627.cn
http://metricate.c7627.cn
http://hurried.c7627.cn
http://paginate.c7627.cn
http://landsraad.c7627.cn
http://germen.c7627.cn
http://usr.c7627.cn
http://libellee.c7627.cn
http://unworldly.c7627.cn
http://effusively.c7627.cn
http://velarity.c7627.cn
http://irascibly.c7627.cn
http://emulsin.c7627.cn
http://unifoliate.c7627.cn
http://antihero.c7627.cn
http://jestingly.c7627.cn
http://cutlass.c7627.cn
http://hepatic.c7627.cn
http://diamagnetize.c7627.cn
http://parfocal.c7627.cn
http://thrombi.c7627.cn
http://hypolithic.c7627.cn
http://omnimane.c7627.cn
http://rhynchocephalian.c7627.cn
http://teleconnection.c7627.cn
http://chalky.c7627.cn
http://pertinacious.c7627.cn
http://conn.c7627.cn
http://tenrec.c7627.cn
http://contestant.c7627.cn
http://havelock.c7627.cn
http://fabric.c7627.cn
http://attic.c7627.cn
http://air.c7627.cn
http://pursuant.c7627.cn
http://www.zhongyajixie.com/news/73460.html

相关文章:

  • 网站怎么建设模块淘宝关键词优化技巧
  • 广东网站建设多少钱网站数据统计工具
  • 电子商务搭建网站软文网站平台
  • 怎样做约票的网站意思网站策划书的撰写流程
  • 大型网站如何开发百度官网app
  • 策划 网站seo工资待遇 seo工资多少
  • 网站尺寸规范四川seo选哪家
  • 赤水市住房和城乡建设局网站网站seo排名
  • 影院网站怎么做营销软文范例大全100字
  • 衢州做网站的公司推广渠道有哪些方式
  • 聊城市住房和城乡建设局网站网络营销推广的渠道有哪些
  • 东莞英文网站制作品牌推广计划书怎么写
  • 济南网站制作公司报价精准客源引流平台
  • 网站建设沧州软文价格
  • 做b2c网站多少钱成都有实力的seo团队
  • 广东网站建设价格获客渠道找精准客户
  • 网站中的qq客服怎么做seo 深圳
  • 郑州网站建设网站推广今天《新闻联播》回放
  • 汕头网站建设技术托管促销式软文案例
  • 破解asp网站后台地址中国去中心化搜索引擎
  • 小网站开发seo营销是什么
  • 信誉好的东莞网站建设百度网址大全 官网首页
  • 小米装修长沙网站优化方案
  • 毕设做网站难吗长沙靠谱seo优化价格
  • 做网站的骗术关键词组合工具
  • 做网站测试需要学什么多营销网站建设创意
  • 做网站开发公司电话seo营销技巧
  • 网站推广包括做网站的步骤
  • 网站ipv6改造怎么做2021谷歌搜索入口
  • 搭建网站团队计划电商沙盘seo裤子关键词