Creating HttpURLConnection for URLStreamHandlerFactory, getting Error: protocol doesn't support input
Creating HttpURLConnection for URLStreamHandlerFactory, getting Error: protocol doesn't support input
一些背景知识,我正在尝试创建一个 URL 流处理程序,以便我可以跟踪在我的 javafx 应用程序中我的 webview 上有多少活动连接。本质上,我是 WebView 中的 运行 一个 AngularJs 应用程序,我想知道它何时完成。不能动网站代码,所以加个js notifier就不上了table。所以,无论我把什么放在一起,设置总是出现“协议不支持输入”的错误。我尝试使用仅 returns false 的方法覆盖 'getDoInput',但我仍然收到错误。有什么想法吗?
这是我正在做的事情:
public class MyUrlStreamHandlerFactory implements URLStreamHandlerFactory {
public URLStreamHandler createURLStreamHandler(String protocol) {
if (protocol.equalsIgnoreCase("http") || protocol.equalsIgnoreCase("https")) {
return new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL url) throws IOException {
return new HttpURLConnection(url) {
@Override
public void connect() throws IOException {
}
@Override
public void disconnect() {
}
@Override
public boolean usingProxy() {
return false;
}
@Override
public boolean getDoInput() {
return false;
}
};
}
};
}
return null;
}
}
我正在安装它:
URL.setURLStreamHandlerFactory(new MyUrlStreamHandlerFactory());
我理解你想要完成的事情,但是我认为这是错误的做法。
From: Java Network Programming by Elliotte Rusty Harold
Only abstract URLConnection
classes are present in the java.net
package. The concrete subclasses are hidden inside the sun.net
package hierarchy. It is rare to instantiate URLConnection
objects directly in your source code; instead, the runtime environment creates these objects as needed, depending on the protocol in use. The class (which is unknown at compile time) is then instantiated using the forName()
and newInstance()
methods of the java.lang.Class
class.
For example, the connect()
method of sun.net.www.protocol.http.HttpURLConnection
creates a sun.net.www.http.HttpClient
object, which is responsible for connecting to the server.
因此,除非您想编写自己的 http 协议处理程序和 HttpClient,否则我建议探索其他途径。
其他
我能找到的唯一抛出消息 "protocol doesn't support input"
的 UnknownServiceException
的方法是:
java.net.URLConnection#getInputStream
/**
* Returns an input stream that reads from this open connection.
*
* @return an input stream that reads from this open connection.
* @exception IOException if an I/O error occurs while
* creating the input stream.
* @exception UnknownServiceException if the protocol does not support
* input.
*/
public InputStream getInputStream() throws IOException {
throw new UnknownServiceException("protocol doesn't support input");
}
覆盖 getDoInput
您不应将 getDoInput
覆盖为仅 return false。相反,您应该使用 setDoInput(false)
。但是,您不想将 doInput
设置为 false。你总是想读一些东西,例如响应代码。
一些背景知识,我正在尝试创建一个 URL 流处理程序,以便我可以跟踪在我的 javafx 应用程序中我的 webview 上有多少活动连接。本质上,我是 WebView 中的 运行 一个 AngularJs 应用程序,我想知道它何时完成。不能动网站代码,所以加个js notifier就不上了table。所以,无论我把什么放在一起,设置总是出现“协议不支持输入”的错误。我尝试使用仅 returns false 的方法覆盖 'getDoInput',但我仍然收到错误。有什么想法吗?
这是我正在做的事情:
public class MyUrlStreamHandlerFactory implements URLStreamHandlerFactory {
public URLStreamHandler createURLStreamHandler(String protocol) {
if (protocol.equalsIgnoreCase("http") || protocol.equalsIgnoreCase("https")) {
return new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL url) throws IOException {
return new HttpURLConnection(url) {
@Override
public void connect() throws IOException {
}
@Override
public void disconnect() {
}
@Override
public boolean usingProxy() {
return false;
}
@Override
public boolean getDoInput() {
return false;
}
};
}
};
}
return null;
}
}
我正在安装它:
URL.setURLStreamHandlerFactory(new MyUrlStreamHandlerFactory());
我理解你想要完成的事情,但是我认为这是错误的做法。
From: Java Network Programming by Elliotte Rusty Harold
Only abstract
URLConnection
classes are present in thejava.net
package. The concrete subclasses are hidden inside thesun.net
package hierarchy. It is rare to instantiateURLConnection
objects directly in your source code; instead, the runtime environment creates these objects as needed, depending on the protocol in use. The class (which is unknown at compile time) is then instantiated using theforName()
andnewInstance()
methods of thejava.lang.Class
class.For example, the
connect()
method ofsun.net.www.protocol.http.HttpURLConnection
creates asun.net.www.http.HttpClient
object, which is responsible for connecting to the server.
因此,除非您想编写自己的 http 协议处理程序和 HttpClient,否则我建议探索其他途径。
其他
我能找到的唯一抛出消息 "protocol doesn't support input"
的 UnknownServiceException
的方法是:
java.net.URLConnection#getInputStream
/**
* Returns an input stream that reads from this open connection.
*
* @return an input stream that reads from this open connection.
* @exception IOException if an I/O error occurs while
* creating the input stream.
* @exception UnknownServiceException if the protocol does not support
* input.
*/
public InputStream getInputStream() throws IOException {
throw new UnknownServiceException("protocol doesn't support input");
}
覆盖 getDoInput
您不应将 getDoInput
覆盖为仅 return false。相反,您应该使用 setDoInput(false)
。但是,您不想将 doInput
设置为 false。你总是想读一些东西,例如响应代码。