httpclient 封装 http(get put post delete)请求

@SuppressWarnings("deprecation")
public class HttpClientUtil {
    private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
    private static final String ContentEncoding = "UTF-8";
    private static final int SocketTimeout = 5000;
 
    /**
     httpClient的get请求方式
     * @return
     * @throws Exception
     */
    public static String doGet(String url)   {
        logger.info("get请求{}",url);
      /*
       * 使用 GetMethod 来访问一个 URL 对应的网页,实现步骤:
       * 1:生成一个 HttpClinet 对象并设置相应的参数。
       * 2:生成一个 GetMethod 对象并设置响应的参数。
       * 3:用 HttpClinet 生成的对象来执行 GetMethod 生成的Get方法。
       * 4:处理响应状态码。
       * 5:若响应正常,处理 HTTP 响应内容。
       * 6:释放连接。
       */
      /* 1 生成 HttpClinet 对象并设置参数 */
        HttpClient httpClient = new HttpClient();
        // 设置 Http 连接超时为5秒
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(SocketTimeout);
      /* 2 生成 GetMethod 对象并设置参数 */
        GetMethod getMethod = new GetMethod(url);
        // 设置 get 请求超时为 5 秒
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, SocketTimeout);
        // 设置请求重试处理,用的是默认的重试处理:请求三次
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        String response = "";
      /* 3 执行 HTTP GET 请求 */
        try {
            int statusCode = httpClient.executeMethod(getMethod);
            logger.info("get请求{}",statusCode);
         /* 4 判断访问的状态码 */
            if (statusCode != HttpStatus.SC_OK&&statusCode != HttpStatus.SC_CREATED&&statusCode != HttpStatus.SC_NO_CONTENT) {
                logger.error("请求出错: "+ getMethod.getStatusLine());
                return response;
            }
            // 读取 HTTP 响应内容,这里简单打印网页内容
            byte[] responseBody = getMethod.getResponseBody();// 读取为字节数组
            response = new String(responseBody, ContentEncoding);
            logger.info("----------response:" + response);
            // 读取为 InputStream,在网页内容数据量大时候推荐使用
            // InputStream response = getMethod.getResponseBodyAsStream();
        } catch (HttpException e) {
            // 发生致命的异常,可能是协议不对或者返回的内容有问题
            logger.error("请检查输入的URL!");
            e.printStackTrace();
        } catch (IOException e) {
            // 发生网络异常
            logger.error("发生网络异常!");
            e.printStackTrace();
        } finally {
         /* 6 .释放连接 */
            getMethod.releaseConnection();
        }
        return response;
    }
 
    /**
     * HttpClient PUT请求
     * @author huang
     * @date 2013-4-10
     * @return
     */
    public static String doPut(String uri,String jsonObj){
        logger.info("put请求{},{}",uri,jsonObj);
 
        String resStr = "";
        HttpClient htpClient = new HttpClient();
        PutMethod putMethod = new PutMethod(uri);
        putMethod.addRequestHeader( "Content-Type","application/json" );
        putMethod.getParams().setParameter( HttpMethodParams.HTTP_CONTENT_CHARSET, ContentEncoding );
        putMethod.setRequestBody( jsonObj );
        try{
            int statusCode = htpClient.executeMethod( putMethod );
            logger.info("put请求{}",statusCode);
            if (statusCode != HttpStatus.SC_OK&&statusCode != HttpStatus.SC_CREATED&&statusCode != HttpStatus.SC_NO_CONTENT) {
                logger.error("Method failed: "+putMethod.getStatusLine() );
                return resStr;
            }
            byte[] responseBody = putMethod.getResponseBody();
            resStr = new String(responseBody,ContentEncoding);
            logger.info("response:" + resStr);
        }catch(Exception e){
            logger.error(" failed: " + e.getMessage());
            e.printStackTrace();
        }finally{
            putMethod.releaseConnection();
        }
        return resStr;
    }
 
    /**
     * post请求
     * @param url
     * @param jsonObj
     * @return
     */
    @SuppressWarnings({ "resource" })
    public static String doPost(String url,String jsonObj){
        logger.info("post请求{},{}",url,jsonObj);
 
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        post.setHeader("Content-Type", "application/json; charset=UTF-8");
        String response = "";
        try {
            StringEntity stringEntity = new StringEntity( jsonObj,"UTF-8");
            stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            post.setEntity(stringEntity);
            HttpResponse res = client.execute(post);
            int statusCode=res.getStatusLine().getStatusCode();
            logger.info("post请求",statusCode);
            if (statusCode != HttpStatus.SC_OK&&statusCode != HttpStatus.SC_CREATED&&statusCode != HttpStatus.SC_NO_CONTENT) {
                logger.error("Method failed: "+res.getStatusLine() );
                return response;
            }
            response = EntityUtils.toString(res.getEntity());// 返回json格式:
            logger.info("----------response:" + response);
        } catch (Exception e) {
            logger.error(" failed: " + e.getMessage());
            e.printStackTrace();
        }
        return response;
    }
 
    public static String doDelete(String uri)  {
        logger.info("delete请求",uri);
 
        String data= "";
        HttpClient httpClient= new HttpClient();
        httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, ContentEncoding);
        DeleteMethod method = null;
        try{
            method= new DeleteMethod();
            method.setURI(new URI(uri,false));
            method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
            method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, SocketTimeout);
            int statusCode = httpClient.executeMethod(method);
            logger.info("delete请求",statusCode);
            if (statusCode != HttpStatus.SC_OK&&statusCode != HttpStatus.SC_CREATED&&statusCode != HttpStatus.SC_NO_CONTENT) {
                logger.error("Method failed: " + method.getStatusLine());
                return data;
            }
            data= new String(method.getResponseBody(),ContentEncoding);
            logger.info("response:" + data);
        }catch(HttpException e){
            e.printStackTrace();
            logger.error("Please check your provided http address!");
        }catch(IOException e){
            e.printStackTrace();
            logger.error(e.getMessage());
        }catch(Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
        }finally{
            if(method!=null)
                method.releaseConnection();
        }
        return data;
    }
 
 
    public static void main(String args[]) {

    }
}

 

posted @ 2018-07-17 14:15  Azjs丶V1  阅读(444)  评论(0编辑  收藏  举报