使用CloseableHttpClient调用服务接口时参数如何传递(CloseableHttpClient参数传递方式)

给一个老项目新增功能接口时,发现前人们在项目中调用外部接口,每个地方都写了一遍CloseableHttpClient,如下代码片段重复了 二十几次。

        String result = null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            HttpPost httpPost = new HttpPost(url);
            UrlEncodedFormEntity formEntity = null;

            httpPost.setEntity(formEntity);
            CloseableHttpResponse response = httpClient.execute(httpPost);
            try {
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity);
                //System.out.println(result);
            } finally {
                response.close();
            }
        } finally{
            httpClient.close();
        }
        return result;

没办法,只有提取出来作为公共方法,删除二十几个地方重复代码,都调用一下这个接口。

但是,重点来了:发现上面代码问题了没有,每个地方调用接口都没有传递参数(参数是null):

            UrlEncodedFormEntity formEntity = null;

            httpPost.setEntity(formEntity);

再来看看原来的代码:

String url = PORT_DATA + GRAPH_URI + "/Protobuf/getDataByTableGet" +
				"?tableID=94&&appID=1600000&&contextID=1&&columnName=graph_name,graph_version";

原来是把参数都拼接到url里面去了,然后httpPost.setEntity(null);

看到这,真不知道说什么好;本想就接着他们用吧,一看我的需求,参数长度未知,随便打印一个:

