zoukankan      html  css  js  c++  java
  • java.net 基本测试

    java.net 基本测试

    java.net

    java.net.ssl

    java.net.URL
    

    测试类

    package com.mozq.boot.kuayu01.demo;
    
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class URL_01 {
        public static void main(String[] args) {
            try {
                URL url = new URL("");
                /*
                java.net.MalformedURLException: no protocol:
                    at java.net.URL.<init>(URL.java:593)
                    at java.net.URL.<init>(URL.java:490)
                    at java.net.URL.<init>(URL.java:439)
                 */
                
                URL url = new URL(null);
                /*
                java.net.MalformedURLException
                    at java.net.URL.<init>(URL.java:627)
                    at java.net.URL.<init>(URL.java:490)
                    at java.net.URL.<init>(URL.java:439)
                    at com.mozq.boot.kuayu01.demo.URL_01.main(URL_01.java:10)
                Caused by: java.lang.NullPointerException
                    at java.net.URL.<init>(URL.java:532)
                    ... 3 more
                 */
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    }
    
    package com.mozq.boot.kuayu01.demo;
    
    import com.alibaba.fastjson.JSONObject;
    
    import java.io.*;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    
    public class URL_02 {
        public static void main(String[] args) throws IOException {
            //URL url = new URL("http://localhost:9001/demo/name");
            URL url = new URL("http://www.baidu.com");
            URLConnection conn = url.openConnection();
            conn.setDoInput(true);
            conn.connect();
    
            //读取输入流
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder result = new StringBuilder();
            String line = null;
            while((line = reader.readLine()) != null){
                result.append(line);
            }
            String s = result.toString();
            System.out.println(s);
        }
    }
    
    @RestController
    @RequestMapping("/demo")
    public class DemoController {
        @RequestMapping("/name")
        public String name(HttpServletResponse response){
            return "测试数据";
        }
    }
    

    异常

    Exception in thread "main" java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)
    
    java.net.MalformedURLException: no protocol: 
    
  • 相关阅读:
    git 之gitignore 添加项之后生效的问题
    使用 padding-bottom 设置高度基于宽度的自适应
    ES5中新增的Array方法详细说明
    zepto.js 自定义打包集成其他模块构建流程
    移动端如何让页面强制横屏
    快来看看抓包工具有哪些?
    实践出真知,小程序wepy,uni-app框架开发使用!
    开发过程遇到的css样式问题记录
    带坑使用微信小程序框架WePY组件化开发项目,附带第三方插件使用坑
    微信 + weui 框架记录
  • 原文地址:https://www.cnblogs.com/mozq/p/11867171.html
Copyright © 2011-2022 走看看