为URLStreamHandlerFactory创建HttpURLConnection,获取错误:协议不支持输入 [英] Creating HttpURLConnection for URLStreamHandlerFactory, getting Error: protocol doesn't support input

查看:451
本文介绍了为URLStreamHandlerFactory创建HttpURLConnection,获取错误:协议不支持输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些背景知识,我正在尝试创建一个URL流处理程序,这样我就可以在javafx应用程序中跟踪我在webview上活动的连接数。基本上,我在WebView中运行AngularJs应用程序,我想知道它何时完成。我无法触摸网站代码,因此不在桌面上添加js通知程序。所以,无论我把它放在一起,设置总是错误'协议不支持输入。'我试图用一个只返回false的方法覆盖'getDoInput',但我仍然得到错误。有什么想法吗?

A bit of background, I'm trying to create a URL Stream Handler so I can keep track of how many connections I have active on my webview in my javafx application. Essentially, I'm running an AngularJs app in the WebView, and I'd like to know when it's finished. I can't touch the web site code, so adding a js notifier is not on the table. So, no matter what I put together, the setup always errors with 'protocol doesn't support input.' I've tried to override 'getDoInput' with a method that only returns false, but I still get the error. Any ideas?

这与我正在做的事情很接近:

Here is something close to what I'm doing:

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());


推荐答案

我明白你要完成的是什么,我认为这是错误的做法。

I understand what you're trying to accomplish however, I think this is the wrong way to go about it.


来自: Elliotte Rusty的Java网络编程Harold

java.net中只有抽象的 URLConnection 包。具体子类隐藏在 sun.net 包层次结构中。很少直接在源代码中实例化 URLConnection 对象;相反,运行时环境根据需要创建这些对象,具体取决于所使用的协议。然后使用 forName() newInstance()方法实例化类(在编译时未知)。 java.lang.Class 类。

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.

例如, connect() sun.net.www.protocol.http.HttpURLConnection 的方法创建 sun.net.www.http.HttpClient object,负责连接服务器。

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,否则我建议您探索其他途径。

So unless you want to write your own http protocol handler and an HttpClient, I would suggest exploring other avenues.

我能找到的唯一方法,抛出 UnknownServiceException 消息为协议不支持输入是:

The only method, that I could find, that throws an UnknownServiceException with the message being "protocol doesn't support input" is:

/**
* 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 以仅返回false。相反,你应该使用 setDoInput(false)。但是,您不希望将 doInput 设置为false。你总是想读一些东西,例如回复代码。

Overriding getDoInput
You should not override getDoInput to only return false. Instead you should use setDoInput(false). However, you don't want to set doInput to false. You always want to read something, for instance the response code.

这篇关于为URLStreamHandlerFactory创建HttpURLConnection,获取错误:协议不支持输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