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: 
    
  • 相关阅读:
    成都Uber优步司机快速注册攻略(外地车牌也可加入,不用现场培训)
    uber司机 如何提高评分、接单率、成单率?
    uber奖励和账单详解
    优步uber司机常见问题与答案(成都地区官方)
    成都优步uber司机第四组奖励政策
    疯狂的补贴,广州司机都被Uber触动
    scheme 宏macro写法
    scheme 模拟queue
    scheme I/0 输入输出操作
    scheme递归
  • 原文地址:https://www.cnblogs.com/mozq/p/11867171.html
Copyright © 2011-2022 走看看