115404740988241081,115404740988241080,115404742263309056,115404742263309052,115404742263309050,115404742263309051,115404740988241079,115404742263309054,115404742263309053,115404742263309049,116812244720814078,116812244720814376,116812245995881092,116812245995881142,116812245995880951,116812245995880981,116812244720818598,116812245995881099,116812245995880975,116812245995881146,116812245995881021,116812245995881094,116812245995881013,122723134607460287,122723134607460290,117375409422599027,117375409422599030,117375409422600352,122723134607460292,116812245995881550,122723134607460293,116812244720813016,116812244720816212,115967864015425564,114560489131872044,114841921158913131,114841921158913132,114841921158913133,114841921158913134,114560489131872040,114841921158913117,114841921158913118,114841921158913119,114841921158913138,114841921158913137,114841921158913136,114841921158913135,114560489131872045,114560489131872046,114841921158913139,114841921158913140,114841921158913141,114841921158913142,114841921158913146,114841921158913145,114841921158913144,114841921158913143,114560489131872047,114560489131872043,114841921158913129,114841921158913130,114841921158913114,114841921158913113,114560489131872041,114841921158913121,114841921158913122,114841921158913123,114841921158913124,114841921158913165,114841921158913164,114841921158913163,114560489131872052,114560489131872048,114841921158913147,114841921158913148,114841921158913149,114841921158913150,114841921158913154,114841921158913153,114841921158913152,114841921158913151,114560489131872049,114841921158913158,114841921158913157,114841921158913156,114841921158913155,114560489131872050,114560489131872042,114841921158913125,114841921158913126,114841921158913127,114841921158913128,114841921158913162,114841921158913161,114841921158913160,114841921158913159,114560489131872051,114560489131872058,114841921158913181,114841921158913182,114841921158913183,114841921158913184,114841921158913188,114841921158913187,114841921158913186,114841921158913185,114560489131872059,114560489131872053,114841921158913166,114841921158913167,114841921158913168,114841921158913115,114841921158913169,114841921158913170,114841921158913174,114841921158913173,114841921158913172,114560489131872055,114560489131872057,114841921158913179,114841921158913180,114841921158913178,114841921158913177,114841921158913176,114560489131872056,114841919883853257,114841919883853256,114560487856805475,114841919883853255,114841921158913116,114841919883853254,114841919883853169,114841919883853168,114560487856805443,114841919883853167,114841919883853166,114841919883848722,114841919883848723,114560487856803885,114841919883848724,114841919883848725,114841919883848721,114841919883848720,114560487856803889,114841919883848719,114841919883848718,114841919883848726,114841919883848727,114560487856803886,114841919883848728,114841919883848729,114841919883848717,114841919883848716,114560487856803890,114841919883848715,114841919883848714,114841919883848733,114841919883848732,114560487856803887,114841919883848731,114841919883848730,114841919883848734,114841919883848735,114560487856804974,114841919883848736,114841919883848713,118500967007135938,118500967007135937,114841921158930451,118500967007135936,118500967007135939,118500967007135940,117938017053705131,118500967007135935,114560489131885889,114841921158930466,114841921158930467,117938017053705132,114560489131885890,114841921158930469,114841921158930452,118500967007134606,114841921158913111,118500967007128925,118500967007134605,114841921158913112,118500967007128923,118500967007133081,118500967007133084,118500967007133083,118500967007133082,118500967007128946,118500967007131418,118500967007128948,118500967007128947,118500967007128924,118500967007128939,118500967007129278,118500967007131468,118500967007128941,118500967007128936,118500967007131428,118500967007129292,118500967007128937,118500967007131429,118500967007131141,118500967007131419,118500967007131389,118500967007134611,118500967007134610,118500967007128943,118500967007128942,118500967007128934,118500967007128935,118500967007129277,118500967007128922,118500967007134609,118500967007128921,118500967007134608,118500967007128920,118500967007134607,118500967007129280,115123394860548128,118500967007128915,114560489131885887,114841921158930463,118500967007128917,115123394860548129,118500967007129283,115123396135617015,118500967007135931,118500967007129281,118500967007129279,118500967007129284,118500967007135932,118500967007129282,118500967007129286,118500967007129285,114841921158930468,114841921158930457,114841921158930444,114841921158930448,113997666752397730,114560489131872054,114841921158913171,114841921158913120,114841921158913175,115404869837259961,115404869837259959,114560616705823854,114560573756150894,114560530806477934,117375366472927392,117375280573581472,117375323523254432,116812330620160296,116812287670487336,117375366472926070,117375323523253110,117375280573580150,116812330620159998,116812287670487038,117375366472926067,117375323523253107,117375280573580147,116812330620158936,116812287670485976,116812330620162132,116812287670489172,117375280573580149,117375323523253109,117375366472926069,117375280573581473,117375323523254433,117375366472927393,117375280573580152,117375323523253112,117375366472926072,115404871112327933,117938145902724011,117938188852396971,117938145902724012,117938188852396972,115404871112327930,117375280573580148,117375323523253108,117375366472926068,117375280573580151,117375323523253111,117375366472926071,117375323523254434,117375366472927394,117375280573581474,116812288945554510,116812331895227470,114560617980890937,116812288945553973,116812331895226933,116812288945553981,116812331895226941,116812288945554106,116812331895227066,116812288945554059,116812331895227019,116812288945553941,116812331895226901,116812288945554102,116812331895227062,115967906965098524,115967949914771484,116812288945553911,116812331895226871,114560532081545012,114560575031217972,114560617980890932,114560617980890923,116812288945553935,116812331895226895,116812288945554054,116812331895227014,116812288945554052,116812331895227012,115404871112327934,115404871112327936,115404871112327931,114841921158930455,115967864015426306,115967906965099266,115967949914772226,115967864015426309,115967906965099269,115967949914772229,115967864015426311,115967906965099271,115967949914772231,115967864015426307,115967906965099267,115967949914772227,115967864015426302,115967906965099262,115967949914772222,117938188852396968,117938145902724008,117938017053705127,117938145902724007,117938188852396967,114841921158930450,115967864015426304,115967906965099264,115967949914772224,118500967007135941,117938188852396970,117938145902724010,117938188852396969,117938145902724009,115967864015426308,115967906965099268,115967949914772228,115967864015426310,115967906965099270,115967949914772230,115967864015426305,115967906965099265,115967949914772225,115967864015426303,115967906965099263,115967949914772223,118500967007136061,118500967007136062,118500967007136063,118500967007136064,118500967007136065,118500967007136067,118500967007136068,118500967007136069,118500967007136066,118500967007136070,118500967007136071,118500967007136075,118500967007136072,118500967007135944,118500967007136073,118500967007136074,118500967007136076,115404871112327929,114560617980904763,114560617980904762,114560575031231802,114560532081558842,115405213434643639,115405234909480119,122160225791907410,116812245995881100,116812288945554060,116812331895227020,116812287670491558,116812330620164518,116812244720818793,116812287670491753,116812330620164713,117938188852397200,117938145902724240,117938145902724239,117938188852397199,114560617980904764,11512339613561

算了,动手改吧,于是就把接口改了,公共方法增加参数作为CloseableHttpClient调用接口的参数。

    public static String doPost(String url, Map<String, String> paramsMap) throws Exception
    {
        String result = null;
        //设置请求的状态参数
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectionRequestTimeout(6000).setConnectTimeout(6000)
                .setSocketTimeout(6000).build();

        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(requestConfig);
            // 参数不为空
            if(null != paramsMap){
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                for(Map.Entry<String, String> entry : paramsMap.entrySet()){
                    params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
                HttpEntity httpEntity = new UrlEncodedFormEntity(params, "UTF-8");
                httpPost.setEntity(httpEntity);
            }else {
                UrlEncodedFormEntity formEntity = null;
                httpPost.setEntity(formEntity);
            }

            CloseableHttpResponse response = httpClient.execute(httpPost);
            try {
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity);
            } finally {
                response.close();
            }
        } finally{
            httpClient.close();
        }
        return result;
    }

写程序一定要用心,要标准化,可复用,可扩展。

比如此例:要是前人们当时开发时就写好方法,那我可以将我需要调用的外部接口url和参数map集合封装好直接调用doPost方法,不用花精力修改他们代码,扩展接口了。

发布了49 篇原创文章 · 获赞 19 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/shenju2011/article/details/102494714