HttpUrlConnection 发送Http Get/Post 请求

本文对比了Socket与HTTP协议的特点,Socket属于传输层,适用于长连接通信,但易受防火墙影响;HTTP作为应用层协议,更易于跨平台使用,但数据传输格式不够友好。

socket的特点:属于传输层,是对Tcp/IP协议的实现
       1.开启端口,该通信为长连接通信,很容易被防火墙回收,可以通过心跳机制来实现,但是开发难度会比较大。
       2.传输的数据是字符串,可读性不强
       3.socket端口不便于推广
         http://45.113.192.102:2345    www.baidu.com
       4.性能相较于其他通信协议是最优秀的
    http协议访问:属于应用层的协议,对socket进行了封装
       1.跨平台
       2.传数据不够友好  
         http://localhost:8080?username=jack&age=18  

 


/**
 * rest 缓存考虑若要利用无状态操作的特性,
 * 
 * 有限的带宽和资源情况下使用
 * 
 * 返回的结构可以采用(由开发者定义的)任何格式
 * 
 * @author xxx
 *
 */
public class HttpUrlConnection {

    private static final Logger LOG = LoggerFactory.getLogger(HttpUrlConnection.class);

    public static String get(List<String> paramList) {

        String responseMsg = "";

        try {
            String content = "params=" + paramList.get(0);
            // 访问地址url对象
            URL url = new URL("http://localhost:8080/Base/testGet?" + content);
            // 创建网络连接对象 http
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方式
            connection.setRequestMethod("GET");
            // 设置超时时间 3秒
            connection.setConnectTimeout(3000);
            // 设置是否自行http重定向
            connection.setInstanceFollowRedirects(true);
            // 设置是否使用缓存
            connection.setDefaultUseCaches(true);
            // 设置是否从HttpURLConnection获取输出
            connection.setDoOutput(true);
            // 设置是否从HttpURLConnection输入
            connection.setDoInput(true);
            // 创建连接
            connection.connect();
            // 获取响应码
            int responseCode = connection.getResponseCode();

            // 判断是否成功响应
            if (responseCode == 200) {
                // 从响应流中读取数据
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line = "";
                while ((line = reader.readLine()) != null) {
                    responseMsg += line;
                }
                // 关闭数据流
                reader.close();
            }
            // 断开连接
            connection.disconnect();

            return responseMsg;

        } catch (IOException e) {
            LOG.error("IOException", e);
        }
        return responseMsg;

    }

    public static String post(List<String> paramlist) {

        String responseMsg = "";

        try {
            // 访问地址url对象
            URL url = new URL("http://localhost:8080/Base/testPost");
            // 创建网络连接对象 http
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方式
            connection.setRequestMethod("POST");
            // 设置超时时间 3秒
            connection.setConnectTimeout(3000);
            // 设置是否自行http重定向
            connection.setInstanceFollowRedirects(true);
            // 设置是否使用缓存
            connection.setDefaultUseCaches(true);
            // 设置是否从HttpURLConnection获取输出
            connection.setDoOutput(true);
            // 设置是否从HttpURLConnection输入
            connection.setDoInput(true);
            // 设置使用标准编码格式编码参数的名-值对
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // 创建连接
            connection.connect();

            // 写入参数到请求中
            String params = "ivString=" + paramlist.get(0) + "&password=" + paramlist.get(1);
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(params.getBytes());
            outputStream.flush();
            outputStream.close();

            // 获取响应码
            int responseCode = connection.getResponseCode();
            if (responseCode == 200) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line = "";
                if ((line = reader.readLine()) != null) {
                    responseMsg += line;
                }
                reader.close();
            }
            connection.disconnect();
            return responseMsg;
        } catch (IOException e) {
            LOG.error("IOException", e);
        }

        return responseMsg;

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值