views:

960

answers:

3

I am working on a SSL client server program and I have 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 at Line,

OutputStream os = conn.getOutputStream();

The exception is

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

What is the reason for this?

+7  A: 

URLs use forward slashes (/), not backward ones (as windows). Try:

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

The reason why you get the error is that the URL class can't parse the host part of the string and therefore, host is null.

Aaron Digulla
A: 

Not sure but this seems weird to me:

if (serverURLS != null){                 
    serverURL = new URL(serverURLS);         
}

Do you mean

if (serverURLS == null){                 
    serverURL = new URL(serverURLS);         
}

Wouldn't you want to initialize if it was null and not when it is not null.

jschoen
I thought that too at first, but note the slightly different variable names
Cogsy
Yes, just saw that. I guess I should read more closely in the future.
jschoen
+2  A: 

This code seems completely unnecessary:

String serverURLS = getRecipientURL(message);

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

if (serverURLS != null){
    serverURL = new URL(serverURLS);
}
  1. serverURLS is assigned the result of getRecipientURL(message)
  2. Then immediately you overwrite the value of serverURLS, making the previous statement a dead store
  3. Then, because if (serverURLS != null) evaluates to true, since you just assigned the variable a value in the preceding statement, you assign a value to serverURL. It is impossible for if (serverURLS != null) to evaluate to false!
  4. You never actually use the variable serverURLS beyond the previous line of code.

You could replace all of this with just:

serverURL = new URL("https:\\\\abc.my.domain.com:55555\\update");
matt b
Hi, That line is added by me just to check the reason for the error. Actually the URL is filtered using the message. Note that the assignment of ServerURLs is a Test line :)
Chathuranga Chandrasekara