zoukankan      html  css  js  c++  java
  • java网络请求HttpURLConnection

    1.

    @Test(timeout = 1000)
        public void testNet() throws IOException {
            URL url = new URL("https://cn.bing.com/search?q=fd");
            URLConnection connection = url.openConnection();
            HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
    
            httpURLConnection.setRequestMethod("GET");  //设置一些属性
    
    
            InputStream inputStream = httpURLConnection.getInputStream();//这个过程可能会阻塞,得到服务器响应的数据
            
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);//对输入流进行包装
            BufferedReader reader = new BufferedReader(inputStreamReader);
            String line;
            while((line=reader.readLine())!=null){
                System.out.println(line);
            }
            
            //关闭输入流对象
            while (reader!=null){
                reader.close();
                reader=null;
            }
            while(inputStreamReader!=null){
                inputStreamReader.close();
                inputStreamReader=null;
            }
            while(inputStream!=null){
                inputStream.close();
                inputStream=null;
            }
        }
  • 相关阅读:
    雨天拍照技巧
    was8.5和aop的问题:JVMVRFY013 违反类装入约束o
    Docker Swarm
    Docker compose
    docker 网络
    dockerFile
    docker容器数据卷
    docker容器数据卷
    dokcer镜像详解
    Portainer可视化面板安装
  • 原文地址:https://www.cnblogs.com/sunupo/p/15503728.html
Copyright © 2011-2022 走看看