httpClient工具类

    技术2023-06-30  81

    github地址:https://github.com/Arronlong/httpclientutil

    maven:

    <!-- https://mvnrepository.com/artifact/com.arronlong/httpclientutil --> <dependency> <groupId>com.arronlong</groupId> <artifactId>httpclientutil</artifactId> <version>1.0.4</version> </dependency> import com.arronlong.httpclientutil.HttpClientUtil; import com.arronlong.httpclientutil.builder.HCB; import com.arronlong.httpclientutil.common.HttpConfig; import com.arronlong.httpclientutil.common.HttpHeader; import com.arronlong.httpclientutil.common.HttpResult; import com.arronlong.httpclientutil.exception.HttpProcessException; import org.apache.commons.lang3.StringUtils; import org.apache.http.Header; import org.apache.http.HttpRequest; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; public class HttpUtils { private static String getConnect(String url, String method, Map<String, Object> params, boolean cookie) throws HttpProcessException { Header[] headers = HttpHeader.custom() .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/69.0.3497.81 Safari/537.36 QQBrowser/4.5.122.400") .contentType("application/x-www-form-urlencoded") .cookie("bad_id1849dbe0-0728-11e8-80f8-29d49d388850=60bd7981-aac9-11ea-a5a3-0be1df714c8e; nice_id1849dbe0-0728-11e8-80f8-29d49d388850=60bd7982-aac9-11ea-a5a3-0be1df714c8e; JSESSIONID=1C55F1C609D5D23A246117B23547A7A7; !Proxy!route1plat=0b7d44f74a7c76aa5ed00f65af4cb367; !Proxy!JSESSIONID=4a3af8be-0df3-43a0-8709-2cfb889ae665; SERVERID=07ec2cdd4c9a0dfd9de364b51aa55d1c|1591776443|1591776277").build() ; HttpConfig config = HttpConfig.custom() .headers(headers) .url(url) .timeout(60000) .encoding("utf-8") .map(params); if (cookie) { HttpResult result = HttpClientUtil.sendAndGetResp(config); Header[] respHeaders = result.getRespHeaders(); } String html = ""; if (StringUtils.equals(method, "GET")) { html = HttpClientUtil.get(config); } else if (StringUtils.equals(method, "POST") && !cookie) { html = HttpClientUtil.post(config); } return html; } public static String get(String url) throws HttpProcessException { Map<String, Object> params = new HashMap<>(0); return getConnect(url, "GET", params, false); } public static String post(String url, Map<String, Object> params) throws HttpProcessException { return getConnect(url, "POST", params, false); } public static String postCookie(String url, Map<String, Object> params) throws HttpProcessException { return getConnect(url, "POST", params, true); } }
    Processed: 0.012, SQL: 9