HttpUrlConnection 发送Http Get/Post 请求

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Stephen_mu/article/details/87070939

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;

    }

}

猜你喜欢

转载自blog.csdn.net/Stephen_mu/article/details/87070939