我应该如何解决 java.lang.IllegalArgumentException: protocol = https host = null Exception?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/793232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:33:27  来源:igfitidea点击:

How should I resolve java.lang.IllegalArgumentException: protocol = https host = null Exception?

javaexceptionsslstream

提问by Chathuranga Chandrasekara

I am working on a SSL client server program and I have to reuse the following method.

我正在开发一个 SSL 客户端服务器程序,我必须重用以下方法。

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

这里 ServerURL 被初始化为

private URL serverURL = null;

When I try to execute this method I get an exception at Line,

当我尝试执行此方法时,我在 Line 处遇到异常,

OutputStream os = conn.getOutputStream();

OutputStream os = conn.getOutputStream();

The exception is

例外是

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

What is the reason for this?

这是什么原因?

采纳答案by Aaron Digulla

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

URL 使用正斜杠 (/),而不是反斜杠(作为窗口)。尝试:

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, hostis null.

出现错误的原因是 URL 类无法解析字符串的主机部分,因此hostnull.

回答by matt b

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. serverURLSis 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 justassigned 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 serverURLSbeyond the previous line of code.
  1. serverURLS被赋值为 getRecipientURL(message)
  2. 然后立即覆盖 的值serverURLS,使之前的语句成为死存储
  3. 然后,因为if (serverURLS != null)计算结果为true,因为您刚刚在前面的语句中为变量分配了一个值,所以您为 分配了一个值serverURL。无法if (serverURLS != null)评价false
  4. 您实际上从未使用过serverURLS前一行代码之外的变量。

You could replace all of this with just:

您可以将所有这些替换为:

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