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

网络小说写作网站wordpress 前台评论

网络小说写作网站,wordpress 前台评论,wordpress 分类seo,网站编辑器判断需求#xff1a;在当前模块对其他模块/服务的接口发送请求。 扩展想法#xff1a;可以利用它实现一个小的接口测试工具#xff08;例如#xff1a;postman#xff09;、分布式微服务快速开发的调用个接口的框架工具。 1.1 介绍 HttpClient 是Apache Jakarta Common 下的…需求在当前模块对其他模块/服务的接口发送请求。 扩展想法可以利用它实现一个小的接口测试工具例如postman、分布式微服务快速开发的调用个接口的框架工具。 1.1 介绍 HttpClient 是Apache Jakarta Common 下的子项目可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包并且它支持 HTTP 协议最新的版本和建议。 HttpClient作用 发送HTTP请求 接收响应数据 HttpClient应用场景 当我们在使用扫描支付、查看地图、获取验证码、查看天气等功能时 其实应用程序本身并未实现这些功能都是在应用程序里访问提供这些功能的服务访问这些服务需要发送HTTP请求并且接收响应数据可通过HttpClient来实现。 HttpClient的maven坐标 dependencygroupIdorg.apache.httpcomponents/groupIdartifactIdhttpclient/artifactIdversion4.5.13/version /dependency HttpClient的核心API HttpClientHttp客户端对象类型使用该类型对象可发起Http请求。 HttpClients可认为是构建器可创建HttpClient对象。 CloseableHttpClient实现类实现了HttpClient接口。 HttpGetGet方式请求类型。 HttpPostPost方式请求类型。 HttpClient发送请求步骤 创建HttpClient对象 创建Http请求对象 调用HttpClient的execute方法发送请求 1.2 入门案例 对HttpClient编程工具包有了一定了解后那么我们使用HttpClient在Java程序当中来构造Http的请求并且把请求发送出去接下来就通过入门案例分别发送GET请求和POST请求具体来学习一下它的使用方法。 1.2.1 GET方式请求 如果在项目中已经引入了aliyun-sdk-oss坐标也可以直接使用已经集成了它的坐标。 dependencygroupIdcom.aliyun.oss/groupIdartifactIdaliyun-sdk-oss/artifactId /dependency 上述依赖的底层已经包含了HttpClient相关依赖。 故选择导入或者不导入均可。 进入到sky-server模块编写测试代码发送GET请求。 实现步骤 创建HttpClient对象 创建请求对象 发送请求接受响应结果 解析结果 关闭资源 测试理解一下就行重点在封装工具使用 创建测试类 在访问自己的http://localhost:8080/user/shop/status请求时需要提前启动项目。 import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpEntity; 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.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest;//SpringBootTest public class HttpClientTest {/*** 测试通过httpclient发送GET方式的请求*/Testpublic void testGET() throws Exception{//创建httpclient对象CloseableHttpClient httpClient HttpClients.createDefault();//创建请求对象//HttpGet httpGet new HttpGet(http://localhost:8080/user/shop/status);HttpGet httpGet new HttpGet(http://www.baidu.com/);//发送请求接受响应结果CloseableHttpResponse response httpClient.execute(httpGet);//获取服务端返回的状态码int statusCode response.getStatusLine().getStatusCode();System.out.println(服务端返回的状态码为 statusCode);//获取响应体HttpEntity entity response.getEntity();String body EntityUtils.toString(entity);System.out.println(服务端返回的数据为 body);//关闭资源response.close();httpClient.close();}测试结果 1.2.2 POST方式请求 在HttpClientTest中添加POST方式请求方法相比GET请求来说POST请求若携带参数需要封装请求体对象并将该对象设置在请求对象中。 实现步骤 创建HttpClient对象 创建请求对象 发送请求接收响应结果 解析响应结果 关闭资源 /*** 测试通过httpclient发送POST方式的请求*/Testpublic void testPOST() throws Exception{// 创建httpclient对象CloseableHttpClient httpClient HttpClients.createDefault();//创建请求对象HttpPost httpPost new HttpPost(http://localhost:8080/admin/employee/login);//构造接口请求参数JSONObject jsonObject new JSONObject();jsonObject.put(username,admin);jsonObject.put(password,123456);StringEntity entity new StringEntity(jsonObject.toString());//指定请求编码方式entity.setContentEncoding(utf-8);//数据格式entity.setContentType(application/json);httpPost.setEntity(entity);//发送请求CloseableHttpResponse response httpClient.execute(httpPost);//解析返回结果int statusCode response.getStatusLine().getStatusCode();System.out.println(响应码为 statusCode);HttpEntity entity1 response.getEntity();String body EntityUtils.toString(entity1);System.out.println(响应数据为 body);//关闭资源response.close();httpClient.close();} 测试结果 二、将其封装成一个工具类来使用 创建工具类HttpClientUtil.java import com.alibaba.fastjson.JSONObject; 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.util.ArrayList; import java.util.List; import java.util.Map; ​ /*** Http工具类*/ public class HttpClientUtil { ​static final  int TIMEOUT_MSEC 5 * 1000; ​/*** 发送GET方式请求* param url* param paramMap* return*/public static String doGet(String url,MapString,String paramMap){// 创建Httpclient对象CloseableHttpClient httpClient HttpClients.createDefault(); ​String result ;CloseableHttpResponse response null; ​try{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); ​//发送请求response httpClient.execute(httpGet); ​//判断响应状态if(response.getStatusLine().getStatusCode() 200){result EntityUtils.toString(response.getEntity(),UTF-8);}}catch (Exception e){e.printStackTrace();}finally {try {response.close();httpClient.close();} catch (IOException e) {e.printStackTrace();}} ​return result;} ​/*** 发送POST方式请求* param url* param paramMap* return* throws IOException*/public static String doPost(String url, MapString, String paramMap) throws IOException {// 创建Httpclient对象CloseableHttpClient httpClient HttpClients.createDefault();CloseableHttpResponse response null;String resultString ; ​try {// 创建Http Post请求HttpPost httpPost new HttpPost(url); ​// 创建参数列表if (paramMap ! null) {ListNameValuePair paramList new ArrayList();for (Map.EntryString, String param : paramMap.entrySet()) {paramList.add(new BasicNameValuePair(param.getKey(), param.getValue()));}// 模拟表单UrlEncodedFormEntity entity new UrlEncodedFormEntity(paramList);httpPost.setEntity(entity);} ​httpPost.setConfig(builderRequestConfig()); ​// 执行http请求response httpClient.execute(httpPost); ​resultString EntityUtils.toString(response.getEntity(), UTF-8);} catch (Exception e) {throw e;} finally {try {response.close();} catch (IOException e) {e.printStackTrace();}} ​return resultString;} ​/*** 发送POST方式请求* param url* param paramMap* return* throws IOException*/public static String doPost4Json(String url, MapString, String paramMap) throws IOException {// 创建Httpclient对象CloseableHttpClient httpClient HttpClients.createDefault();CloseableHttpResponse response null;String resultString ; ​try {// 创建Http Post请求HttpPost httpPost new HttpPost(url); ​if (paramMap ! null) {//构造json格式数据JSONObject jsonObject new JSONObject();for (Map.EntryString, String param : paramMap.entrySet()) {jsonObject.put(param.getKey(),param.getValue());}StringEntity entity new StringEntity(jsonObject.toString(),utf-8);//设置请求编码entity.setContentEncoding(utf-8);//设置数据类型entity.setContentType(application/json);httpPost.setEntity(entity);} ​httpPost.setConfig(builderRequestConfig()); ​// 执行http请求response httpClient.execute(httpPost); ​resultString EntityUtils.toString(response.getEntity(), UTF-8);} catch (Exception e) {throw e;} finally {try {response.close();} catch (IOException e) {e.printStackTrace();}} ​return resultString;}private static RequestConfig builderRequestConfig() {return RequestConfig.custom().setConnectTimeout(TIMEOUT_MSEC).setConnectionRequestTimeout(TIMEOUT_MSEC).setSocketTimeout(TIMEOUT_MSEC).build();} ​ } 记得导入相关依赖。
http://www.hkea.cn/news/14292881/

