乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      HttpClientUtils 工具類

       修行的嘟嘟 2023-09-25

      添加依賴

      <dependency>

            <groupId>org.apache.httpcomponents</groupId>

            <artifactId>httpclient</artifactId>

            <version>4.5.5</version>

          </dependency>

      代碼

      package com.utils;

      import org.apache.commons.logging.Log;

      import org.apache.commons.logging.LogFactory;

      import org.apache.http.HttpEntity;

      import org.apache.http.NameValuePair;

      import org.apache.http.ParseException;

      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.impl.client.CloseableHttpClient;

      import org.apache.http.impl.client.HttpClientBuilder;

      import org.apache.http.message.BasicNameValuePair;

      import org.apache.http.util.EntityUtils;

      import java.io.IOException;

      import java.util.ArrayList;

      import java.util.List;

      import java.util.Map;

      /**

       * @author 余勝軍

       * @ClassName HttpClients

       * @qq 644064779

       * @addres www.mayikt.com

       * 微信:yushengjun644

       */

      public class HttpClientUtils {

          private static final CloseableHttpClient httpClient;

          public static final String CHARSET = "UTF-8";

          private static final Log log = LogFactory.getLog(HttpClientUtils.class);

          // 采用靜態(tài)代碼塊,初始化超時時間配置,再根據(jù)配置生成默認httpClient對象

          static {

              RequestConfig config = RequestConfig.custom().setConnectTimeout(60000).setSocketTimeout(15000).build();

              httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();

          }

          public static String doGet(String url, Map<String, String> params) {

              return doGet(url, params, CHARSET);

          }

          public static String doPost(String url, Map<String, String> params) throws IOException {

              return doPost(url, params, CHARSET);

          }

          /**

           * HTTP Get 獲取內(nèi)容

           *

           * @param url     請求的url地址 ?之前的地址

           * @param params  請求的參數(shù)

           * @param charset 編碼格式

           * @return 頁面內(nèi)容

           */

          public static String doGet(String url, Map<String, String> params, String charset) {

              try {

                  if (params != null && !params.isEmpty()) {

                      List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());

                      for (Map.Entry<String, String> entry : params.entrySet()) {

                          String value = entry.getValue();

                          if (value != null) {

                              pairs.add(new BasicNameValuePair(entry.getKey(), value));

                          }

                      }

                      // 將請求參數(shù)和url進行拼接

                      url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));

                  }

                  HttpGet httpGet = new HttpGet(url);

                  CloseableHttpResponse response = httpClient.execute(httpGet);

                  int statusCode = response.getStatusLine().getStatusCode();

                  if (statusCode != 200) {

                      httpGet.abort();

                      throw new RuntimeException("HttpClient,error status code :" + statusCode);

                  }

                  HttpEntity entity = response.getEntity();

                  String result = null;

                  if (entity != null) {

                      result = EntityUtils.toString(entity, "utf-8");

                  }

                  EntityUtils.consume(entity);

                  response.close();

                  return result;

              } catch (Exception e) {

                  log.error("請求服務器端出錯:" + e);

                  return null;

              }

          }

          /**

           * HTTP Post 獲取內(nèi)容

           *

           * @param url     請求的url地址 ?之前的地址

           * @param params  請求的參數(shù)

           * @param charset 編碼格式

           * @return 頁面內(nèi)容

           * @throws IOException

           */

          public static String doPost(String url, Map<String, String> params, String charset)

                  throws IOException {

              List<NameValuePair> pairs = null;

              if (params != null && !params.isEmpty()) {

                  pairs = new ArrayList<NameValuePair>(params.size());

                  for (Map.Entry<String, String> entry : params.entrySet()) {

                      String value = entry.getValue();

                      if (value != null) {

                          pairs.add(new BasicNameValuePair(entry.getKey(), value));

                      }

                  }

              }

              HttpPost httpPost = new HttpPost(url);

              if (pairs != null && pairs.size() > 0) {

                  httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));

              }

              CloseableHttpResponse response = null;

              try {

                  response = httpClient.execute(httpPost);

                  int statusCode = response.getStatusLine().getStatusCode();

                  if (statusCode != 200) {

                      httpPost.abort();

                      throw new RuntimeException("HttpClient,error status code :" + statusCode);

                  }

                  HttpEntity entity = response.getEntity();

                  String result = null;

                  if (entity != null) {

                      result = EntityUtils.toString(entity, "utf-8");

                  }

                  EntityUtils.consume(entity);

                  return result;

              } catch (ParseException e) {

                  log.error("請求服務器端出錯:" + e);

                  return null;

              } finally {

                  if (response != null)

                      response.close();

              }

          }

      }

        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多