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

标签 java exception ssl stream

我正在开发一个 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;

    }

这里ServerURL初始化为

private URL serverURL = null;

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

OutputStream os = conn.getOutputStream();

异常(exception)情况是

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

这是什么原因?

最佳答案

URL 使用正斜杠 (/),而不是反斜杠(如 windows)。尝试:

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

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

关于java - 我应该如何解决 java.lang.IllegalArgumentException : protocol = https host = null Exception?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/793232/

相关文章:

java - SSL java.security.NoSuchAlgorithmException

java - 应用程序正在运行,但现在意外关闭

java - JProfiler 调用堆栈拆分

unit-testing - 在没有 ExpectedException 属性的 nUnit 中预期异常

python - 在python中,调用后是否有可能发生异常,但其后的try block 为 "before"?

ssl - 允许用户对我的云端应用程序使用自定义域

python - 缓存条目反序列化失败,条目被忽略

java - 即使更改应用程序名称和 apk 文件名后仍需要替换应用程序吗?

java - 计算没有两个相邻字符相同的所有排列

java - ScriptEngineManager 特别谨慎的异常处理