我正在开发一个 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 类无法解析字符串的主机部分,因此 host
为 null
。
关于java - 我应该如何解决 java.lang.IllegalArgumentException : protocol = https host = null Exception?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/793232/