HttpComponents??????????
???????????? ???????[ 2012/11/26 10:37:29 ] ????????
????}
????/**
????* Determine whether the given response indicates a GZIP response.
????*
????* The default implementation checks whether the HTTP "Content-Encoding"
????* header contains "gzip" (in any casing).
????*
????* @param httpResponse the resulting HttpResponse to check
????* @return whether the given response indicates a GZIP response
????*/
????protected boolean isGzipResponse(HttpResponse httpResponse) {
????Header encodingHeader = httpResponse.getFirstHeader(HTTP_HEADER_CONTENT_ENCODING);
????return (encodingHeader != null && encodingHeader.getValue() != null && encodingHeader
????.getValue().toLowerCase().contains(ENCODING_GZIP));
????}
????/**
????* Shutdown hook that closes the underlying
????*{@link }org.apache.http.conn.ClientConnectionManager
????* ClientConnectionManager}'s connection pool?? if any.
????*/
????public void destroy() {
????getHttpClient().getConnectionManager().shutdown();
????}
????enum HttpMethod {
????GET??
????POST??
????HEAD??
????OPTIONS??
????PUT??
????DELETE??
????TRACE
????}
????}
??????????t??????httpClient 3????η????????:
????[java] view plaincopyprint?
????/**
????* @author von gosling 2011-12-12
????*/
????public class HttpClientUtils {
????private static final Logger log = LoggerFactory
????.getLogger(HttpClientUtils.class);
????private static int timeOut = 100;
????private static int retryCount = 1;
????private static int connectionTimeout = 100;
????private static int maxHostConnections = 32; //????apache work MPM??????
????private static int maxTotalConnections = 512; //???
????private static String charsetName = "UTF-8";
????public static JSONObject executeMethod(HttpClient httpClient?? HttpMethod method) {
????JSONObject result = new JSONObject();
????StopWatch watch = new StopWatch();
????int status = -1;
????try {
????log.info("Execute method({}) begin..."?? method.getURI());
????watch.start();
????status = httpClient.executeMethod(method);
????watch.stop();
????if (status == HttpStatus.SC_OK) {
????InputStream inputStream = method.getResponseBodyAsStream();
????ByteArrayOutputStream baos = new ByteArrayOutputStream();
????IOUtils.copy(inputStream?? baos);
????String response = new String(baos.toByteArray()?? charsetName);
????log.info("Response is:{}"?? response);
????result = JSONObject.parseObject(response);
????} else {
????log.error("Http request failure! status is {}"?? status);
????}
????} catch (SocketTimeoutException e) {
????log.error("Request time out!");//??????????????????????????????????????
????} catch (Exception e) {
????log.error("Error occur!"?? e);
????} finally {
????method.releaseConnection();
????log.info("Method {}??statusCode {}??consuming {} ms"?? new Object[] { method.getName()??
????status?? watch.getTime() });
????}
????return result;
????}
????/**
????* @param uri
????* @param nameValuePairs
????* @return
????*/
????public static PostMethod createPostMethod(String uri?? NameValuePair[] nameValuePairs) {
????PostMethod method = new PostMethod(uri);
????method.addParameters(nameValuePairs);
????method.getParams().setContentCharset(charsetName);
????return method;
????}
????/**
????* @param uri
????* @param nameValuePairs
????* @return
????*/
????public static GetMethod createGetMethod(String uri?? NameValuePair[] nameValuePairs) {
????GetMethod method = new GetMethod(uri);
????List list = Lists.newArrayList();
????if (nameValuePairs != null) {
????Collections.addAll(list?? nameValuePairs);
????method.setQueryString(list.toArray(new NameValuePair[nameValuePairs.length]));
????}
????method.getParams().setContentCharset(charsetName);
????return method;
????}
????public static HttpClient createHttpClient() {
????//1.
????HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
????//2.
????HttpConnectionManagerParams httpConnectionManagerParams = httpClient
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11