我想向网站发出 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
最佳答案
您的代码运行良好。
我用这个测试了代码:
String url = "https://google.com";
HttpURLConnection connection = (HttpURLConnection)(new URL(url)).openConnection();
connection.setRequestMethod("GET");
connection.connect();
可能您访问的网址不是GET类型。检查一下。
关于java - HttpURLConnection URI 不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56902670/