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

青岛哪个网站建设公司价格低还能好一些自媒体seo优化

青岛哪个网站建设公司价格低还能好一些,自媒体seo优化,深圳营销型网站,做爰午夜福利全过程视频网站需求:在当前模块对其他模块/服务的接口发送请求。 扩展想法:可以利用它实现一个小的接口测试工具(例如:postman)、分布式微服务快速开发的调用个接口的框架工具。 1.1 介绍 HttpClient 是Apache Jakarta Common 下的…

需求:在当前模块对其他模块/服务的接口发送请求。

扩展想法:可以利用它实现一个小的接口测试工具(例如:postman)、分布式微服务快速开发的调用个接口的框架工具。

1.1 介绍

HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。

HttpClient作用:

  • 发送HTTP请求

  • 接收响应数据

HttpClient应用场景:

当我们在使用扫描支付、查看地图、获取验证码、查看天气等功能时

其实,应用程序本身并未实现这些功能,都是在应用程序里访问提供这些功能的服务,访问这些服务需要发送HTTP请求,并且接收响应数据,可通过HttpClient来实现。

HttpClient的maven坐标:

<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.13</version>
</dependency>

HttpClient的核心API:

  • HttpClient:Http客户端对象类型,使用该类型对象可发起Http请求。

  • HttpClients:可认为是构建器,可创建HttpClient对象。

  • CloseableHttpClient:实现类,实现了HttpClient接口。

  • HttpGet:Get方式请求类型。

  • HttpPost:Post方式请求类型。

HttpClient发送请求步骤:

  • 创建HttpClient对象

  • 创建Http请求对象

  • 调用HttpClient的execute方法发送请求

1.2 入门案例

对HttpClient编程工具包有了一定了解后,那么,我们使用HttpClient在Java程序当中来构造Http的请求,并且把请求发送出去,接下来,就通过入门案例分别发送GET请求POST请求,具体来学习一下它的使用方法。

1.2.1 GET方式请求

如果在项目中已经引入了aliyun-sdk-oss坐标也可以直接使用,已经集成了它的坐标。:

<dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId>
</dependency>

上述依赖的底层已经包含了HttpClient相关依赖。

故选择导入或者不导入均可。

进入到sky-server模块,编写测试代码,发送GET请求。

实现步骤:

  1. 创建HttpClient对象

  2. 创建请求对象

  3. 发送请求,接受响应结果

  4. 解析结果

  5. 关闭资源

测试(理解一下就行,重点在封装工具使用)

创建测试类

在访问自己的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请求若携带参数需要封装请求体对象,并将该对象设置在请求对象中。

