wangyunpeng 11 mesiacov pred
rodič
commit
11da696249
19 zmenil súbory, kde vykonal 1296 pridanie a 0 odobranie
  1. 1 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/Application.java
  2. 15 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/remote/NLPRemoteService.java
  3. 49 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/MapBuilder.java
  4. 78 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/FeiShu.java
  5. 70 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/FeishuMessageSender.java
  6. 37 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/FeishuChatListResult.java
  7. 62 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/FeishuGroup.java
  8. 48 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/Message.java
  9. 50 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/MessageParams.java
  10. 26 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/config/MessageConfig.java
  11. 17 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/element/BaseElement.java
  12. 24 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/element/DivElement.java
  13. 30 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/element/Filed.java
  14. 31 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/element/MarkDownText.java
  15. 14 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/header/HeadTitle.java
  16. 17 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/header/MessageHeader.java
  17. 604 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/http/HttpClientUtils.java
  18. 24 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/http/HttpDeleteExpand.java
  19. 99 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/http/HttpResponseContent.java

+ 1 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/Application.java

@@ -22,6 +22,7 @@ import org.springframework.context.annotation.EnableAspectJAutoProxy;
         "com.tzld.longarticle.recommend.server.remote",
         "com.tzld.longarticle.recommend.server.config",
         "com.tzld.longarticle.recommend.server.web",
+        "com.tzld.longarticle.recommend.server.util",
         "com.tzld.longarticle.recommend.server.repository",
 })
 @EnableAspectJAutoProxy

+ 15 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/remote/NLPRemoteService.java

@@ -3,6 +3,8 @@ package com.tzld.longarticle.recommend.server.remote;
 import com.alibaba.fastjson.JSONObject;
 import com.tzld.longarticle.recommend.server.common.HttpPoolFactory;
 import com.tzld.longarticle.recommend.server.model.Content;
+import com.tzld.longarticle.recommend.server.util.MapBuilder;
+import com.tzld.longarticle.recommend.server.util.feishu.FeishuMessageSender;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.http.HttpEntity;
 import org.apache.http.StatusLine;
@@ -11,6 +13,8 @@ import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.util.EntityUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;
 
