java如何高质量的介绍自己的项目--关于Spring MVC+Springboot+Mybatis等(二)

实现跨系统调用我了解到的有两种方式:ajax通过jsonp来跨域;后台通过httpclient来调
下面这些事讲解关于HttpClient的几种使用示例:(其中由参考别人的用例,和自己总结的,还不完全规范后期会继续完善。)
GET无参
/**

  • GET—无参测试
  • @date 2020年9月10日 晚上9:47
    */

@Test
Public void doGetTestOne(){
//获得Http客户端(可以理解为)电脑中首先存在一个浏览器,但是实际上HttpClient与浏览器是不一样的
CloseableHttpClient httpClient =HttpClientBulider.create().Build();
//创建get请求
Http httpGet = new HttpGet(“Http://localhost:12345/doGetControllerOne”);

//响应模型
CloseableHttpReponse reponse= null;
Try{
//由客户端执行发送GET请求
Response=HttpClient.execute(httpGet);
//从响应模型中获取响应实体;
HttpEntity responseEntity =response.getEntity();
System.out.println(“响应状态为:”+response.getStatusLine());
If(responseEntity!=null){
System.out.println(“响应长度为:”+responseEntity.getContentLength());
System.out.println(“响应内容为:”+EntityUtils.toString(responseEntity));
}
} catch (ClientProtocolException e){
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

对应接受示例:
/**

  • GET无参

*/
@RequestMapping(“/doGetControllerOne”)
Public Strinng doGeControllerOne(){
Return “123”;
}

GET有参(方式1,直接拼接URL)
/**
GET有参测试,1:直接手动在url后面加上参数
*
/
@Test
Public void doGetTestWayOne(){
//获得Http客户端,可以理解为首先有一个浏览器
CloseableHttpClient httpclient = HttpClientBulider…create().build();
//参数
StringBuffer params =new StringBuffer();
Try{
//字符数据最好encoding以下:这样一来。某些特殊字符才能传过去,
Params.append(“name”+URLEncoder.encode(“&”,”utf-8”));
Params.append(“&”);
Params.appennd(“age=24”);
}catch(UnsupportedEncodingException e1){
e.printStackTrace();
}
创建get请求
HttpGet httpGrt =new HttpGet (“http://localhost:123456/doGetControllerTwo”+”?”+params);
//响应模型
CloseableHttpResponse response =null;
Try{
//配置信息
RequestConfig requestConfig =RequestConfig.custom()
//设置连接超时时间(单位毫秒)
.setConnectTimeout(5000);
//设置请求超时时间
.setConnectionRequestTimeout(5000);
//stocket读写超时时间
.SetSocketTimeout(5000);
//设置是否允许重定向(默认为true)
.setRedisrectsEnabled(true).build();
//将上面的配置信息 运用到这个GET请求里
httpGet.setConfig(requestConfig);
//由客户端执行发送(GET)请求;
Response=httpClient.execute(httpGet);
//从响应模型中获取响应体
HttpEntity responseEntity =response.getEntity();
System.out.println(“响应状态为:”+response.getStatusLine());
If(responseEntity!=null){
System.out.println(“响应内容长度为:”+responseEntity.getContentLength());
System.out.println(“响应内容为:”+EntityUtils.toString(responseEntity));
}
} catch (ClientProtocolException e){
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
对应接收示例:
/

*GET有参
*@Param name 姓名
*@Param age 年龄
*/
@RequestMapping(“/doGetControllerTwo”)
Public String doGetControllerTwo(String name, Integer age){
Return “没想到[“ +name+”]都”+age +”岁了!”;
}

GET有参 使用URL获取HTTPGET 方式二
/**
*

  • GET有参 将参数放入键值对类中,再放入URI中 通过URI得到HttpGet实例
    */
    @Test
    Public void doGetTestWayTwo(){
    //获得Http客户端(可以理解位:你得先有一个浏览器,注意:实际上HttpClient与浏览器是不一样的)
    CloseableHttpClient httpClient =HttpClientBulider.create().build();

//参数
URI uri =null;
Try{
//将参数放入键值对类NameValuePair中,再放入集合中;
List params =new ArrayList<>();
Params.add(new BasicNameValuePair(“name”,”&”));
Params.add(new BasicNameValuePair(“age”,”18”));
//设置uri信息,并将参数集合放入uri
//注: 这里也有一个键值对一个键值对往里面放setParameter(String key ,String value)
Uri= new URIilder().setScheme(“http”).setHost(“localhost”)
.setPort(12345).setPath(“/doGetControllerTwo”)
.setParameters(params).build();
}catch (URISyntaxException e1){
E.printStackTrace();
}
//创建Get请求
HttpGet httpGet =new HttpGet(uri);
}
//响应模型
CloseableHttpResponse response =null;
Try{
//配置信息
RequestConfig =requestConfig =RequestConfig.custom();
//设置连接超时时间(单位毫秒)
.setConnectTimeout(5000);
//设置请求超时时间
.setConnectionRequestTimeout(5000);
//stocket读写超时时间
.setSockerTimeout(5000);
//设置是否允许重定向(默认为true)
.setRedirectsEnabled(true).build();

//将上面的配置信息,运用到这个Get请求里
httpGet.setConfig(requestConfig);
//由客户端,执行发送Get请求
Response =httpClient.execute(httpGet);
//从响应模型中获取响应实体
HttpEntity responseEntity= response.getEntity();
System.out.println(“响应状态为:”+response.getStatusLine());
If(responseEntity!=null){
System.out.println(“相应内容长度为:”+responseEntity.getContentLength());
System.out.println(“响应内容为:”+EntityUtils.toString(responseEntity));
}
} catch (ClientProtocolException e){
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

对应接受示例:
/**
*GET有参
*
*@Param name
*
*@Param age
*
*
*@return 测试数据
*
*@date 2020年9月14日 晚上0:18:00
*
*/
@RequestMappinG(“/doGetControllerTwo”)
Public String doGetControllerTwo(String name ,Integer age){
Return “没想到[”+name+”]都”+age+”岁了!”;
}

POST无参
HttpClient发送示例:
/**
*post 无参测试
*
*@date
*/
@Test
Public void doPostTestOne(){
//获得Http客户端(可以理解为:你得先有一个浏览器;但是Httpclient与浏览器是不一样的)
CloseableHttpClient httpClient =HttpClientBulider.create().build();
//创建GET请求
HttpPost httpPost =new HttpPost(“http://localhost:12345/doGetControllerOne”);
//响应模型
CloseableHttpResponse response =null;
Try{
//由客户端执行(发送)POST请求
Response=httpClient.execute(httpPost);
//从响应模型中获取响应实体
HttpEntity responseEntity =response.getEntity();
System.out.println(“响应状态为:”+response.getStatusLine());
If(responseEntity!=null){
System.out.println(“响应长度为:”+responseEntity.getContentLength());
System.out.println(“响应内容为:”+EntityUtils.toString(responseEntity()));
}
}catch(ClientProtocolException e){
E.printStackTrace();
}catch(ParaseException e){
E.printStackTrace();
}catch(IOException e){
E.printStackTrace();
}finally{
Try{
//释放资源
If(httpClient!=null){
httpClient.close();
}
If(response!=null){
Response.close();
}
}catch (IOException e){
E.printStackTrace();
}
}
}

对应接受示例
/**
*POST无参
*
*return 测试数据
*
*/
@RequestMapping(value=”/doPostContRollerOne”,method=RequestMethod.POST)
Public String doPostControllerOne(){
Return “这个post请求没有任何参数!”
}

POST有参(普通参数)
POST参数传递普通参数时,这里是再url后缀上参数的方式示例
/**
*
*POST有参测试
*
*/
@Test
Public void doPostTestFour(){
//获得http客户端
CloseableHttpClient httpClient =HttpClientBulider.create().build();

//参数
StringBuffer params =new StringBuffer();
Try{
//字符数据最好encoding以下;这样某些特殊字符才能传过去,如果某人的名字是&不encodi就传不过去
Params.append(“name=”+URLEncoder.encode(“&”,”UTF-8”));
Params.append(“&”);
Params.append(“age=24”);’
}catch(UnsupportedEncodingException e1){
e1.printStackTrace();
}
//创建Post请求
HttPost httpPost =new HttpPost(“http://locallhost:123456/doPostControllerFour”+ “?”+params);
//设置ContentType注:如果只是普通参数的话,ContentType不一定非要用appliCation/json
httpPost.setHeader(“Content-Type”,”application/json;charset=utf8”);
//响应模型
Try{
//由客户端执行(发送)Post请求
Response =httpClient.execute(httpPost);
//从响应模型中获取响应实体
HttpEntity responseEntity =response.getEntity();
System.out.println(“响应状态为:”+response.getStatusLine());
If(responseEntity!=null){
System.out.println(“响应内容长度为:”+reponseEntity.getContentLength());
//主动设置编码,来防止响应乱码
String responseStr = EntityUtils.toString(responseEntity,StandardCharsets.UTF-8);
System.out.println(“相应内容:”+EntityUtils.toString(responseEntity));
}
}catch(ClientProtocolException e){
E.printStackTrace();
}catch(ParaseException e){
E.printStackTrace();
}catch(IOException e){
E.printStackTrace();
}finally{
Try{
//释放资源
If(httpClient!=null){
httpClient.close();
}
If(response!=null){
Response.close();
}
}catch (IOException e){
E.printStackTrace();
}
}
}

/**
*
*
*POST有参(普通参数)
*
*/

@RequestMapping(value =”/doPostContRollerFour”,method=RequestMethod.POST)
Public String doPostControllerThree1(String name,Integer age){
Return “[”+name+”]居然才[”+age+”]岁!!!”
}

POST有参(对象参数)
/**
*
*用户实体类模型
*
*
*/
Public class User{
//姓名
Private String name;
//年龄
Private Integer age;
//性别
Private String gender;
//座右铭
Private String motto;
// 省略get set方法…
Public String getName(){
Return name;
}
Public void setName(String name){
This.name=name;
}
//…
@Override
Public String toString(){
Return age+”岁”+gender+”人[”+name+”]的座右铭居然是:”+motto+”!!!”;
}

HttpClient发送示例:
/**
*POST有参测试(对象参数)
*
*
*/
@Test
Public void doPostTestTwo(){
//获得Http客户端可以理解为:先有一个浏览器,但是实际上与浏览器不一样
CloseableHttpClient httpClient =HttpClientBuilder.create().build();
//创建Post请求
Http httpPost= new HttpPost(“http://localhost:12345/doPostControllerTwo”);
User user =new User();
user.setName(“潘晓婷”);
user.setAge(“18”);
user.setGender(“女”);
User.setMotto(“姿势咬优雅~”);
//利用阿里的fastjson,将object转换为json字符串
导入com.alibab.fastjson.JSON包
String jsonString=Json.toJsonString(user);
StringEntity entity =new StirngEntity(jsonStirng,”utf-8”);
//post请求时将参数放在请求里面传过去,这里将entity放入post请求体中
httpPost.setEntity(entity);
httpPost.setHeader(“Content-Type”,”application/json;charset=utf-8”);

//响应模型
CloseableHttpResponse response=null;
Try{
//由客户端执行发送Post请求
Response=httpClient.execute(httpPost);
}
//从响应模型中获取响应实体
HttpEntity responseEntity =response.getEntity();
System.out.println(“响应状态为:”+response.getStatusLine());
If(responseEntity!=null){
System.out.println(“响应内容长度为:”+responseEntity.getContentLength());
//主动设置编码,来防止响应乱码
String responseStr = EntityUtils.toString(responseEntity,StandardCharsets.UTF-8);
System.out.println(“相应内容为:”+EntityUtils.toString(responseEntity()));
}
}catch(ClientProtocolException e){
E.printlnStatckTrace();
}catch(ParseException e){
E.printStackTrace();
}catch(IOException e){
E.printStackTrace();
}finally{
Try{
//释放资源
If(httpClient!=null){
httpClient.close();
}
If(response! =null){
Response.close();
}
}catch(IOException e){
E.printStackTrace();
}
}
}
}
}

/**
*
*POST有参(对象参数)
*
*
*/
@RequestMapping(value=”/doPostControllerTwo”,method =RequestMethod.POST)
Public String doPostControllerTwo(@RequestBody User user){
Return user.toString();
}

POST有参(普通参数+对象参数)

POST传递普通参数时,方式与GET一样即可,这里可以通过URI获得HttpPost的方式为例:
先给出User类:
/**
*
*用户实体类模型
*
*/

Public class User{
/*姓名/
Private String name;
/*年龄/
Private Integer age;
//座右铭
Private String motto;
Public String getName();
Return name;
}
Public void setName(String name){
This.name =name;
}
//省略get set 方法
@Override
Public String toString(){
Return age+”岁”+gender+”人[”+name+”]的座右铭居然是:”+motto+”!!!”;
}
HttpClient发送示例

/**
*
*POST 有参测试 (普通参数+对象参数)
*
*
*/

@Test
Public void doPostThree(){
//获得Http客户端(可以理解为你得先有一个浏览器)
//创建 Post请求
//参数
URI uri =null;
Try{
//将参数放入键值对类NameValuePair中,再放入集合中
List params =new ArrayList<>();
Params.add(new BasicNameValuePair(“flag”,”4”));
Params.add(new BasicNameValuePair(“meaning”,”这是什么鬼?”));

//设置uri信息,并将参数集合放入uri
//注:这里也支持一个键值对一个键值对往里面放setParameter(Strnig key ,String value)
Uri =new URIBuilder().setScheme(“http”).setHost(“localhost”).setPort(12345).setPath(“doPostControllerThree”).setParams(params).build();
}catch(URISyntaxException e1){
E1.prinStackThree();
}
HttpPost httpPost =new HttpPost(uri);
//HttpPost httpPost = new
//HttpPost(“http://localhost:12345/doPostControllerThree1”);
//创建user参数
User user =new User();
User.setName(“潘晓婷”);
User.setAge(18);
User.setGender(“女”);
User.setMotto(“姿势要优雅~”);
//将user对象转换为json字符串,并放入entity 中
StringEntity eneity =new StringEntity(JSON.toJSONString(user),”UTF-8”);
//POST 请求时将参数放在请求体里面传过去,这里将entity放入 post请求体中
httpPost.setEntity(entity);
httpPost.setHeader(“Content-Type”,”application/json;charset=utf8”);
//响应模型
CloseableHttpResponse response=null;
Try{
//由客户端执行发送Post请求
Response=httpClient.execute(httpPost);
}
//从响应模型中获取响应实体
HttpEntity responseEntity =response.getEntity();
System.out.println(“响应状态为:”+response.getStatusLine());
If(responseEntity!=null){
System.out.println(“响应内容长度为:”+responseEntity.getContentLength());
//主动设置编码,来防止响应乱码
String responseStr = EntityUtils.toString(responseEntity,StandardCharsets.UTF-8);
System.out.println(“相应内容为:”+EntityUtils.toString(responseEntity()));
}
}catch(ClientProtocolException e){
E.printlnStatckTrace();
}catch(ParseException e){
F.printStackTrace();
}catch(IOException e){
E.printStackTrace();
}finally{
Try{
//释放资源
If(httpClient!=null){
httpClient.close();
}
If(response! =null){
Response.close();
}
}catch(IOException e){
E.printStackTrace();
}
}
}
}
}
对应接受示例:
/**
*
*POST有参 普通参数+对象参数
*
*
*/
@RequestMapping(value =”/doPostControllerThree”,method =RequestMethod.POST)
Public String doPostControllerThree(@RequestBody User user ,Integer flag, String meaning){
Return user.toString()+”\n”+flag+”>>>”meaning;
}

解决响应乱码问题

进行HTTPS请求并进行(或不进行)证书校验
CloseableHttpClient httpClient =getHttpClient(true);

相关的HttpClient封装,
Http证书校验
/**
*
*
*是否是https请求
@return HttpClient 实例
/
Private CloseableHttpClient getHttpClient(boolean isHttps){
CliseableHttpClient httpClient;
If(isHttps){
SSLConnectionSocketFactory sslSockerFactory;
Try{
//如果不做证书校验的话
SslSocketFactory =getSocketFactory
Try{
//如果不做证书校验的话
sslSocketFactory =getSocketFactory(false,null,null);
//如果要进行证书校验
//证书
//InputStream ca =thIs.getClass().getClassLoader().getResourceAsStream(“client/ds.crt”));
//证书的别名:key cAalias只需保证唯一即可,推荐使用生成keystore时使用的别名.
//String cAalias =System.out.currentTimeMillus()+””+new .SecureRandom().nextInt(1000);
//sslSocketFactory =getSocketFactory(true,ca,cAalias);
}catch(Exception e){
Throw new RuntimeException(e);
}
}
httpClient = HttpClientBuilder.create().setSSLSocketFactory(sslSocketFactory).build();
return httpClient;
}
httpClient = HttpClientBuilder.create().build();
return httpClient;
}
/

  • HTTPS辅助方法, 为HTTPS请求 创建SSLSocketFactory实例、TrustManager实例
  • @param needVerifyCa
  • 是否需要检验CA证书(即:是否需要检验服务器的身份)
  • @param caInputStream
  • CA证书。(若不需要检验证书,那么此处传null即可)
  • @param cAalias
  • 别名。(若不需要检验证书,那么此处传null即可)
  • 注意:别名应该是唯一的, 别名不要和其他的别名一样,否者会覆盖之前的相同别名的证书信息。别名即key-value中的key。
  • @return SSLConnectionSocketFactory实例
  • @throws NoSuchAlgorithmException
  • 异常信息
  • @throws CertificateException
  • 异常信息
  • @throws KeyStoreException
  • 异常信息
  • @throws IOException
  • 异常信息
  • @throws KeyManagementException
  • 异常信息
  • @date 2019/6/11 19:52
    */
    private static SSLConnectionSocketFactory getSocketFactory(boolean needVerifyCa, InputStream caInputStream, String cAalias)
    throws CertificateException, NoSuchAlgorithmException, KeyStoreException,
    IOException, KeyManagementException {

0X509TrustManager x509TrustManager;
// https请求,需要校验证书
if (needVerifyCa) {
KeyStore keyStore = getKeyStore(caInputStream, cAalias);
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException(“Unexpected default trust managers:” + Arrays.toString(trustManagers));
}
x509TrustManager = (X509TrustManager) trustManagers[0];
// 这里传TLS或SSL其实都可以的
SSLContext sslContext = SSLContext.getInstance(“TLS”);
sslContext.init(null, new TrustManager[]{x509TrustManager}, new SecureRandom());
return new SSLConnectionSocketFactory(sslContext);
}
// https请求,不作证书校验
x509TrustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) {

}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) {
// 不验证
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0]
SSLContext sslContext = SSLContext.getInstance(“TLS”);
sslContext.init(null, new TrustManager[]{x509TrustManager}, new SecureRandom());
return new SSLConnectionSocketFactory(sslContext);
}
/**

  • 获取(密钥及证书)仓库
  • 注:该仓库用于存放 密钥以及证书
  • @param caInputStream
  • CA证书(此证书应由要访问的服务端提供)
  • @param cAalias
  • 别名
  • 注意:别名应该是唯一的, 别名不要和其他的别名一样,否者会覆盖之前的相同别名的证书信息。别名即key-value中的key。
  • @return 密钥、证书 仓库
  • @throws KeyStoreException 异常信息
  • @throws CertificateException 异常信息
  • @throws IOException 异常信息
  • @throws NoSuchAlgorithmException 异常信息
  • @date 2019/6/11 18:48
    */
    private static KeyStore getKeyStore(InputStream caInputStream, String cAalias)
    throws KeyStoreException, CertificateException, IOException, NoSuchAlgorithmException {
    // 证书工厂
    CertificateFactory certificateFactory = CertificateFactory.getInstance(“X.509”);
    // 秘钥仓库
    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(null);
    keyStore.setCertificateEntry(cAalias, certificateFactory.generateCertificate(caInputStream));
    return keyStore;
    }

