How do I resolve java.lang.IllegalArgumentException: protocol = https host = null exception?

I am working on an SSL client server program and I need to reuse the following method.

private boolean postMessage(String message){ try{ String serverURLS = getRecipientURL(message); serverURLS = "https:\\\\abc.my.domain.com:55555\\update"; if (serverURLS != null){ serverURL = new URL(serverURLS); } HttpsURLConnection conn = (HttpsURLConnection)serverURL.openConnection(); conn.setHostnameVerifier(new HostnameVerifier() { public boolean verify(String arg0, SSLSession arg1) { return true; } }); conn.setDoOutput(true); OutputStream os = conn.getOutputStream(); OutputStreamWriter wr = new OutputStreamWriter(os); wr.write(message); wr.flush(); if (conn.getResponseCode() != HttpsURLConnection.HTTP_OK) return false; else return true; } 

Here ServerURL is initialized as

 private URL serverURL = null; 

When I try to execute this method, I get an exception in the string,

OutputStream os = conn.getOutputStream ();

The exception is

 java.lang.IllegalArgumentException: protocol = https host = null 

What is the reason for this?

+7
java stream exception ssl
source share
2 answers

URLs use slashes (/) rather than backslashes (like windows). Try:

 serverURLS = "https://abc.my.domain.com:55555/update"; 

The reason you get the error message is because the URL class cannot parse the host part of the string and therefore host is null .

+18
source

This code seems completely unnecessary:

 String serverURLS = getRecipientURL(message); serverURLS = "https:\\\\abc.my.domain.com:55555\\update"; if (serverURLS != null){ serverURL = new URL(serverURLS); } 
  • serverURLS assigned the result getRecipientURL(message)
  • Then, immediately, you overwrite the serverURLS value, making the previous statement a dead store
  • Then, since if (serverURLS != null) evaluates to true , since you just assigned the variable the value in the previous expression, you serverURL value. It is impossible for if (serverURLS != null) evaluate to false !
  • You never use the serverURLS variable outside the previous line of code.

You can replace all of this simply:

 serverURL = new URL("https:\\\\abc.my.domain.com:55555\\update"); 
+3
source

All Articles