首页 > 解决方案 > HttpURLConnection URI 不能为空

问题描述

我想向网站发出 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

PS:我用的是jdk10

标签: java

解决方案


您的代码运行良好。

我用这个测试了代码:

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

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


推荐阅读