httpclient请求方法

    /**
     * httpclient请求方法
     * @param url 请求地址
     * @param paramMap 请求参数
     * @param ent 编码格式 gbk、utf-8
     * @return String 返回字符串
     */
    public static String httpClientRQ(String url,Map<String,String> paramMap,String ent){
        String rs = "";
        HttpClient httpClient = new HttpClient();
        PostMethod post = new PostMethod(url);
        try{
            int paramLength = paramMap.size();
            NameValuePair[] nps = new NameValuePair[paramLength];
            int index = 0;
            for(Map.Entry<String, String> map:paramMap.entrySet()){
                String key = map.getKey();
                String value = map.getValue();
//                Object objvalue = map.getValue();
//                String value = "";
//                if(objvalue!=null){
//                    value = objvalue.toString();
//                }
                nps[index] = new NameValuePair();
                nps[index].setName(key);
                nps[index].setValue(value);
                index++;
            }

            post.setRequestBody(nps);
            post.getParams().setContentCharset(ent);
            httpClient.getParams().setConnectionManagerTimeout(10000);
            httpClient.getParams().setSoTimeout(10000);
            httpClient.executeMethod(post);

            rs = post.getResponseBodyAsString();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            post.releaseConnection();

        }

        return rs;
    }
上一篇:python 字体颜色的设置


下一篇:无限互联IOS电影项目视频笔记