手写HTTP网络请求框架

作者 : 开心源码 本文共5763个字,预计阅读时间需要15分钟 发布时间: 2022-05-12 共221人阅读

创立基于HttpUrlConnection的具体获取网络数据流HttpUrlConnectionUtil

public class HttpUrlConnectionUtil {    public static ByteArrayOutputStream execute(Request request) throws HttpException {        switch (request.requestMethod) {            case GET:                return get(request);            case POST:                return post(request);        }        return null;    }    private static ByteArrayOutputStream get(Request request) throws HttpException {        try {            HttpURLConnection connection = (HttpURLConnection) new URL(request.url).openConnection();            connection.setRequestMethod("GET");            connection.setConnectTimeout(15 * 1000);            addHeaders(connection, request);            if (HttpStatus.HTTP_OK == connection.getResponseCode()) {                InputStream is = connection.getInputStream();                ByteArrayOutputStream baos = new ByteArrayOutputStream();                byte[] buffer = new byte[2048];                int len = 0;                while (-1 != (len = is.read(buffer))) {                    baos.write(buffer, 0, len);                }                baos.flush();                baos.close();                is.close();                return baos;            }        } catch (MalformedURLException e) {            throw new HttpException(HttpException.Status.XXX_EXCEPTION);        } catch (ProtocolException e) {            throw new HttpException(HttpException.Status.XXX_EXCEPTION);        } catch (SocketTimeoutException e) {            throw new HttpException(HttpException.Status.TIMEOUT_EXCEPTION);        } catch (IOException e) {            throw new HttpException(HttpException.Status.XXX_EXCEPTION);        }        throw new HttpException(HttpException.Status.XXX_EXCEPTION);    }    private static ByteArrayOutputStream post(Request request) throws HttpException {        try {            HttpURLConnection connection = (HttpURLConnection) new URL(request.url).openConnection();            connection.setRequestMethod("POST");            connection.setConnectTimeout(15 * 1000);            connection.setDoOutput(true);            connection.setDoInput(true);            addHeaders(connection, request);            StringBuilder out = new StringBuilder();            for (String key : request.body.keySet()) {                if (out.length() != 0) {                    out.append("&");                }                out.append(key).append("=").append(request.body.get(key));            }            OutputStream outputStream = connection.getOutputStream();            outputStream.write(out.toString().getBytes());            if (HttpStatus.HTTP_OK == connection.getResponseCode()) {                InputStream is = connection.getInputStream();                ByteArrayOutputStream baos = new ByteArrayOutputStream();                byte[] buffer = new byte[2048];                int len = 0;                while (-1 != (len = is.read(buffer))) {                    baos.write(buffer, 0, len);                }                baos.flush();                baos.close();                is.close();                return baos;            }        } catch (MalformedURLException e) {            throw new HttpException(HttpException.Status.XXX_EXCEPTION);        } catch (ProtocolException e) {            throw new HttpException(HttpException.Status.XXX_EXCEPTION);        } catch (SocketTimeoutException e) {            throw new HttpException(HttpException.Status.TIMEOUT_EXCEPTION);        } catch (IOException e) {            throw new HttpException(HttpException.Status.XXX_EXCEPTION);        }        throw new HttpException(HttpException.Status.XXX_EXCEPTION);    }    private static void addHeaders(HttpURLConnection connection, Request request) {        for (Map.Entry<String, String> header : request.headers.entrySet()) {            connection.addRequestProperty(header.getKey(), header.getValue());        }    }}

包装具体每一个请求的Request类

public class Request {    /**     * 请求地址     */    public String url;    /**     * 请求form表单     */    public Map<String, String> body;    /**     * 请求头     */    public Map<String, String> headers = new HashMap<>();    /**     * 请求方法     */    public RequestMethod requestMethod;    /**     * 请求回调     */    public AbsCallback callBack;    /**     * 请求重试次数     */    public int maxRequestCount = 3;    public enum RequestMethod {GET, POST}    public Request(String url, RequestMethod requestMethod) {        this.url = url;        this.requestMethod = requestMethod;    }    public Request setUrl(String url) {        this.url = url;        return this;    }    public Request setBody(Map<String, String> body) {        this.body = body;        return this;    }    public void setCallBack(AbsCallback callBack) {        this.callBack = callBack;    }}

基于Handler、ThreadPoolExecutor线程池的异步请求解决类

public class RequestTask {    private static final int MAIN_THREAD = 0x0001;    private static InnerHandler handler = new InnerHandler();    private final ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5,            Integer.MAX_VALUE,            60 * 1000,            TimeUnit.MILLISECONDS,            new LinkedBlockingDeque<Runnable>(128));    private Request request;    public RequestTask(Request request) {        this.request = request;    }    public void execute() {        poolExecutor.execute(new Runnable() {            @Override            public void run() {                Result result = request(0);                Message message = handler.obtainMessage();                message.obj = result;                message.what = MAIN_THREAD;                handler.sendMessage(message);            }        });    }    private Result request(int retry) {        try {            final ByteArrayOutputStream response = HttpUrlConnectionUtil.execute(request);            return new Result(RequestTask.this, request.callBack.handleData(response));        } catch (HttpException e) {            if (e.type == HttpException.Status.TIMEOUT_EXCEPTION) {                if (retry < request.maxRequestCount) {                    retry++;                    request(retry);                }            }            return new Result(RequestTask.this, e);        }    }    private static class InnerHandler extends Handler {        private InnerHandler() {            super(Looper.getMainLooper());        }        @Override        public void handleMessage(Message msg) {            Result result = (Result) msg.obj;            switch (msg.what) {                case MAIN_THREAD:                    if (result.response instanceof Exception) {                        result.requestTask.request.callBack.failure((Exception) result.response);                        return;                    }                    result.requestTask.request.callBack.sucess(result.response);            }        }    }    private static class Result<T> {        private RequestTask requestTask;        private T response;        private Result(RequestTask requestTask, T response) {            this.requestTask = requestTask;            this.response = response;        }    }}

可拓展的响应解决callback,自己可根据需要拓展

public interface AbsCallback<T> {    void sucess(T response);    void failure(Exception e);    T handleData(ByteArrayOutputStream response) throws HttpException;}
public abstract class JsonCallback<T> implements AbsCallback<T> {    private static Gson gson = new Gson();    @Override    @SuppressWarnings("unchecked")    public T handleData(ByteArrayOutputStream response) throws HttpException {        try {            Class<T> clz= (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];            return gson.fromJson(response.toString("UTF-8"),clz);        } catch (UnsupportedEncodingException e) {            throw new HttpException(HttpException.Status.XXX_EXCEPTION);        }    }}
public abstract class StringCallback implements AbsCallback<String> {    @Override    public String handleData(ByteArrayOutputStream response) throws HttpException {        try {            return response.toString("UTF-8");        } catch (UnsupportedEncodingException e) {            throw new HttpException(HttpException.Status.XXX_EXCEPTION);        }    }}

有问题,请留言交流

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 手写HTTP网络请求框架

发表回复