灵活方便的传输文件
除了引入org.apache.httpcomponents基本的http依赖外额外引入org.apache.httpcomponents的httpmime依赖

org.apache.httpcomponents
httpmime
4.5.5

.
/**
*

  • 发送文件

  • multipart/form-data传递文件(及相关信息)

  • 注:如果想要灵活方便的传输文件的话,

  • 除了引入org.apache.httpcomponents基本的httpclient依赖外

  • 再额外引入org.apache.httpcomponents的httpmime依赖。

  • 追注:即便不引入httpmime依赖,也是能传输文件的,不过功能不够强大。

*/
@Test
public void test4() {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(“http://localhost:12345/file”);
CloseableHttpResponse response = null;
try {
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
// 第一个文件
String filesKey = “files”;
File file1 = new File(“C:\Users\JustryDeng\Desktop\back.jpg”);
multipartEntityBuilder.addBinaryBody(filesKey, file1);
// 第二个文件(多个文件的话,使用同一个key就行,后端用数组或集合进行接收即可)
File file2 = new File(“C:\Users\JustryDeng\Desktop\头像.jpg”);
// 防止服务端收到的文件名乱码。 我们这里可以先将文件名URLEncode,然后服务端拿到文件名时在URLDecode。就能避免乱码问题。
// 文件名其实是放在请求头的Content-Disposition里面进行传输的,如其值为form-data; name=“files”; filename=“头像.jpg”
multipartEntityBuilder.addBinaryBody(filesKey, file2, ContentType.DEFAULT_BINARY, URLEncoder.encode(file2.getName(), “utf-8”));
// 其它参数(注:自定义contentType,设置UTF-8是为了防止服务端拿到的参数出现乱码)
ContentType contentType = ContentType.create(“text/plain”, Charset.forName(“UTF-8”));
multipartEntityBuilder.addTextBody(“name”, “邓沙利文”, contentType);
multipartEntityBuilder.addTextBody(“age”, “25”, contentType);
HttpEntity httpEntity = multipartEntityBuilder.build();
httpPost.setEntity(httpEntity);
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
System.out.println(“HTTPS响应状态为:” + response.getStatusLine());
if (responseEntity != null) {
System.out.println(“HTTPS响应内容长度为:” + responseEntity.getContentLength());
// 主动设置编码,来防止响应乱码
String responseStr = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
System.out.println(“HTTPS响应内容为:” + responseStr);
}
} catch (ParseException | IOException e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}

接收端是这样的

发送流:
/**
*

  • 发送流

*/
@Test
public void test5() {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(“http://localhost:12345/is?name=邓沙利文”);
CloseableHttpResponse response = null;
try {
InputStream is = new ByteArrayInputStream(“流啊流~”.getBytes());
InputStreamEntity ise = new InputStreamEntity(is);
httpPost.setEntity(ise);
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
System.out.println(“HTTPS响应状态为:” + response.getStatusLine());
if (responseEntity != null) {
System.out.println(“HTTPS响应内容长度为:” + responseEntity.getContentLength());
// 主动设置编码,来防止响应乱码
String responseStr = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
System.out.println(“HTTPS响应内容为:” + responseStr);
}
} catch (ParseException | IOException e) {
e.printStackTrace();

} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}

}
在此期间我还参与了购物车模块的开发,在这个模块中我们还考虑了会员在登录和未登录情况下把商品保存到购物车后台如何保存商品信息,会不会发生冲突。当时采用了登录拦截器这个插件来判断用户是否登录,如果没有登录就将商品信息保存到cookie中,当用户登录后,再把商品持久到数据库中,但是考虑到cookie才4k存储量小就决定使用redis来缓存,用户在未登录状态下商品信息,在redis中设置缓存生存时间,如果在规定时间内没有登录,数据就会自动删除,如果用户在规定时间内登陆了便会使用activemq机制将数据同步到数据库中。
后面的内容,会继续完善,接着会有第三篇;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值