httpclient4.3发送post和get实例代码

来源:互联网 发布:软件前景 编辑:程序博客网 时间:2024/06/05 16:30

httpclient4.3发送post和get请求的工具代码。

/**     * @throws IOException      *      * @Title: httpPost      * @Description: httpPost请求工具类     * @param url     * @param params     * @param charset     * @param socketTimeOut     * @param connectTimeOut     * @param connReqTimeOut     * @return    设定文件     * @return String    返回类型     * @date 2015年7月10日 上午11:55:39      * @throws     */    public static String httpPost(String url, Map<String, Object> params, String charset, int socketTimeOut, int connectTimeOut, int connReqTimeOut ) throws IOException{        if (Tools.checkNull(charset)) {            charset = UTF8;        }        HttpPost httppost = new HttpPost(url);        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeOut).setConnectTimeout(connectTimeOut).setConnectionRequestTimeout(connReqTimeOut)                .build();//设置请求和传输超时时间        httppost.setConfig(requestConfig);        //定义传递参数        List<NameValuePair> formparams = new ArrayList<NameValuePair>();        if (params != null) {            for (Entry<String,Object> entry : params.entrySet()) {                formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()==null?"":entry.getValue().toString()));            }        }        UrlEncodedFormEntity uefEntity;        String result = null;        CloseableHttpClient httpclient = HttpClients.custom().build();        CloseableHttpResponse response = null;        try{            uefEntity = new UrlEncodedFormEntity(formparams, charset);//对参数进行编码            httppost.setEntity(uefEntity);            response = httpclient.execute(httppost);            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {                HttpEntity entity = response.getEntity();                if (entity != null) {                    return EntityUtils.toString(entity, charset);                }            } else {                return result;            }        } finally{            if (response != null) {                try {                    response.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            try {                httpclient.close();            } catch (IOException e) {                e.printStackTrace();            }        }        return result;    }    /**     * @throws IOException      *      * @Title: httpPost      * @Description: httpPost请求工具类     * @param url     * @param params     * @param timeOut     * @param charset     * @return    设定文件     * @return String    返回类型     * @date 2015年7月2日 下午4:51:55      * @throws     */    public static String httpPost(String url, Map<String, Object> params, String charset, int timeOut) throws IOException{        return httpPost(url, params, charset, timeOut, timeOut, timeOut);    }    /**     * @throws IOException      *      * @Title: httpGet      * @Description: httpPost请求工具类     * @param url     * @param params     * @param charset     * @param socketTimeOut     * @param connectTimeOut     * @param connReqTimeOut     * @return    设定文件     * @return String    返回类型     * @date 2015年7月10日 上午11:55:39      * @throws     */    public static String httpGet(String url, Map<String, Object> params, String charset, int socketTimeOut, int connectTimeOut, int connReqTimeOut ) throws IOException{        if (Tools.checkNull(charset)) {            charset = UTF8;        }        log.info("url : [{}]", url);        String result = null;        CloseableHttpClient httpclient = HttpClients.createDefault();        CloseableHttpResponse response = null;        try{            //定义传递参数            List<NameValuePair> formparams = new ArrayList<NameValuePair>();            for (Entry<String,Object> entry : params.entrySet()) {                formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()==null?"":entry.getValue().toString()));            }            url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(formparams), charset);            HttpGet httpget = new HttpGet(url);            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeOut).setConnectTimeout(connectTimeOut).setConnectionRequestTimeout(connReqTimeOut)                    .build();//设置请求和传输超时时间            httpget.setConfig(requestConfig);            response = httpclient.execute(httpget);             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {                HttpEntity entity = response.getEntity();                if (entity != null) {                    return EntityUtils.toString(entity, charset);                }            } else {                return result;            }        } finally{            if (response != null) {                try {                    response.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            try {                httpclient.close();            } catch (IOException e) {                e.printStackTrace();            }        }        return result;    }    /**     * @throws IOException      *      * @Title: httpGet      * @Description: httpGet请求工具类     * @param url     * @param params     * @param charset     * @param timeOut     * @return    设定文件     * @return String    返回类型     * @date 2015年7月15日 上午10:34:41      * @throws     */    public static String httpGet(String url, Map<String, Object> params, String charset, int timeOut) throws IOException{        return httpGet(url, params, charset, timeOut, timeOut, timeOut);    }

httpclient4.3 jar包

1 0