HttpURLConnection URI can't be null

KunLun :

I want to make a GET request to an website, but I 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.

Why I get URI can't be null?

P.S.: I'm using jdk10

Anish B. :

Your code is working fine.

I tested the code with this :

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

May be the url you are accessing is not GET type. Check for that.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=134351&siteId=1