美文网首页
HttpSimpleUtils

HttpSimpleUtils

作者: 翼徳 | 来源:发表于2016-07-26 17:09 被阅读108次
    功能:

    基于HttpURLConnection 实现的常用 Http 常用方法:

    • post
    • get
    • put
    • delete
    源码:

    package com.dotions.utils;
    
    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.Map;
    
    /**
     * @Description HTTP toolkit
     * 
     * @author Dotions 2016年7月26日 下午4:50:26
     */
    public class HttpSimpleUtils {
    
        static {
            System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
        }
    
        public static String post(String strUrl, final String json, Map<String, String> header) throws IOException {
            final HttpURLConnection connection = getHttpURLConnection(strUrl);
    
            return execute(connection, header, new HttpCallback() {
                public void doSomething() throws IOException {
                    connection.setDoOutput(true);
                    connection.setRequestMethod(Method.POST);
                    IOUtil.copy(json.getBytes(), connection.getOutputStream());
    
                }
            });
        }
    
        public static String post(String strUrl, final String json) throws IOException {
            return post(strUrl, json, null);
        }
    
        public static String get(String strUrl, Map<String, String> header) throws IOException {
            final HttpURLConnection connection = getHttpURLConnection(strUrl);
    
            return execute(connection, header, new HttpCallback() {
                public void doSomething() throws IOException {
                    connection.setRequestMethod(Method.GET);
                }
            });
        }
    
        public static String get(String strUrl) throws IOException {
            return get(strUrl, null);
        }
    
        public static String put(String strUrl, final String json, Map<String, String> header) throws IOException {
            final HttpURLConnection connection = getHttpURLConnection(strUrl);
    
            return execute(connection, header, new HttpCallback() {
                public void doSomething() throws IOException {
                    connection.setDoOutput(true);
                    connection.setRequestMethod(Method.PUT);
                    IOUtil.copy(json.getBytes(), connection.getOutputStream());
                }
            });
        }
    
        public static String put(String strUrl, final String json) throws IOException {
            return put(strUrl, json, null);
        }
    
        public static String delete(String strUrl, Map<String, String> header) throws IOException {
            final HttpURLConnection connection = getHttpURLConnection(strUrl);
    
            return execute(connection, header, new HttpCallback() {
                public void doSomething() throws IOException {
                    connection.setRequestMethod(Method.DELETE);
                }
            });
        }
    
        public static String delete(String strUrl) throws IOException {
            return delete(strUrl, null);
        }
    
        /*
         * Execute http request and return result
         */
        private static String execute(HttpURLConnection connection, Map<String, String> header, HttpCallback callback)
                throws IOException {
            if (connection == null) {
                return null;
            }
    
            // Set custom http header
            if (header != null && header.size() > 0) {
                for (String key : header.keySet()) {
                    connection.setRequestProperty(key, header.get(key));
                }
            }
    
            // Set timeout
            connection.setReadTimeout(DefaultTimeOut.SOCKET);
            connection.setConnectTimeout(DefaultTimeOut.CONNECTION);
    
            if (callback != null) {
                callback.doSomething();
            }
    
            System.out.println("responseCode = " + connection.getResponseCode());
    
            if (connection.getResponseCode() == ResponseCode.SUCCESS) {
                return StringUtil.toString(connection.getInputStream());
            }
    
            return null;
    
        }
    
        /*
         * Get http URL connection
         */
        private static HttpURLConnection getHttpURLConnection(String strUrl) throws IOException {
            if (StringUtil.isBlank(strUrl)) {
                return null;
            }
    
            URL url = new URL(strUrl);
            return (HttpURLConnection) url.openConnection();
        }
    
        /**
         * Default timeout config
         */
        private static interface DefaultTimeOut {
            public int SOCKET = 20000;
            public int CONNECTION = 5000;
        }
    
        /**
         * Http method
         */
        private static interface Method {
            public String GET = "GET";
            public String POST = "POST";
            public String PUT = "PUT";
            public String DELETE = "DELETE";
        }
    
        /**
         * Http ResponseCode
         */
        @SuppressWarnings("unused")
        private static interface ResponseCode {
            public int SUCCESS = 200;
            public int NOT_FOUND = 404;
        }
    
        private interface HttpCallback {
            public void doSomething() throws IOException;
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:HttpSimpleUtils

          本文链接:https://www.haomeiwen.com/subject/jqnnjttx.html