相关文章:

  • 成都企业网站建设哪家好seo网站页面优化
  • 三网合一网站建设系统 价格建设设计公司网站
  • 在家百度统计网站打不开张家港杨舍网站建设
  • 深圳网站建设方维网站模板站的模板展示怎么做的
  • 大连哪个企业想做网站江阴网站开发公司电话
  • wordpress怎么加快网站打开速度石家庄邮电职业技术学院
  • 网站怎样做权重涉密资质 网站建设
  • 西安企业网站排名优化工具怎样建设和维护网站
  • 网站一键生成wapwordpress 页面 浏览量
  • 馨雨公司网站建设策划方案开源快速网站搭建平台
  • 大连模板网站制作公司电话wordpress官方主题下载
  • 固镇县住房和城乡建设局网站wordpress不能注册
  • 嘉兴优化网站排名定制化网站建设公司
  • 网站建设费无形资产摊销动画做视频在线观看网站
  • 北京住房和建设部网站搭建网站案例
  • 厦门中科做网站总打电话来注册公司在哪个网站系统
  • 太仓网站制作网站建设毕业答辩ppt模板下载
  • 房地产网站制作公司淘宝网页版电脑版
  • wordpress建立网站实例深圳建筑公司招聘信息
  • 泊头市做网站湖北高端网站建设
  • 贵州省城乡与住房建设厅网站非织梦做的网站能仿吗
  • 高端网站建设的流程是什么做设计必须知道的几个网站
  • 专门做淘宝代运营的网站wordpress 全屏浮动
  • 大作设计网站官网登录入口网站怎么做qq登录
  • 买什么样的主机(用来建网站的)支持下载宣传推广方案范文
  • app应用开发的价位上海搜索优化推广
  • 河南网站建设问一问公司做网站的协议书和计划书
  • 门户网站的基本特征有做建站较好的网站
  • 电子商务网站建设c做百度推广的网络公司广州
  • 锤子网站cms版本如何解决旅游网站建设问题