HttpURLConnection URI不能为null

时间:2019-07-05 12:00:26

标签: java

我想向网站发出GET请求,但我收到URI can't be null.

String url = "https://valid_url.com";

HttpURLConnection connection = (HttpURLConnection)(new URL(url)).openConnection();

connection.setRequestMethod("GET");

connection.connect(); //-> java.lang.IllegalArgumentException: URI can't be null.

为什么我得到URI can't be null

P.S .:我正在使用jdk10

1 个答案:

答案 0 :(得分:2)

您的代码运行正常。

我以此测试代码:

String url = "https://google.com";
HttpURLConnection connection = (HttpURLConnection)(new URL(url)).openConnection();
connection.setRequestMethod("GET");
connection.connect();

您访问的网址可能不是 GET 类型。检查一下。