zoukankan      html  css  js  c++  java
  • HttpClient基本功能的使用 Get方式

    一、GET 方法
        使用 HttpClient 需要以下 6 个步骤:
        1. 创建 HttpClient 的实例
        2. 创建某种连接方法的实例,在这里是 GetMethod。在 GetMethod 的构造函数中传入待连接的地址
        3. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
        4. 读 response
        5. 释放连接。无论执行方法是否成功,都必须释放连接
        6. 对得到后的内容进行处理
    根据以上步骤,我们来编写用GET方法取得某网页内容的代码。

    1、大部分情况下 HttpClient 默认的构造函数已经足够使用。

    HttpClient httpClient  =   new  HttpClient();

    2、创建GET方法的实例。
          在GET方法的构造函数中传入待连接的地址即可。用GetMethod将会自动处理转发过程,如果想要把自动处理转发过程去掉的话,可以调用方法setFollowRedirects(false)。

    GetMethod getMethod  =   new  GetMethod( " http://www.ibm.com/ " );

    3、调用 httpClient 的 executeMethod 方法来执行 getMethod。

       由于是执行在网络上的程序,在运行executeMethod方法的时候,需要处理两个异常,分别是HttpException和 IOException

          HttpException:引起第一种异常的原因主要可能是在构造getMethod的时候传入的协议不对,比如将"http"写成了"htp",或者服务 器端返回的内容不正常等,并且该异常发生是不可恢复的;

         IOException:一般是由于网络原因引起的异常,对于这种异常 (IOException),HttpClient会根据你指定的恢复策略自动试着重新执行executeMethod方法。HttpClient的恢复 策略可以自定义(通过实现接口HttpMethodRetryHandler来实现)。通过httpClient的方法setParameter设置你实 现的恢复策略。

      本例中使用的是系统提供的默认恢复策略,该策略在碰到第二类异常的时候将自动重试3次。executeMethod返回值是一个整数,表示 了执行该方法后服务器返回的状态码,该状态码能表示出该方法执行是否成功、需要认证或 者页面发生了跳转(默认状态下GetMethod的实例是自动处理跳 转的)等。

    getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new  DefaultHttpMethodRetryHandler()); // 设置成了默认的恢复策略,在发生异常时候将自动重试3次,在这里你也可以设置成自定义的恢复策略 
    int statusCode = client.executeMethod(getMethod); // 执行getMethod if (statusCode != HttpStatus.SC_OK) { System.err.println( " Method failed: " + getMethod.getStatusLine()); }

    4、在返回的状态码正确后,即可取得内容。

    取得目标地址的内容有三种方法:
          Ⅰ、getResponseBody,该方法返回的是目标的二进制的byte流;
          Ⅱ、getResponseBodyAsString,这个方法返回的是String类型,值得注意的是该方法返回的String的编码是根据系统默认的编码方式,所以返回的String值可能编码类型有误;
          Ⅲ、getResponseBodyAsStream,这个方法对于目标地址中有大量数据需要传输是最佳的。
          在这里我们使用了最简单的 getResponseBody方法。

    byte [] responseBody  =  method.getResponseBody();

    5、释放连接。
           无论执行方法是否成功,都必须释放连接。

    method.releaseConnection();

    6、处理内容。
          在这一步中根据你的需要处理内容,本例中只是简单的将内容打印到控制台。System.out.println( new  String(responseBody));

     

    package com.asiainfo.hsop.common.utils;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import java.nio.charset.Charset;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import com.alibaba.fastjson.JSONObject;
    import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpException;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
    import org.apache.commons.httpclient.NameValuePair;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.methods.PostMethod;
    import org.apache.commons.httpclient.params.HttpClientParams;
    import org.apache.commons.httpclient.params.HttpMethodParams;
    import org.apache.commons.httpclient.protocol.Protocol;
    import org.apache.commons.lang3.StringUtils;
    import org.apache.http.HttpEntity;
    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.HttpPost;
    import org.apache.http.entity.ContentType;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.entity.mime.HttpMultipartMode;
    import org.apache.http.entity.mime.MultipartEntityBuilder;
    import org.apache.http.entity.mime.content.FileBody;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    
    /**
     * 处理http和https协议请求,根据请求url判断协议类型
     *
     * @author yangxiaobing
     * @date 2017/9/6
     */
    public class HttpUtil {
    
    
        private static Logger logger = LogManager.getLogger(HttpUtil.class);
        private static final int HTTP_DEFAULT_TIMEOUT = 15000;               //超时时间默认为15秒
        private static final int HTTP_SOCKET_TIMEOUT = 30000;               //连接状态下没有收到数据的话强制断时间为30秒
        private static final int MAX_TOTAL_CONNECTIONS = 500;                 //最大连接数
        private static final int CONN_MANAGER_TIMEOUT = 500;                  //该值就是连接不够用的时候等待超时时间,一定要设置,而且不能太大
    
        private static MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
    
        //接口调用频繁,结果出现了很多ConnectTimeoutException 配置优化,共用HttpClient,减少开销
        static {
            //每主机最大连接数和总共最大连接数,通过hosfConfiguration设置host来区分每个主机
            //client.getHttpConnectionManager().getParams().setDefaultMaxConnectionsPerHost(8);
            httpConnectionManager.getParams().setMaxTotalConnections(MAX_TOTAL_CONNECTIONS);
            httpConnectionManager.getParams().setConnectionTimeout(HTTP_DEFAULT_TIMEOUT);//连接超时时间
            httpConnectionManager.getParams().setSoTimeout(HTTP_SOCKET_TIMEOUT);         //连接状态下没有收到数据的话强制断时间
            httpConnectionManager.getParams().setLongParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, CONN_MANAGER_TIMEOUT);
            //是否计算节省带宽
            httpConnectionManager.getParams().setTcpNoDelay(true);
            //延迟关闭时间
            httpConnectionManager.getParams().setLinger(0);
            //失败的情况下会默认进行3次尝试,成功之后不会再尝试    ------关闭
            httpConnectionManager.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
        }
    
    
        /**
         * 构建 httpsclient 请求
         *
         * @return
         */
        private static HttpClient getHttpClient() {
            HttpClient httpClient = new HttpClient(httpConnectionManager);
            httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
            return httpClient;
        }
    
    
        /**
         * POST请求统一入口
         */
        public static String post(String url, Map<String, Object> paramMap) {
            logger.info("===>>>调用[post]接口开始...===>>> URL:" + url);
            Long beginTime = System.currentTimeMillis();
            if (StringUtils.isEmpty(url)) {
                return null;
            }
            String result = null;
            if (startsWithIgnoreCase(url, "https")) {
                result = httpsPost(url, paramMap, "UTF-8");
            } else if (startsWithIgnoreCase(url, "http")) {
                result = httpPost(url, paramMap, "UTF-8");
            } else {
                logger.warn("http url format error!");
            }
            logger.info("===>>>调用[post]接口结束   ===>>>URL:" + url + ",耗时:" + (System.currentTimeMillis() - beginTime) + "毫秒");
            return result;
        }
    
    
        /**
         * GET请求统一入口
         */
        public static String get(String url) {
            logger.info("===>>>调用[get]接口开始...===>>> URL:" + url);
            Long beginTime = System.currentTimeMillis();
            if (StringUtils.isEmpty(url)) {
                return null;
            }
            String result = null;
            if (startsWithIgnoreCase(url, "https")) {
                result = httpsGet(url, "UTF-8");
            } else if (startsWithIgnoreCase(url, "http")) {
                result = httpGet(url, "UTF-8");
            } else {
                logger.warn("http url format error!");
            }
            logger.info("===>>>调用[get]接口结束   ===>>>URL:" + url + ",耗时:" + (System.currentTimeMillis() - beginTime) + "毫秒");
            return result;
        }
    
        public static String httpPost(String url, Map<String, Object> paramMap, String encoding) {
            String content = null;
            if (paramMap == null) {
                paramMap = new HashMap<String, Object>();
            }
            logger.info("http param:" + paramMap.toString());
            HttpClient httpClient = getHttpClient();
            PostMethod method = new PostMethod(url);
    
            if (!paramMap.isEmpty()) {
                for (Map.Entry<String, ?> entry : paramMap.entrySet()) {
                    method.addParameter(new NameValuePair(entry.getKey(), entry.getValue().toString()));
                }
            }
            try {
                httpClient.executeMethod(method);
                logger.info("http status : " + method.getStatusLine().getStatusCode());
                content = new String(method.getResponseBody(), encoding);
                logger.info("http response : [" + content + "]");
            } catch (Exception e) {
                logger.error("发起http请求失败[" + url + "]" + ",param" + paramMap.toString(), e);
            } finally {
                method.releaseConnection();
                httpClient.getHttpConnectionManager().closeIdleConnections(0);
            }
            return content;
        }
    
        public static String httpPost(String url, JSONObject param) {
            String content = null;
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
    
            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
            StringEntity se = null;
            try {
                se = new StringEntity(JSONObject.toJSONString(param));
                se.setContentType("text/json");
                httpPost.setEntity(se);
    
                CloseableHttpResponse response = null;
                response = httpClient.execute(httpPost);
                HttpEntity httpEntity = response.getEntity();
                content = EntityUtils.toString(httpEntity, "UTF-8");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (httpClient != null) {
                    try {
                        httpClient.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return content;
        }
    
    
        private static String httpsPost(String url, Map<String, Object> paramMap, String encoding) {
            String content = null;
            HttpClient httpsClient = getHttpClient();
            Protocol myhttps = new Protocol("https", new MySSLProtocolSocketFactory(), 443);
            Protocol.registerProtocol("https", myhttps);
            PostMethod method = new PostMethod(url);
            if (paramMap != null && !paramMap.isEmpty()) {
                for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
                    if (null != entry.getValue()) {
                        method.addParameter(new NameValuePair(entry.getKey(), entry.getValue().toString()));
                    }
                }
                logger.info("https param : " + paramMap.toString());
            }
            try {
                httpsClient.executeMethod(method);
                logger.info("https status :" + method.getStatusLine().getStatusCode());
                if (method.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    content = new String(method.getResponseBody(), encoding);
                    logger.info("https response : [" + content + "]");
                }
            } catch (Exception e) {
                logger.error("https request failed. url : [" + url + "]" + ", param : [" + paramMap + "]", e);
            } finally {
                method.releaseConnection();
                httpsClient.getHttpConnectionManager().closeIdleConnections(0);
            }
            return content;
        }
    
    
        private static String httpGet(String url, String encoding) {
            HttpClient httpClient = getHttpClient();
            GetMethod method = new GetMethod(url);
            String result = null;
            try {
                httpClient.executeMethod(method);
                int status = method.getStatusCode();
                if (status == HttpStatus.SC_OK) {
                    result = method.getResponseBodyAsString();
                } else {
                    logger.error("Method failed: " + method.getStatusLine());
                }
            } catch (HttpException e) {
                // 发生致命的异常,可能是协议不对或者返回的内容有问题
                logger.error("Please check your provided http address!");
                logger.error(e, e);
            } catch (IOException e) {
                // 发生网络异常
                logger.error("发生网络异常!");
                logger.error(e, e);
            } finally {
                // 释放连接
                method.releaseConnection();
                httpClient.getHttpConnectionManager().closeIdleConnections(0);
            }
            return result;
        }
    
        private static String httpsGet(String url, String encoding) {
            HttpClient httpsClient = getHttpClient();//HttpConnectionManager.alwaysClose=true
            Protocol myhttps = new Protocol("https", new MySSLProtocolSocketFactory(), 443);
            Protocol.registerProtocol("https", myhttps);
            GetMethod method = new GetMethod(url);
            String content = null;
            try {
                httpsClient.executeMethod(method);
                logger.info("https status : " + method.getStatusLine().getStatusCode());
                if (method.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    content = new String(method.getResponseBody(), encoding);
                    logger.info("https response : [" + content + "]");
                }
            } catch (Exception e) {
                logger.error("https request failed. url : [" + url + "]", e.getCause());
            } finally {
                method.releaseConnection();
                httpsClient.getHttpConnectionManager().closeIdleConnections(0);
            }
            return content;
        }
    
    
        private static boolean startsWithIgnoreCase(String origin, String prefix) {
            int len = prefix.length();
            if (len == 0 || origin.length() < len) {
                return false;
            }
            while (len-- > 0) {
                char a = origin.charAt(len);
                char b = prefix.charAt(len);
                if (a == b || Character.toUpperCase(a) == Character.toUpperCase(b)) {
                    continue;
                }
                return false;
            }
            return true;
        }
    
        public static void main(String args[]) {
    
    
          //  String url = "http://10.253.6.202:30001/dipic/api/auditControl/queryTraffic";
            String url = "http://localhost:8180/comm/api/upload.do";
            JSONObject Json = new JSONObject();
            File file = new File("E:\initParam.properties");
            Map<String,String> map = new HashMap<>();
            map.put("fileName","redis-64bit.rar");
            System.out.println(HttpUtil.postFile(url, file));
           }
    
            //文件上传,调用fastDFS方法封装层
            public static String postFile(String url, File file){
                CloseableHttpClient httpclient = HttpClients.createDefault();
                CloseableHttpResponse response = null;
                String result = null;
                try {
                    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000)
                            .setConnectTimeout(3000).setConnectionRequestTimeout(3000).build();
    //                if (params != null && !params.isEmpty()) {
    //                    List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>(params.size());
    //                    for (Map.Entry<String, String> entry : params.entrySet()) {
    //                        String value = entry.getValue();
    //                        if (value != null) {
    //                            pairs.add(new BasicNameValuePair(entry.getKey(), value));
    //                        }
    //                    }
    //                    url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
    //                }
                    HttpPost httpPost = new HttpPost(url);
                    httpPost.setConfig(requestConfig);
                    // 将java.io.File对象添加到HttpEntity(org.apache.http.HttpEntity)对象中
                    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                    // 解决上传文件,文件名中文乱码问题
                    builder.setCharset(Charset.forName("utf-8"));
                    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);// 设置浏览器兼容模式
                    builder.addPart("file", new FileBody(file));
    
                    httpPost.setEntity(builder.build());
                    response = httpclient.execute(httpPost);
    
                    int statusCode = response.getStatusLine().getStatusCode();
                    if (statusCode == HttpStatus.SC_OK) {
                        HttpEntity resEntity = response.getEntity();
                        result = EntityUtils.toString(resEntity);
    
                        // 消耗掉response
                        EntityUtils.consume(resEntity);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    if(response!=null){
                        try {
                            response.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if(httpclient!=null){
                        try {
                            httpclient.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
    
                }
                return result;
            }
    
        }
    HttpUtil工具类

     

    package  test;
      import  java.io.IOException;
       import  org.apache.commons.httpclient. * ;
      import  org.apache.commons.httpclient.methods.GetMethod;
       import  org.apache.commons.httpclient.params.HttpMethodParams;
      public   class  GetSample{
         public   static   void  main(String[] args) {
             // 构造HttpClient的实例
             HttpClient httpClient  =   new  HttpClient();
             // 创建GET方法的实例
              GetMethod getMethod  =   new  GetMethod( " http://www.ibm.com " );
             // 使用系统提供的默认的恢复策略
              getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                                                     new  DefaultHttpMethodRetryHandler());
              try  {
                // 执行getMethod
                int  statusCode  =  httpClient.executeMethod(getMethod);
                if  (statusCode  !=  HttpStatus.SC_OK) {
                   System.err.println( " Method failed:  "
                                            +  getMethod.getStatusLine());
                }
                // 读取内容
                 byte [] responseBody  =  getMethod.getResponseBody();
                // 处理内容
                 System.out.println( new  String(responseBody));
             }  catch  (HttpException e) {
                // 发生致命的异常,可能是协议不对或者返回的内容有问题
                System.out.println( " Please check your provided http address! " );
                e.printStackTrace();
             }  catch  (IOException e) {
                 // 发生网络异常
                 e.printStackTrace();
             }  finally  {
                // 释放连接
                getMethod.releaseConnection();
            }
           }
        }
    View Code
  • 相关阅读:
    NOI 模拟赛
    bzoj 4998 星球联盟
    bzoj 4545 DQS 的 Trie
    loj #161 子集卷积
    bzoj 5093 图的价值
    bzoj 4299 Codechef FRBSUM
    NOI 模拟赛
    WC2018 州区划分
    CSP 2020 T2 动物园
    CSP 2020 T1 儒略日
  • 原文地址:https://www.cnblogs.com/zouhong/p/12061191.html
Copyright © 2011-2022 走看看