Class HttpUtils

java.lang.Object
com.luna.common.net.HttpUtils

public class HttpUtils extends Object
Author:
Luna
  • Field Details

    • CLIENT_CONTEXT

      public static final org.apache.hc.client5.http.protocol.HttpClientContext CLIENT_CONTEXT
    • MAX_REDIRECTS

      public static int MAX_REDIRECTS
    • MAX_CONN

      public static int MAX_CONN
      最大连接数
    • CONNECT_TIMEOUT

      public static int CONNECT_TIMEOUT
      设置连接建立的超时时间为10s
    • RESPONSE_TIMEOUT

      public static int RESPONSE_TIMEOUT
    • MAX_ROUTE

      public static int MAX_ROUTE
    • SOCKET_TIME_OUT

      public static int SOCKET_TIME_OUT
  • Constructor Details

    • HttpUtils

      public HttpUtils()
  • Method Details

    • init

      public static void init()
    • refresh

      public static void refresh()
    • basicAuth

      public static void basicAuth(String userName, String password, String host)
    • digestAuth

      public static void digestAuth(String userName, String password, String host)
    • authContext

      public static void authContext(String userName, String password, String host, String authType)
      需要用户名basic 验证
      Parameters:
      userName -
      password -
      host -
    • setProxy

      public static void setProxy(Integer port)
    • setProxy

      public static void setProxy(String host, Integer port)
      使用代理访问
      Parameters:
      host - 代理地址
      port - 代理端口
    • setProxy

      public static void setProxy(String host, Integer port, String username, String password)
    • builderHeader

      public static void builderHeader(Map<String,String> headers, org.apache.hc.core5.http.message.BasicClassicHttpRequest requestBase)
      请求头构建
      Parameters:
      headers -
      requestBase -
    • builderHeader

      public static void builderHeader(Map<String,String> headers, org.apache.hc.core5.http.nio.support.AsyncRequestBuilder requestBase)
    • getCookie

      public static List<org.apache.hc.client5.http.cookie.Cookie> getCookie()
    • addCookie

      public static void addCookie(org.apache.hc.client5.http.cookie.Cookie cookie)
    • addCookie

      public static void addCookie(List<org.apache.hc.client5.http.cookie.Cookie> cookies)
    • addCookie

      public static void addCookie(org.apache.hc.client5.http.cookie.Cookie... cookies)
    • doHead

      public static org.apache.hc.core5.http.HttpResponse doHead(String url)
    • doGet

      public static org.apache.hc.core5.http.HttpResponse doGet(String url, Map<String,String> headers)
    • doGet

      public static <T> T doGet(String host, String path, Map<String,String> headers, Map<String,String> queries, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler)
      发送 get 请求
      Parameters:
      host - 主机地址
      path - 路径
      headers - 请求头
      queries - 请求参数
      responseHandler - 响应处理器
      Returns:
      Throws:
      Exception
    • doGetHandler

      public static String doGetHandler(String host, String path, Map<String,String> headers, Map<String,String> queries)
      发送 get 请求
      Parameters:
      host - 主机地址
      path - 路径
      headers - 请求头
      queries - 请求参数
      Returns:
    • doGet

      public static org.apache.hc.core5.http.ClassicHttpResponse doGet(String host, String path, Map<String,String> headers, Map<String,String> queries)
    • doDelete

      public static <T> T doDelete(String host, String path, Map<String,String> headers, Map<String,String> queries, String body, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler)
      delete request
      Parameters:
      host - 主机地址
      path - 路径
      headers - 请求头
      queries - 请求参数
      responseHandler - 响应处理器
      body -
      Returns:
    • doDeleteHandler

      public static String doDeleteHandler(String host, String path, Map<String,String> headers, Map<String,String> queries, String body)
    • doDelete

      public static org.apache.hc.core5.http.ClassicHttpResponse doDelete(String host, String path, Map<String,String> headers, Map<String,String> queries, String body)
    • doPut

      public static <T> T doPut(String host, String path, Map<String,String> headers, Map<String,String> queries, String body, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler)
      PUT 方法请求
      Parameters:
      host - 主机地址
      path - 路径
      headers - 请求头
      queries - 请求参数
      body - 请求体
      Returns:
      ClassicHttpResponse
    • doPut

      public static org.apache.hc.core5.http.ClassicHttpResponse doPut(String host, String path, Map<String,String> headers, Map<String,String> queries, String body)
      PUT 方法请求
      Parameters:
      host - 主机地址
      path - 路径
      headers - 请求头
      queries - 请求参数
      body - 请求体
      Returns:
      ClassicHttpResponse
    • doPutHandler

      public static String doPutHandler(String host, String path, Map<String,String> headers, Map<String,String> queries, String body)
    • doPost

      public static <T> T doPost(String host, String path, Map<String,String> headers, Map<String,String> queries, Map<String,String> bodies, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler)
      POST 发送文件请求
      Parameters:
      host - 主机地址
      path - 路径
      headers - 请求头
      queries - 请求参数
      bodies - 文件列表
      Returns:
      ClassicHttpResponse
      Throws:
      Exception - 运行时异常
    • doPost

      public static <T> void doPost(String host, String path, Map<String,String> headers, Map<String,String> queries, Map<String,String> bodies, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler, org.apache.hc.core5.concurrent.FutureCallback<Object> futureCallback)
    • breakingPointDownload

      public static <T> void breakingPointDownload(String host, String path, Map<String,String> headers, Map<String,String> queries, org.apache.hc.core5.concurrent.FutureCallback<Object> futureCallback)
    • breakingPointUpload

      public static <T> void breakingPointUpload(String host, String path, Map<String,String> headers, Map<String,String> queries, String key, File file, org.apache.hc.core5.concurrent.FutureCallback<Object> futureCallback)
    • doPost

      public static org.apache.hc.core5.http.ClassicHttpResponse doPost(String host, String path, Map<String,String> headers, Map<String,String> queries, Map<String,String> bodies)
    • doPost

      public static <T> T doPost(String host, String path, Map<String,String> headers, Map<String,String> queries, String body, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler)
      POST请求体为字符串
      Parameters:
      host - 主机地址
      path - 路径
      headers - 请求头
      queries - 请求参数
      body - 请求体
      Returns:
      ClassicHttpResponse
    • doPost

      public static <T> T doPost(String host, String path, Map<String,String> headers, Map<String,String> queries, org.apache.hc.core5.http.HttpEntity httpEntity, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler)
    • doPost

      public static org.apache.hc.core5.http.ClassicHttpResponse doPost(String host, String path, Map<String,String> headers, Map<String,String> queries, String body)
    • doPostHander

      public static String doPostHander(String host, String path, Map<String,String> headers, Map<String,String> queries, String body)
    • doPost

      public static <T> T doPost(String host, String path, Map<String,String> headers, Map<String,String> queries, byte[] body, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler)
      Post stream
      Parameters:
      host - 主机地址
      path - 路径
      headers - 请求头
      queries - 请求参数
      body - 请求体
      Returns:
      HttpResponse
      Throws:
      RuntimeException
    • doPost

      public static org.apache.hc.core5.http.HttpResponse doPost(String host, String path, Map<String,String> headers, Map<String,String> queries, byte[] body)
    • isUrl

      public static boolean isUrl(String url)
    • buildUrlHead

      public static String buildUrlHead(String host, String path)
      构建请求路径
      Parameters:
      host - 主机地址
      path - 请求路径
      Returns:
      String
    • buildUrlObject

      public static String buildUrlObject(String host, String path, Map<String,Object> queries)
      构建请求路径
      Parameters:
      host - 主机地址
      path - 请求路径
      queries - 请求参数
      Returns:
      String
    • buildUrlString

      public static String buildUrlString(String host, String path, Map<String,String> queries)
      构建请求路径
      Parameters:
      host - 主机地址
      path - 请求路径
      queries - 请求参数
      Returns:
      String
    • buildUrl

      public static String buildUrl(String host, String path, Map<?,?> queries)
      构建url
      Parameters:
      host - 主机地址
      path - 路径
      queries - 请求参数
      Returns:
      String
    • getUserInfo

      public static String getUserInfo(String url, String username, String password)
      拼接URL用户名和密码
      Parameters:
      url -
      username - 用户名
      password -
      Returns:
    • checkResponseAndGetResultV2

      public static String checkResponseAndGetResultV2(org.apache.hc.core5.http.ClassicHttpResponse httpResponse, boolean isEnsure)
      检测响应体
      Parameters:
      httpResponse - 响应体
      Returns:
      String
    • checkResponseStreamAndGetResult

      public static byte[] checkResponseStreamAndGetResult(org.apache.hc.core5.http.ClassicHttpResponse httpResponse)
      检测响应体获取相应流
      Parameters:
      httpResponse - 响应体
      Returns:
    • urlEncode

      public static String urlEncode(Map<?,?> map)
      解析生成URL
      Parameters:
      map - 键值对
      Returns:
      生成的URL尾部
    • urlEncodeWithUtf8

      public static String urlEncodeWithUtf8(String s)
    • urlEncode

      public static String urlEncode(String s, String chareset)
    • checkResponseStreamAndGetResult

      public static byte[] checkResponseStreamAndGetResult(org.apache.hc.core5.http.ClassicHttpResponse httpResponse, List<Integer> statusList)
      检测响应体并解析
      Parameters:
      httpResponse - 响应体
      statusList - 状态码列表
      Returns:
      解析字节
    • checkResponseAndGetResult

      public static String checkResponseAndGetResult(org.apache.hc.core5.http.ClassicHttpResponse httpResponse, List<Integer> statusList)
      检测响应体并解析
      Parameters:
      httpResponse - 响应体
      statusList - 状态码列表
      Returns:
      解析字符串
    • checkResponseAndGetResult

      public static String checkResponseAndGetResult(org.apache.hc.core5.http.ClassicHttpResponse httpResponse)
      解析响应体
      Parameters:
      httpResponse - 响应体
      Returns:
      String
    • checkResponseAndGetResult

      public static String checkResponseAndGetResult(org.apache.hc.core5.http.HttpResponse httpResponse)
    • checkResponseAndGetResult

      public static String checkResponseAndGetResult(org.apache.hc.core5.http.HttpResponse httpResponse, Boolean isEnsure)
    • addRequestInterceptorFirst

      public void addRequestInterceptorFirst(org.apache.hc.core5.http.HttpRequestInterceptor httpRequestInterceptor)
    • addExecInterceptorAfter

      public void addExecInterceptorAfter(String existing, String name, org.apache.hc.client5.http.classic.ExecChainHandler interceptor)