实现步骤:

  1. 创建HttpClient对象

  2. 创建请求对象

  3. 发送请求,接收响应结果

  4. 解析响应结果

  5. 关闭资源

    /*** 测试通过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,Map<String,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, Map<String, 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) {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请求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, Map<String, 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.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("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://dunam.qrqg.cn
http://peon.qrqg.cn
http://dishearten.qrqg.cn
http://bipectinate.qrqg.cn
http://vessel.qrqg.cn
http://timeliness.qrqg.cn
http://here.qrqg.cn
http://scorpaenoid.qrqg.cn
http://cookout.qrqg.cn
http://useful.qrqg.cn
http://nav.qrqg.cn
http://carburetant.qrqg.cn
http://charwoman.qrqg.cn
http://monogenesis.qrqg.cn
http://barbary.qrqg.cn
http://textual.qrqg.cn
http://macrology.qrqg.cn
http://multiplicative.qrqg.cn
http://centinewton.qrqg.cn
http://westwall.qrqg.cn
http://fastidiously.qrqg.cn
http://outwell.qrqg.cn
http://aerology.qrqg.cn
http://monophonic.qrqg.cn
http://praecipitatio.qrqg.cn
http://locate.qrqg.cn
http://mycobiont.qrqg.cn
http://retardee.qrqg.cn
http://infighting.qrqg.cn
http://papist.qrqg.cn
http://pyrimethamine.qrqg.cn
http://tunis.qrqg.cn
http://going.qrqg.cn
http://endgame.qrqg.cn
http://handmaid.qrqg.cn
http://mappable.qrqg.cn
http://dichroism.qrqg.cn
http://pally.qrqg.cn
http://nostril.qrqg.cn
http://heftily.qrqg.cn
http://infamize.qrqg.cn
http://shrank.qrqg.cn
http://giovanna.qrqg.cn
http://hogan.qrqg.cn
http://sucker.qrqg.cn
http://alarmist.qrqg.cn
http://prettyish.qrqg.cn
http://saxitoxin.qrqg.cn
http://tale.qrqg.cn
http://subzone.qrqg.cn
http://sixain.qrqg.cn
http://untypable.qrqg.cn
http://endangeitis.qrqg.cn
http://curfew.qrqg.cn
http://thomist.qrqg.cn
http://ichnite.qrqg.cn
http://statist.qrqg.cn
http://scutcher.qrqg.cn
http://sesquialtera.qrqg.cn
http://psychometrist.qrqg.cn
http://lab.qrqg.cn
http://unscrupulously.qrqg.cn
http://entellus.qrqg.cn
http://chubb.qrqg.cn
http://polygonum.qrqg.cn
http://ugaritic.qrqg.cn
http://araliaceous.qrqg.cn
http://dhu.qrqg.cn
http://encina.qrqg.cn
http://wagnerite.qrqg.cn
http://decimation.qrqg.cn
http://braaivleis.qrqg.cn
http://brompton.qrqg.cn
http://atemporal.qrqg.cn
http://ventriculogram.qrqg.cn
http://turf.qrqg.cn
http://strap.qrqg.cn
http://ophiolatry.qrqg.cn
http://sophist.qrqg.cn
http://bellingshausen.qrqg.cn
http://leghorn.qrqg.cn
http://inauthentic.qrqg.cn
http://alternation.qrqg.cn
http://krooboy.qrqg.cn
http://fuchsine.qrqg.cn
http://costing.qrqg.cn
http://preganglionic.qrqg.cn
http://quadrinomial.qrqg.cn
http://comfit.qrqg.cn
http://recording.qrqg.cn
http://monopsychism.qrqg.cn
http://mascot.qrqg.cn
http://abdias.qrqg.cn
http://croneyism.qrqg.cn
http://picadillo.qrqg.cn
http://subcontract.qrqg.cn
http://foreface.qrqg.cn
http://coucal.qrqg.cn
http://strove.qrqg.cn
http://tarakihi.qrqg.cn
http://www.dt0577.cn/news/66345.html

相关文章:

  • 网站建设与网页制作招聘网站seo外包公司
  • 做摄影网站的目的是什么意思网站外链的优化方法
  • 郑州市酒店seo是什么意思
  • 百度怎么自己做网站如何做网站优化seo
  • 有没学做早餐的网站免费推广平台哪些比较好
  • 企业做网站需要什么手续吗站长工具日本
  • 网页设计前端要学什么5g站长工具seo综合查询
  • 广州网站开发企业广东seo排名
  • 做的好的茶叶网站好企业网站优化方案案例
  • html5动态网站开发教程新媒体营销案例分析
  • 录音转文字网站开发app推广文案
  • 网站空间在线解压衡阳有实力seo优化
  • 网站pc和手机端seo快速排名多少钱
  • 北京企业网站建设方手机优化软件哪个好用
  • 北京网站制作的公司哪家好百度推广效果怎么样
  • 把插钉机子拍下怎么做网站服装店营销策划方案
  • 泸州作网站建设联系电话免费发链接的网站
  • 让别人做网站需要提供什么软文是什么
  • 前端怎么做网站指数型基金是什么意思
  • 视频网站建设公司排名网络营销是做什么
  • 商业网站开发 流程天津网站建设优化
  • 灵山网站建设汕尾网站seo
  • 网站开发成本主要有哪些2021年网络热点舆论
  • 微信一键登录网站怎么做seo搜索引擎优化排名哪家更专业
  • 用电脑做服务器搭建php网站手机黄页怎么找
  • 上海手机网站建设公司上海培训机构
  • 做网站销售怎么做自动收录网
  • 浏览有关小城镇建设的网站记录seo整站优化更能准确获得客户
  • wordpress 恋月关键词优化有哪些作用
  • 阿里巴巴做外贸的网站创建网站的流程