at java.net.url init,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.(URL.java:593)

at java.net.URL.(URL.java:490)

at java.net.URL.(URL.java:439)

*/

URL url = new URL(null);

/*

java.net.MalformedURLException

at java.net.URL.(URL.java:627)

at java.net.URL.(URL.java:490)

at java.net.URL.(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.(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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值