HttpURLConnection封装


public class HttpTransfer {
    protected static final String CHARSET = "UTF-8";
    protected static final int readTimeout = 5 * 1000;
    protected static final int connectTimeout = 5 * 1000;

    //单例模式
    private HttpTransfer() {
    }

    private static HttpTransfer httpTransfer;

    public static HttpTransfer getInstance() {
        if (httpTransfer == null) {
            synchronized (HttpTransfer.class) {
                if (httpTransfer == null) {
                    httpTransfer = new HttpTransfer();
                }
            }
        }
        return httpTransfer;
    }


    //获取HttpURLConnection
    public HttpURLConnection createConnection(String urls) throws Exception {
        URL url = new URL(urls);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(connectTimeout);
        connection.setReadTimeout(readTimeout);
        return connection;
    }

    //获取流
    public InputStream getInputStream(String urls) throws Exception {
        HttpURLConnection connection = createConnection(urls);
        connection.setRequestMethod("GET");
        InputStream inputStream = connection.getInputStream();
        return inputStream;
    }

    //获取流数据
    public String getString(String urls) {
        InputStream in = null;
        ByteArrayOutputStream stream = null;
        String result = null;
        try {
            in = getInputStream(urls);
            stream = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = in.read(b)) != -1) {
                stream.write(b, 0, len);
            }
            result = stream.toString(CHARSET);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public boolean shouldBeProcessed(HttpURLConnection conn) throws IOException {
        return conn.getResponseCode() == 200;
    }


}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HttpURLConnection是Java中用于进行HTTP通信的类,它提供了一些方法来发送HTTP请求和接收HTTP响应。 HttpURLConnection类是Java标准库中的一部分,因此我们不需要额外导入任何包。我们可以通过调用URL对象的openConnection方法来创建一个HttpURLConnection实例。 一旦我们创建了HttpURLConnection对象,我们就可以设置请求方法(如GET或POST),设置请求头部信息,设置连接超时和读取超时时间,设置是否允许输入输出等。 发送请求时,我们可以使用getInputStream方法获取服务器响应的输入流,并通过输入流读取服务器响应的内容。此外,我们还可以通过getResponseCode获取服务器的HTTP响应状态码。 HttpURLConnection还提供了一些其他方法,如getHeaderField用于获取响应头部字段的值,getOutputStream用于获取发送请求时的输出流等。 为了方便使用,我们可以将发送HTTP请求和接收HTTP响应的功能封装到一个单独的方法中。这样我们可以直接调用这个方法来发送HTTP请求并获取响应。 封装HttpURLConnection时,我们可以将URL、请求方法、请求头部信息、请求体等作为方法的参数,方法中的逻辑可以参考上述的步骤。 另外,为了便于处理异常,我们可以在封装的方法中使用try-catch语句来捕获可能的异常,如IOException等。 封装HttpURLConnection可以使我们的代码更加简洁和可读性更高,同时也提高了代码的复用性。当我们需要发送HTTP请求时,只需要调用这个封装好的方法,而不需要重复编写相同的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值