HttpURLConnection 类笔记

HTTP HTTPS

没有规矩, 不成方圆. 人类的世界有了法律, 从此很多事情有了边界. 计算机世界 也必须存在各种类似的东西去规范 文本的传输, 邮件的上传, 图片的下载等等

http: 超文本传输协议, 无状态的, 明文传输的, 不安全的 https: 基于超文本传输协议, 最下层 加了 SSL协议, ssl中对请求添加了校验, 对响应进行了加密操作, 是安全的

HttpURLConnection

java 提供的针对 http 协议的请求处理类

    HttpURLConnection connection = null;
    try {
      // 1. 获取HttpURLConnection类的实例
      URL url = new URL("https://www.baidu.com");
      connection = (HttpURLConnection)url.openConnection();

      // 2. 设置HTTP请求的参数
      connection.setRequestMethod("GET"); //设置请求方式为GET
      connection.setConnectTimeout(8000);  //设置连接超时为8s
      connection.setReadTimeout(8000);    //设置读取操作超时为8s

      // 3. 连接远程资源,并对服务器响应进行判断
      connection.connect();

      // 4. 获取相应码
      int responseCode = connection.getResponseCode();
      if (HttpURLConnection.HTTP_OK == responseCode) {
        // 5. 获取服务端返回 响应流
        InputStream in = connection.getInputStream();

        // 6. 读取流文件
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));

        // 7. 接收流中读取的数据
        StringBuffer sb = new StringBuffer();
        while(null != bufferedReader.readLine()) {
          sb.append(bufferedReader.readLine());
        }
        System.out.println("---------读取的文件-----------");
        System.out.println(sb);
      } else {
        String responseMessage = connection.getResponseMessage();
        throw new HttpException(responseMessage);
      }
    } catch (MalformedURLException e) {
      throw new RuntimeException(e);
    } catch (IOException e) {
      throw new RuntimeException(e);
    } finally {
      Optional.ofNullable(connection).ifPresent(i -> {
        i.disconnect();
      });
    }

结果:

D:\jdk\bin\java.exe "-javaagent:D:\idea\IntelliJ IDEA 2022.2.3\lib\idea_rt.jar=52889:D:\idea\IntelliJ IDEA 2022.2.3\bin" -Dfile.encoding=UTF-8 -classpath D:\jdk\jre\lib\charsets.jar;D:\jdk\jre\lib\deploy.jar;D:\jdk\jre\lib\ext\access-bridge-64.jar;D:\jdk\jre\lib\ext\cldrdata.jar;D:\jdk\jre\lib\ext\dnsns.jar;D:\jdk\jre\lib\ext\jaccess.jar;D:\jdk\jre\lib\ext\jfxrt.jar;D:\jdk\jre\lib\ext\localedata.jar;D:\jdk\jre\lib\ext\nashorn.jar;D:\jdk\jre\lib\ext\sunec.jar;D:\jdk\jre\lib\ext\sunjce_provider.jar;D:\jdk\jre\lib\ext\sunmscapi.jar;D:\jdk\jre\lib\ext\sunpkcs11.jar;D:\jdk\jre\lib\ext\zipfs.jar;D:\jdk\jre\lib\javaws.jar;D:\jdk\jre\lib\jce.jar;D:\jdk\jre\lib\jfr.jar;D:\jdk\jre\lib\jfxswt.jar;D:\jdk\jre\lib\jsse.jar;D:\jdk\jre\lib\management-agent.jar;D:\jdk\jre\lib\plugin.jar;D:\jdk\jre\lib\resources.jar;D:\jdk\jre\lib\rt.jar;D:\extend-workspace\basic-study\target\classes;D:\repository\org\apache\commons\commons-lang3\3.12.0\commons-lang3-3.12.0.jar;D:\repository\cn\hutool\hutool-all\5.8.10\hutool-all-5.8.10.jar;D:\repository\org\hswebframework\hsweb-utils\3.0.3\hsweb-utils-3.0.3.jar;D:\repository\joda-time\joda-time\2.7\joda-time-2.7.jar com.huntkey.sceo.http.HttpURLTest
---------读取的文件-----------
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');null

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值