@@ -25,6 +29,12 @@ import java.util.stream.Collectors;
 @Slf4j
 public class NLPRemoteService {
 
+    @Autowired
+    FeishuMessageSender feishuMessageSender;
+
+    @Value("${nlp.chat.id}")
+    private String chatId;
+
     private static CloseableHttpClient client = HttpPoolFactory.nlpPool();
     private static final String scoreListUrl = "http://61.48.133.26:6060/score_list";
     private static final String backUrl = "http://47.98.136.48:6060/score_list";
@@ -69,6 +79,11 @@ public class NLPRemoteService {
                     }
                 }
             } catch (Exception e) {
+                feishuMessageSender.sendChat(chatId, "4090计算评分失败",
+                        MapBuilder.<String, String>builder(new LinkedHashMap<>())
+                                .put("url", url)
+                                .put("账号名称", accountName)
+                                .build());
                 if (StringUtils.hasText(e.getMessage()) && e.getMessage().contains("Connection")) {
                     url = backUrl;
                     client = HttpPoolFactory.thirtySecondPool();

+ 49 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/MapBuilder.java

@@ -0,0 +1,49 @@
+package com.tzld.longarticle.recommend.server.util;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * @author: TanJingyu
+ * @create:2023-06-12 13:28:18
+ **/
+public class MapBuilder {
+
+    private MapBuilder() {}
+
+    public static <K,V> Builder<K,V> builder() {
+        return new Builder<>(null);
+    }
+
+    public static <K,V> Builder<K,V> builder(Map<K,V> map) {
+        return new Builder<>(map);
+    }
+
+    public static class Builder<K,V> {
+        private  Map<K,V> resultMap = new HashMap<>();
+
+        private Builder(Map<K,V> map) {
+            if(Objects.nonNull(map)){
+                resultMap = map;
+            }
+        }
+
+        public Builder<K,V> put(K k, V v) {
+            resultMap.put(k, v);
+            return this;
+        }
+
+        public Map<K,V> build() {
+            return resultMap;
+        }
+    }
+
+
+    public static void main(String[] args) {
+        Map<String, Object> build = MapBuilder.<String, Object>builder().put("name", "忘记")
+                .put("age", 23)
+                .build();
+        System.out.println(build);
+    }
+}

+ 78 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/FeiShu.java

@@ -0,0 +1,78 @@
+package com.tzld.longarticle.recommend.server.util.feishu;
+
+import com.alibaba.fastjson.JSONObject;
+import com.tzld.longarticle.recommend.server.util.feishu.model.FeishuChatListResult;
+import com.tzld.longarticle.recommend.server.util.feishu.model.FeishuGroup;
+import com.tzld.longarticle.recommend.server.util.http.HttpClientUtils;
+import com.tzld.longarticle.recommend.server.util.http.HttpResponseContent;
+import org.slf4j.LoggerFactory;
+import org.springframework.data.util.Pair;
+
+import java.util.HashMap;
+
+public class FeiShu {
+
+    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(FeiShu.class);
+
+    public static final String URL_FEISHU_ACCESSTOKEN = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal";
+    public static final String URL_FEISHU_CHAT_LIST = "https://open.feishu.cn/open-apis/chat/v4/list?page_size=200";
+    public static final String URL_FEISHU_SEND_MESSAGE = "https://open.feishu.cn/open-apis/message/v4/send";
+    public static final String URL_FEISHU_GROUP_MEMBERS = "https://open.feishu.cn/open-apis/im/v1/chats/:chat_id/members?member_id_type=open_id";
+    public static final String URL_FEISHU_REPLY_MESSAGE = "https://open.feishu.cn/open-apis/im/v1/messages/:message_id/reply";
+    public static final String URL_FEISHU_CARD_MESSAGE_UPDATE = "https://open.feishu.cn/open-apis/im/v1/messages/:message_id";
+    public static final String APPID = "cli_a22acf2916b8500e";
+    public static final String APPSECRET = "tE0xAB2gZTMlBGdPczCGLcmpRlZQm5CQ";
+
+    public static Pair<String, Integer> requestAccessToken() {
+        Pair<String, Integer> result = Pair.of("", 0);
+        long startTime = System.currentTimeMillis();
+        int retryCount = 0;
+
+        while (retryCount < 3) {
+            try {
+                HashMap<String, String> params = new HashMap<>();
+                params.put("app_id", APPID);
+                params.put("app_secret", APPSECRET);
+                HttpResponseContent hrc = HttpClientUtils.postForm(URL_FEISHU_ACCESSTOKEN, params, new HashMap<>());
+                LOGGER.info("feishu accessToken response = {}", hrc.getBodyContent());
+                JSONObject response = JSONObject.parseObject(hrc.getBodyContent());
+                String accessToken = response.getString("tenant_access_token");
+                int expireSeconds = response.getIntValue("expire");
+                return Pair.of(accessToken, expireSeconds);
+            } catch (Exception e) {
+                LOGGER.error("requestFeishuAccessToken, error ,retryCount = {}, cost = {}", retryCount, System.currentTimeMillis() - startTime, e);
+            }
+            retryCount++;
+        }
+
+        return result;
+
+    }
+
+    public static void main(String[] args) {
+        HashMap<String, String> params = new HashMap<>();
+        params.put("app_id", APPID);
+        params.put("app_secret", APPSECRET);
+        HttpResponseContent hrc = HttpClientUtils.postForm(URL_FEISHU_ACCESSTOKEN, params, new HashMap<>());
+        System.out.println(hrc.getBodyContent());
+        JSONObject response = JSONObject.parseObject(hrc.getBodyContent());
+        String accessToken = response.getString("tenant_access_token");
+
+        HashMap<String, String> header = new HashMap<>();
+        header.put("Authorization", "Bearer " + accessToken);
+        header.put("content-type", "application/json; charset=utf-8");
+
+        //获取群聊ID
+        HttpResponseContent chatHrc = HttpClientUtils.get(URL_FEISHU_CHAT_LIST, header);
+        JSONObject responseJSON = JSONObject.parseObject(chatHrc.getBodyContent());
+        String chatData = responseJSON.getString("data");
+        System.out.println(chatData);
+        System.out.println("-------------------");
+        FeishuChatListResult feishuChatListResult = JSONObject.parseObject(chatData, FeishuChatListResult.class);
+        for (FeishuGroup group : feishuChatListResult.getGroups()) {
+            if ("重要!Ai Dit公众号推送失败告警群".equals(group.getName())) {
+                System.out.println(group.getChat_id());
+            }
+        }
+    }
+}

+ 70 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/FeishuMessageSender.java

@@ -0,0 +1,70 @@
+package com.tzld.longarticle.recommend.server.util.feishu;
+
+import com.tzld.longarticle.recommend.server.util.MapBuilder;
+import com.tzld.longarticle.recommend.server.util.feishu.model.Message;
+import com.tzld.longarticle.recommend.server.util.feishu.model.MessageParams;
+import com.tzld.longarticle.recommend.server.util.feishu.model.config.MessageConfig;
+import com.tzld.longarticle.recommend.server.util.feishu.model.element.BaseElement;
+import com.tzld.longarticle.recommend.server.util.feishu.model.element.DivElement;
+import com.tzld.longarticle.recommend.server.util.feishu.model.element.Filed;
+import com.tzld.longarticle.recommend.server.util.feishu.model.element.MarkDownText;
+import com.tzld.longarticle.recommend.server.util.feishu.model.header.HeadTitle;
+import com.tzld.longarticle.recommend.server.util.feishu.model.header.MessageHeader;
+import com.tzld.longarticle.recommend.server.util.http.HttpClientUtils;
+import com.tzld.longarticle.recommend.server.util.http.HttpResponseContent;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.util.TextUtils;
+import org.slf4j.LoggerFactory;
+import org.springframework.data.util.Pair;
+import org.springframework.stereotype.Component;
+
+import java.util.*;
+
+@Component
+@Slf4j
+public class FeishuMessageSender {
+
+    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(FeishuMessageSender.class);
+
+    public void sendChat(String chatId, String title, Map<String, String> infos) {
+        sendChat(chatId, null, title, infos);
+    }
+
+    public void sendChat(String chatId, String userOpenId, String title, Map<String, String> infos) {
+        try {
+            Pair<String, Integer> token = FeiShu.requestAccessToken();
+            HashMap<String, String> header = new HashMap<>();
+            header.put("Authorization", "Bearer " + token.getFirst());
+            header.put("content-type", "application/json; charset=utf-8");
+
+            MessageConfig messageConfig = new MessageConfig();
+            MessageHeader messageHeader = new MessageHeader();
+            messageHeader.title = new HeadTitle(title);
+            List<BaseElement> elements = new ArrayList<>();
+            DivElement textElement = new DivElement();
+            for (Map.Entry<String, String> stringStringEntry : infos.entrySet()) {
+                if (TextUtils.isBlank(stringStringEntry.getValue())) {
+                    textElement.fields.add(new Filed(new MarkDownText(String.format("%s", stringStringEntry.getKey()))));
+                } else {
+                    textElement.fields.add(new Filed(new MarkDownText(String.format("**%s**:%s", stringStringEntry.getKey(), stringStringEntry.getValue()))));
+                }
+            }
+            elements.add(textElement);
+            MessageParams messageParams = new MessageParams(chatId, userOpenId, new Message(messageConfig, messageHeader, elements));
+            HttpResponseContent hrc = HttpClientUtils.postRequestBody(FeiShu.URL_FEISHU_SEND_MESSAGE, messageParams, header);
+        } catch (Exception e) {
+            LOGGER.error("FeishuMessageSender", e);
+        }
+    }
+
+
+    public static void main(String[] args) {
+        FeishuMessageSender sender = new FeishuMessageSender();
+        sender.sendChat("oc_b20f013b336e466fa572bc04aab13aba", "4090计算评分失败Test",
+                MapBuilder.<String, String>builder(new LinkedHashMap<>())
+                        .put("url", "http://61.48.133.26:6060/score_list")
+                        .put("账号名称", "xxxxxx")
+                        .build());
+
+    }
+}

+ 37 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/FeishuChatListResult.java

@@ -0,0 +1,37 @@
+package com.tzld.longarticle.recommend.server.util.feishu.model;
+
+import java.util.List;
+
+/**
+ * Create by nieqi on 2021/7/8
+ */
+public class FeishuChatListResult {
+
+    private boolean has_more;
+    private String page_token;
+    private List<FeishuGroup> groups;
+
+    public boolean isHas_more() {
+        return has_more;
+    }
+
+    public void setHas_more(boolean has_more) {
+        this.has_more = has_more;
+    }
+
+    public String getPage_token() {
+        return page_token;
+    }
+
+    public void setPage_token(String page_token) {
+        this.page_token = page_token;
+    }
+
+    public List<FeishuGroup> getGroups() {
+        return groups;
+    }
+
+    public void setGroups(List<FeishuGroup> groups) {
+        this.groups = groups;
+    }
+}

+ 62 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/FeishuGroup.java

@@ -0,0 +1,62 @@
+package com.tzld.longarticle.recommend.server.util.feishu.model;
+
+/**
+ * Create by nieqi on 2021/7/8
+ */
+public class FeishuGroup {
+
+    private String avatar;
+    private String description;
+    private String chat_id;
+    private String name;
+    private String owner_open_id;
+    private String owner_user_id;
+
+    public String getAvatar() {
+        return avatar;
+    }
+
+    public void setAvatar(String avatar) {
+        this.avatar = avatar;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public String getChat_id() {
+        return chat_id;
+    }
+
+    public void setChat_id(String chat_id) {
+        this.chat_id = chat_id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getOwner_open_id() {
+        return owner_open_id;
+    }
+
+    public void setOwner_open_id(String owner_open_id) {
+        this.owner_open_id = owner_open_id;
+    }
+
+    public String getOwner_user_id() {
+        return owner_user_id;
+    }
+
+    public void setOwner_user_id(String owner_user_id) {
+        this.owner_user_id = owner_user_id;
+    }
+}

+ 48 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/Message.java

@@ -0,0 +1,48 @@
+package com.tzld.longarticle.recommend.server.util.feishu.model;
+
+
+import com.tzld.longarticle.recommend.server.util.feishu.model.config.MessageConfig;
+import com.tzld.longarticle.recommend.server.util.feishu.model.element.BaseElement;
+import com.tzld.longarticle.recommend.server.util.feishu.model.header.MessageHeader;
+
+import java.util.List;
+
+/**
+ * Create by nieqi on 2021/7/9
+ */
+public class Message {
+
+    private MessageConfig config;
+    private MessageHeader header;
+    private List<BaseElement> elements;
+
+    public Message(MessageConfig config, MessageHeader header, List<BaseElement> elements) {
+        this.config = config;
+        this.header = header;
+        this.elements = elements;
+    }
+
+    public MessageConfig getConfig() {
+        return config;
+    }
+
+    public void setConfig(MessageConfig config) {
+        this.config = config;
+    }
+
+    public MessageHeader getHeader() {
+        return header;
+    }
+
+    public void setHeader(MessageHeader header) {
+        this.header = header;
+    }
+
+    public List<BaseElement> getElements() {
+        return elements;
+    }
+
+    public void setElements(List<BaseElement> elements) {
+        this.elements = elements;
+    }
+}

+ 50 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/MessageParams.java

@@ -0,0 +1,50 @@
+package com.tzld.longarticle.recommend.server.util.feishu.model;
+
+/**
+ * Create by nieqi on 2021/7/9
+ */
+public class MessageParams {
+
+    private String chat_id;
+    private String open_id;
+    private String msg_type = "interactive";
+    private Message card;
+
+    public MessageParams(String chat_id, String open_id, Message card) {
+        this.chat_id = chat_id;
+        this.open_id = open_id;
+        this.card = card;
+    }
+
+    public String getOpen_id() {
+        return open_id;
+    }
+
+    public void setOpen_id(String open_id) {
+        this.open_id = open_id;
+    }
+
+    public void setChat_id(String chat_id) {
+        this.chat_id = chat_id;
+    }
+
+    public void setMsg_type(String msg_type) {
+        this.msg_type = msg_type;
+    }
+
+    public void setCard(Message card) {
+        this.card = card;
+    }
+
+    public String getChat_id() {
+        return chat_id;
+    }
+
+    public String getMsg_type() {
+        return msg_type;
+    }
+
+    public Message getCard() {
+        return card;
+    }
+}

+ 26 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/config/MessageConfig.java

@@ -0,0 +1,26 @@
+package com.tzld.longarticle.recommend.server.util.feishu.model.config;
+
+/**
+ * Create by nieqi on 2021/7/9
+ */
+public class MessageConfig {
+
+    boolean wide_screen_mode = true;
+    boolean enable_forward = true;
+
+    public boolean isWide_screen_mode() {
+        return wide_screen_mode;
+    }
+
+    public void setWide_screen_mode(boolean wide_screen_mode) {
+        this.wide_screen_mode = wide_screen_mode;
+    }
+
+    public boolean isEnable_forward() {
+        return enable_forward;
+    }
+
+    public void setEnable_forward(boolean enable_forward) {
+        this.enable_forward = enable_forward;
+    }
+}

+ 17 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/element/BaseElement.java

@@ -0,0 +1,17 @@
+package com.tzld.longarticle.recommend.server.util.feishu.model.element;
+
+/**
+ * Create by nieqi on 2021/7/9
+ */
+public class BaseElement {
+
+    public String tag = "";
+
+    public String getTag() {
+        return tag;
+    }
+
+    public void setTag(String tag) {
+        this.tag = tag;
+    }
+}

+ 24 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/element/DivElement.java

@@ -0,0 +1,24 @@
+package com.tzld.longarticle.recommend.server.util.feishu.model.element;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Create by nieqi on 2021/7/9
+ */
+public class DivElement extends BaseElement {
+
+    public DivElement() {
+        this.tag = "div";
+    }
+
+    public List<Filed> fields = new ArrayList<>();
+
+    public List<Filed> getFields() {
+        return fields;
+    }
+
+    public void setFields(List<Filed> fields) {
+        this.fields = fields;
+    }
+}

+ 30 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/element/Filed.java

@@ -0,0 +1,30 @@
+package com.tzld.longarticle.recommend.server.util.feishu.model.element;
+
+/**
+ * Create by nieqi on 2021/7/9
+ */
+public class Filed {
+
+    boolean is_short = false;
+    MarkDownText text;
+
+    public Filed(MarkDownText text) {
+        this.text = text;
+    }
+
+    public boolean isIs_short() {
+        return is_short;
+    }
+
+    public void setIs_short(boolean is_short) {
+        this.is_short = is_short;
+    }
+
+    public MarkDownText getText() {
+        return text;
+    }
+
+    public void setText(MarkDownText text) {
+        this.text = text;
+    }
+}

+ 31 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/element/MarkDownText.java

@@ -0,0 +1,31 @@
+package com.tzld.longarticle.recommend.server.util.feishu.model.element;
+
+/**
+ * Create by nieqi on 2021/7/9
+ */
+public class MarkDownText {
+
+    private String tag = "lark_md";
+
+    String content = "";
+
+    public MarkDownText(String content) {
+        this.content = content;
+    }
+
+    public String getTag() {
+        return tag;
+    }
+
+    public void setTag(String tag) {
+        this.tag = tag;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
+    }
+}

+ 14 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/header/HeadTitle.java

@@ -0,0 +1,14 @@
+package com.tzld.longarticle.recommend.server.util.feishu.model.header;
+
+/**
+ * Create by nieqi on 2021/7/9
+ */
+public class HeadTitle {
+
+    public String tag = "plain_text";
+    public String content;
+
+    public HeadTitle(String content) {
+        this.content = content;
+    }
+}

+ 17 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/feishu/model/header/MessageHeader.java

@@ -0,0 +1,17 @@
+package com.tzld.longarticle.recommend.server.util.feishu.model.header;
+
+/**
+ * Create by nieqi on 2021/7/9
+ */
+public class MessageHeader {
+
+    public HeadTitle title;
+
+    public HeadTitle getTitle() {
+        return title;
+    }
+
+    public void setTitle(HeadTitle title) {
+        this.title = title;
+    }
+}

+ 604 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/http/HttpClientUtils.java

@@ -0,0 +1,604 @@
+package com.tzld.longarticle.recommend.server.util.http;
+
+import com.alibaba.fastjson.JSONObject;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.http.Consts;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpHeaders;
+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.*;
+import org.apache.http.config.*;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.socket.PlainConnectionSocketFactory;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.ssl.SSLContexts;
+import org.apache.http.util.EntityUtils;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.nio.charset.CodingErrorAction;
+import java.security.KeyManagementException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+import java.util.*;
+import java.util.Map.Entry;
+
+/**
+ * 基于apache httpclient4.5.5 的HTTP工具类
+ *
+ */
+public class HttpClientUtils {
+
+    static Log log = LogFactory.getLog(HttpClientUtils.class);
+
+    private static CloseableHttpClient client;
+    private static RequestConfig requestConfigDefault;
+    private static PoolingHttpClientConnectionManager connManager = null;
+    // 默认请求获取数据的超时时间
+    private static Integer readTimeoutDefault = 150000;
+    // 默认连接超时时间
+    private static Integer connectTimeoutDefault = 50000;
+    // 默认从connectManager获取Connection超时时间
+    private static Integer getConnectionTimeoutDefault = 1000;
+    // 默认的字符集
+    private static String charsetDefault = "UTF-8";
+
+    public static final String contentTypeJson = "application/json";
+    public static final String contentTypeXml = "application/xml";
+
+    private enum MethodType {
+        GET, POST
+    }
+
+    static {
+        try {
+            SSLContext sslContext = SSLContexts.createDefault();
+            sslContext.init(null, new TrustManager[]{new X509TrustManager() {
+
+                public void checkClientTrusted(X509Certificate[] arg0, String arg1)
+                        throws CertificateException {
+                }
+
+                public void checkServerTrusted(X509Certificate[] arg0, String arg1)
+                        throws CertificateException {
+                }
+
+                public X509Certificate[] getAcceptedIssuers() {
+                    return null;
+                }
+            }}, null);
+            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
+                    .register("http", PlainConnectionSocketFactory.INSTANCE)
+                    .register("https", new SSLConnectionSocketFactory(sslContext)).build();
+            connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
+            SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).build();
+            connManager.setDefaultSocketConfig(socketConfig);
+            MessageConstraints messageConstraints = MessageConstraints.custom().build();
+            ConnectionConfig connectionConfig = ConnectionConfig.custom()
+                    .setMalformedInputAction(CodingErrorAction.IGNORE)
+                    .setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8)
+                    .setMessageConstraints(messageConstraints).build();
+            connManager.setDefaultConnectionConfig(connectionConfig);
+            connManager.setMaxTotal(1000);
+            connManager.setDefaultMaxPerRoute(500);
+            connManager.setValidateAfterInactivity(3000);
+        } catch (KeyManagementException e) {
+            log.error(e);
+        }
+
+        requestConfigDefault = RequestConfig.custom().setConnectionRequestTimeout(getConnectionTimeoutDefault)
+                .setConnectTimeout(connectTimeoutDefault).setSocketTimeout(readTimeoutDefault).build();
+        client = HttpClients.custom().useSystemProperties().setConnectionManager(connManager).setDefaultRequestConfig(requestConfigDefault)
+                .build();
+
+    }
+
+    /**
+     * get请求
+     *
+     * @param url 请求的url
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    public static HttpResponseContent get(String url) {
+        return get(url, null, connectTimeoutDefault, readTimeoutDefault, null);
+    }
+
+    /**
+     * @param url     请求的url
+     * @param headers 请求需要携带的header信息
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    public static HttpResponseContent get(String url, Map<String, String> headers) {
+        return get(url, null, connectTimeoutDefault, readTimeoutDefault, headers);
+    }
+
+    /**
+     * get请求
+     *
+     * @param url         请求的url
+     * @param contentType contentType,例如:text/plain
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    public static HttpResponseContent get(String url, String contentType) {
+        return get(url, contentType, connectTimeoutDefault, readTimeoutDefault, null);
+    }
+
+    /**
+     * get请求
+     *
+     * @param url            请求的url
+     * @param connectTimeout 连接超时设置,毫秒
+     * @param readTimeout    读取超时设置,毫秒
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    public static HttpResponseContent get(String url, int connectTimeout, int readTimeout) {
+        return get(url, null, connectTimeout, readTimeout, null);
+    }
+
+    /**
+     * get请求
+     *
+     * @param url            请求的url
+     * @param contentType    contentType,例如:text/plain
+     * @param connectTimeout 连接超时设置,毫秒
+     * @param readTimeout    读取超时设置,毫秒
+     * @param headers        请求需要携带的header信息
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    public static HttpResponseContent get(String url, String contentType, int connectTimeout, int readTimeout, Map<String, String> headers) {
+        return getOrPostUrl(MethodType.GET, url, contentType, connectTimeout, readTimeout, headers);
+    }
+
+    /**
+     * post请求,参数在url中
+     *
+     * @param url 请求的url
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    public static HttpResponseContent postUrl(String url) {
+        return postUrl(url, null, connectTimeoutDefault, readTimeoutDefault);
+    }
+
+    /**
+     * post请求,参数在url中
+     *
+     * @param url         请求的url
+     * @param contentType contentType,例如:text/plain
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    public static HttpResponseContent postUrl(String url, String contentType) {
+        return postUrl(url, contentType, connectTimeoutDefault, readTimeoutDefault);
+    }
+
+    /**
+     * post请求,参数在url中
+     *
+     * @param url            请求的url
+     * @param connectTimeout 连接超时设置,毫秒
+     * @param readTimeout    读取超时设置,毫秒
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    public static HttpResponseContent postUrl(String url, int connectTimeout, int readTimeout) {
+        return postUrl(url, null, connectTimeout, readTimeout);
+    }
+
+    /**
+     * post请求,参数在url中
+     *
+     * @param url            请求的url
+     * @param contentType    contentType,例如:text/plain
+     * @param connectTimeout 连接超时设置,毫秒
+     * @param readTimeout    读取超时设置,毫秒
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    public static HttpResponseContent postUrl(String url, String contentType, int connectTimeout, int readTimeout) {
+        return getOrPostUrl(MethodType.POST, url, contentType, connectTimeout, readTimeout);
+    }
+
+    /**
+     * get或者post请求,参数在url中
+     *
+     * @param methodType     GET/POST
+     * @param url            请求的url
+     * @param contentType    contentType,例如:text/plain
+     * @param connectTimeout 连接超时设置,毫秒
+     * @param readTimeout    读取超时设置,毫秒
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    private static HttpResponseContent getOrPostUrl(MethodType methodType, String url, String contentType,
+                                                    int connectTimeout, int readTimeout) {
+        return getOrPostUrl(methodType, url, contentType, connectTimeout, readTimeout, null);
+    }
+
+    private static HttpResponseContent getOrPostUrl(MethodType methodType, String url, String contentType,
+                                                    int connectTimeout, int readTimeout, Map<String, String> extHeaders) {
+        HttpRequestBase request = null;
+        HttpResponseContent hrc = null;
+        if (methodType == MethodType.GET) {
+            request = new HttpGet(url);
+        } else {
+            request = new HttpPost(url);
+        }
+        // 设置contentType
+        if (contentType != null) {
+            request.addHeader(HttpHeaders.CONTENT_TYPE, contentType);
+        }
+        //add by nieqi since 2022-3-23
+        if (Objects.nonNull(extHeaders)) {
+            for (String s : extHeaders.keySet()) {
+                if (Objects.nonNull(s)) {
+                    String headerValue = extHeaders.get(s);
+                    if (Objects.nonNull(headerValue)) {
+                        request.addHeader(s, headerValue);
+                    }
+                }
+            }
+        }
+        hrc = executeHttpRequest(request, connectTimeout, readTimeout);
+        return hrc;
+    }
+
+    /**
+     * post请求,请求数据在data中
+     *
+     * @param url  请求的url
+     * @param data 请求数据
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    public static HttpResponseContent postData(String url, String data) {
+        return postData(url, data, null, connectTimeoutDefault, readTimeoutDefault);
+
+    }
+
+    /**
+     * post请求,请求数据在data中
+     *
+     * @param url         请求的url
+     * @param data        请求数据
+     * @param contentType contentType,例如:text/plain
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    public static HttpResponseContent postData(String url, String data, String contentType) {
+        return postData(url, data, contentType, connectTimeoutDefault, readTimeoutDefault);
+    }
+
+    /**
+     * post请求,请求数据在data中
+     *
+     * @param url            请求的url
+     * @param data           请求数据
+     * @param connectTimeout 连接超时设置,毫秒
+     * @param readTimeout    读取超时设置,毫秒
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    public static HttpResponseContent postData(String url, String data, int connectTimeout, int readTimeout) {
+        return postData(url, data, null, connectTimeout, readTimeout);
+    }
+
+    /**
+     * post请求,请求数据在data中
+     *
+     * @param url            请求的url
+     * @param data           请求数据
+     * @param contentType    contentType,例如:text/plain
+     * @param connectTimeout 连接超时设置,毫秒
+     * @param readTimeout    读取超时设置,毫秒
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    public static HttpResponseContent postData(String url, String data, String contentType, int connectTimeout,
+                                               int readTimeout) {
+        Map<String, String> headerMap = new HashMap<String, String>();
+        headerMap.put(HttpHeaders.CONTENT_TYPE, contentType);
+        return postDataAddHeader(url, data, headerMap, connectTimeout, readTimeout);
+
+    }
+
+    /**
+     * post请求,请求数据在data中
+     *
+     * @param url            请求的url
+     * @param data           请求数据
+     * @param headerMap      http请求头,key-value放在map中
+     * @param connectTimeout 连接超时设置,毫秒
+     * @param readTimeout    读取超时设置,毫秒
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    public static HttpResponseContent postDataAddHeader(String url, String data, Map<String, String> headerMap,
+                                                        int connectTimeout, int readTimeout) {
+        HttpRequestBase request = null;
+        HttpResponseContent hrc = null;
+        HttpPost httpPost = new HttpPost(url);
+        httpPost.setEntity(new StringEntity(data, charsetDefault));
+        request = httpPost;
+        // 设置header
+        if (headerMap != null) {
+            for (Entry<String, String> entry : headerMap.entrySet()) {
+                request.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
+        hrc = executeHttpRequest(request, connectTimeout, readTimeout);
+        return hrc;
+
+    }
+
+    /**
+     * post请求,form表单,不能包含二进制数据
+     *
+     * @param url       请求的url
+     * @param paramsMap form表单参数map
+     * @param headerMap form表单header
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    public static HttpResponseContent postForm(String url, Map<String, String> paramsMap, Map<String, String> headerMap) {
+        return postForm(url, paramsMap, connectTimeoutDefault, readTimeoutDefault, headerMap);
+    }
+
+    /**
+     * post请求,form表单,不能包含二进制数据
+     *
+     * @param url            请求的url
+     * @param paramsMap      form表单参数map
+     * @param connectTimeout 连接超时设置,毫秒
+     * @param readTimeout    读取超时设置,毫秒
+     * @param headerMap      form表单header
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    public static HttpResponseContent postForm(String url, Map<String, String> paramsMap, int connectTimeout,
+                                               int readTimeout, Map<String, String> headerMap) {
+        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
+        if (Objects.nonNull(paramsMap)) {
+            Iterator<String> iterator = paramsMap.keySet().iterator();
+            while (iterator.hasNext()) {
+                String key = iterator.next();
+                Object value = paramsMap.get(key);
+                nvps.add(new BasicNameValuePair(key, String.valueOf(value)));
+            }
+        }
+        return postForm(url, nvps, connectTimeout, readTimeout, headerMap);
+    }
+
+    /**
+     * post请求,form表单,不能包含二进制数据
+     *
+     * @param url            请求的url
+     * @param nvps      form表单参数
+     * @param connectTimeout 连接超时设置,毫秒
+     * @param readTimeout    读取超时设置,毫秒
+     * @param headerMap      form表单header
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    public static HttpResponseContent postForm(String url, List<NameValuePair> nvps, int connectTimeout,
+                                               int readTimeout, Map<String, String> headerMap) {
+        HttpRequestBase request = null;
+        HttpResponseContent hrc = null;
+        HttpPost httpPost = new HttpPost(url);
+        // 设置header
+        if (headerMap != null) {
+            for (Entry<String, String> entry : headerMap.entrySet()) {
+                httpPost.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
+        try {
+            httpPost.setEntity(new UrlEncodedFormEntity(nvps, charsetDefault));
+        } catch (UnsupportedEncodingException e) {
+            log.error(e);
+        }
+        request = httpPost;
+        hrc = executeHttpRequest(request, connectTimeout, readTimeout);
+        return hrc;
+    }
+
+    public static HttpResponseContent postRequestBody(String url, Object s, Map<String, String> headerMap) {
+        HttpRequestBase request = null;
+        HttpResponseContent hrc = null;
+        HttpPost httpPost = new HttpPost(url);
+
+        StringEntity stringEntity = new StringEntity((s instanceof String) ? (String) s : JSONObject.toJSONString(s), Consts.UTF_8);
+        stringEntity.setContentEncoding(Consts.UTF_8.name());
+        stringEntity.setContentType("application/json");
+        httpPost.setEntity(stringEntity);
+        // 设置header
+        if (headerMap != null) {
+            for (Entry<String, String> entry : headerMap.entrySet()) {
+                httpPost.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
+        request = httpPost;
+        hrc = executeHttpRequest(request, connectTimeoutDefault, readTimeoutDefault);
+        return hrc;
+    }
+
+    public static HttpResponseContent patchRequestBody(String url, Object s, Map<String, String> headerMap) {
+        HttpRequestBase request = null;
+        HttpResponseContent hrc = null;
+        HttpPatch httpPost = new HttpPatch(url);
+
+        StringEntity stringEntity = new StringEntity((s instanceof String) ? (String) s : JSONObject.toJSONString(s), Consts.UTF_8);
+        stringEntity.setContentEncoding(Consts.UTF_8.name());
+        stringEntity.setContentType("application/json");
+        httpPost.setEntity(stringEntity);
+        // 设置header
+        if (headerMap != null) {
+            for (Entry<String, String> entry : headerMap.entrySet()) {
+                httpPost.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
+        request = httpPost;
+        hrc = executeHttpRequest(request, connectTimeoutDefault, readTimeoutDefault);
+        return hrc;
+    }
+
+    public static HttpResponseContent deleteRequestBody(String url, Object s, HashMap<String, String> headerMap) {
+        HttpRequestBase request = null;
+        HttpResponseContent hrc = null;
+        HttpDeleteExpand httpDelete = new HttpDeleteExpand(url);
+
+        StringEntity stringEntity = new StringEntity(JSONObject.toJSONString(s), Consts.UTF_8);
+        stringEntity.setContentEncoding(Consts.UTF_8.name());
+        stringEntity.setContentType("application/json");
+        httpDelete.setEntity(stringEntity);
+        // 设置header
+        if (headerMap != null) {
+            for (Entry<String, String> entry : headerMap.entrySet()) {
+                httpDelete.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
+        request = httpDelete;
+        hrc = executeHttpRequest(request, connectTimeoutDefault, readTimeoutDefault);
+        return hrc;
+    }
+
+    public static HttpResponseContent putRequestBody(String url, Object s, HashMap<String, String> headerMap) {
+        HttpRequestBase request = null;
+        HttpResponseContent hrc = null;
+        HttpPut httpPut = new HttpPut(url);
+
+        StringEntity stringEntity = new StringEntity(JSONObject.toJSONString(s), Consts.UTF_8);
+        stringEntity.setContentEncoding(Consts.UTF_8.name());
+        stringEntity.setContentType("application/json");
+        httpPut.setEntity(stringEntity);
+        // 设置header
+        if (headerMap != null) {
+            for (Entry<String, String> entry : headerMap.entrySet()) {
+                httpPut.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
+        request = httpPut;
+        hrc = executeHttpRequest(request, connectTimeoutDefault, readTimeoutDefault);
+        return hrc;
+    }
+
+    /**
+     * 执行Http请求
+     *
+     * @param request
+     * @param connectTimeout
+     * @param readTimeout
+     * @return HttpResponseContent对象,如果http请求出现异常,返回null
+     */
+    private static HttpResponseContent executeHttpRequest(HttpRequestBase request, int connectTimeout,
+                                                          int readTimeout) {
+        CloseableHttpResponse response = null;
+        HttpResponseContent hrc = null;
+        try {
+            // 设置请求配置
+            RequestConfig.Builder configBuilder = RequestConfig.custom();
+            // 设置连接超时
+            configBuilder.setConnectTimeout(connectTimeout);
+            // 设置读取超时
+            configBuilder.setSocketTimeout(readTimeout);
+            // 设置从连接池获取连接实例的超时
+            configBuilder.setConnectionRequestTimeout(getConnectionTimeoutDefault);
+            RequestConfig requestConfig = configBuilder.build();
+            request.setConfig(requestConfig);
+            log.debug("开始执行Http请求, uri:" + request.getURI());
+            response = client.execute(request);
+            hrc = getHttpResponseContent(response);
+            return hrc;
+        } catch (Exception e) {
+            log.error("执行Http请求异常, uri:" + request.getURI(), e);
+        } finally {
+            close(request, response);
+        }
+        return hrc;
+
+    }
+
+    /**
+     * 封装HTTP响应报文
+     *
+     * @param response
+     * @return
+     */
+    private static HttpResponseContent getHttpResponseContent(CloseableHttpResponse response) {
+        // 获取响应实体
+        HttpEntity entity = response.getEntity();
+        if (entity == null) {
+            return null;
+        }
+        HttpResponseContent hrc = new HttpResponseContent();
+        hrc.setHeaders(response.getAllHeaders());
+        hrc.setStatusCode(response.getStatusLine().getStatusCode());
+        ContentType contentType = ContentType.getOrDefault(entity);
+        hrc.setMimeType(contentType.getMimeType());
+        if (contentType.getCharset() != null) {
+            hrc.setCharsetName(contentType.getCharset().name());
+        }
+        try {
+            hrc.setContentBytes(EntityUtils.toByteArray(entity));
+        } catch (IOException e) {
+            log.error("封装HTTP响应报文异常", e);
+        }
+        return hrc;
+
+    }
+
+    /**
+     * 关闭资源
+     *
+     * @param request
+     * @param response
+     */
+    private static void close(HttpRequestBase request, CloseableHttpResponse response) {
+        try {
+            if (request != null)
+                request.releaseConnection();
+            if (response != null)
+                response.close();
+        } catch (Exception e) {
+            log.error("关闭资源异常", e);
+        }
+    }
+
+    /**
+     * url编码
+     *
+     * @param url
+     * @param charset
+     * @return
+     */
+    public static String encodeURL(String url, String charset) {
+        if (url == null || charset == null) {
+            return null;
+        }
+        int p = url.indexOf("?");
+        if (p < 0) {
+            return url;
+        }
+        StringBuilder sb = new StringBuilder();
+        String preStr = url.substring(0, p + 1);
+        sb.append(preStr);
+        String queryStr = url.substring(p + 1, url.length());
+        String[] array = queryStr.split("&");
+        for (int i = 0; i < array.length; i++) {
+            String str = array[i];
+            int pp = str.indexOf("=");
+            if (pp > -1) {
+                sb.append(str.substring(0, pp + 1));
+                try {
+                    sb.append(URLEncoder.encode(str.substring(pp + 1, str.length()), charset));
+                } catch (UnsupportedEncodingException e) {
+                    e.printStackTrace();
+                }
+                if (i < array.length - 1) {
+                    sb.append("&");
+                }
+            }
+        }
+        return sb.toString();
+    }
+
+}

+ 24 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/http/HttpDeleteExpand.java

@@ -0,0 +1,24 @@
+package com.tzld.longarticle.recommend.server.util.http;
+
+import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
+
+import java.net.URI;
+
+public class HttpDeleteExpand extends HttpEntityEnclosingRequestBase {
+    public static final String METHOD_NAME = "DELETE";
+
+    public HttpDeleteExpand() {
+    }
+
+    public HttpDeleteExpand(URI uri) {
+        this.setURI(uri);
+    }
+
+    public HttpDeleteExpand(String uri) {
+        this.setURI(URI.create(uri));
+    }
+
+    public String getMethod() {
+        return "DELETE";
+    }
+}

+ 99 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/http/HttpResponseContent.java

@@ -0,0 +1,99 @@
+package com.tzld.longarticle.recommend.server.util.http;
+
+import org.apache.http.Header;
+
+import java.io.UnsupportedEncodingException;
+
+/**
+ * HTTP请求响应报文封装类
+ * 
+ */
+public class HttpResponseContent {
+
+	private int statusCode; // 响应状态码
+	private String mimeType; // mime类型
+	private String charsetName; // 字符集
+	private byte[] contentBytes; // 响应报文主体内容的二进制数据
+	private Header[] headers; // 响应头
+
+	/**
+	 * 获取响应报文主体内容,字符串,使用响应报文中的字符集编码
+	 * 
+	 * @return
+	 */
+	public String getBodyContent() {
+		return this.getBodyContent(charsetName);
+	}
+
+	/**
+	 * 获取响应报文主体内容,字符串
+	 * 
+	 * @param charsetName
+	 *            指定的字符编码
+	 * @return
+	 */
+	public String getBodyContent(String charsetName) {
+		String str = null;
+		if (charsetName == null) {
+			charsetName = "UTF-8";
+		}
+		try {
+			str = new String(contentBytes, charsetName);
+		} catch (UnsupportedEncodingException e) {
+			// 忽略异常
+			e.printStackTrace();
+		}
+		return str;
+	}
+
+	public boolean isSuccessful(){
+		return statusCode == 200;
+	}
+
+	public int getStatusCode() {
+		return statusCode;
+	}
+
+	public void setStatusCode(int statusCode) {
+		this.statusCode = statusCode;
+	}
+
+	public String getMimeType() {
+		return mimeType;
+	}
+
+	public void setMimeType(String mimeType) {
+		this.mimeType = mimeType;
+	}
+
+	public String getCharsetName() {
+		return charsetName;
+	}
+
+	public void setCharsetName(String charsetName) {
+		this.charsetName = charsetName;
+	}
+
+	public byte[] getContentBytes() {
+		return contentBytes;
+	}
+
+	public void setContentBytes(byte[] contentBytes) {
+		this.contentBytes = contentBytes;
+	}
+
+	public Header[] getHeaders() {
+		return headers;
+	}
+
+	public void setHeaders(Header[] headers) {
+		this.headers = headers;
+	}
+
+	@Override
+	public String toString() {
+		return "HttpResponseContent [statusCode=" + statusCode + ", mimeType=" + mimeType + ", charsetName=" + charsetName
+				+ ", bodyContent=" + getBodyContent() + "]";
+	}
+
+}