x-www-form-urlencoded请求封装

<dependency>
   <groupId>commons-httpclient</groupId>
   <artifactId>commons-httpclient</artifactId>
   <version>3.1</version>
</dependency>

 

    public static String postJsonMethodParamsEncode(String postURL,Map<String,String> destMap){
        log.info("postJsonMethodParamsEncode request postURL:{}",postURL);
        try {
            PostMethod postMethod = null;
            postMethod = new PostMethod(postURL) ;
            postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;
            NameValuePair[] data = new NameValuePair[destMap.size()];
            int i = 0;
            for (String key : destMap.keySet()){
                data[i] = new NameValuePair(key,destMap.get(key));
                i++;
            }

            postMethod.setRequestBody(data);
            HttpClient httpClient = new HttpClient();
            int response = httpClient.executeMethod(postMethod);
            return postMethod.getResponseBodyAsString() ;
        } catch (Exception e) {
            log.error("请求异常:",e);
            throw new RuntimeException(e.getMessage());
        }
    }

 

posted @ 2020-08-20 14:04  天天大傻猫  Views(4568)  Comments(0Edit  收藏  举报