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());
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM