说明
我也遇到了这个问题,并通过提供我自己的sun.net.www.protocol.http.HttpURLConnection 实现来解决它,我用它来处理任何 AJAX 请求。我的类,方便地称为AjaxHttpURLConnection,挂钩到getInputStream() 函数,但不返回其原始输入流。相反,我将PipedInputStream 的实例返回给WebEngine。然后我读取来自原始输入流的所有数据并将其传递给我的管道流。
这样,我获得了 2 个好处:
- 我知道何时收到最后一个字节,因此 AJAX 请求已被完全处理。
- 我什至可以抓取所有传入的数据并已经使用它(如果我愿意的话)。
示例
首先,你必须告诉 Java 使用你的 URLConnection 实现而不是默认的。为此,您必须为其提供您自己的URLStreamHandlerFactory 版本。您可以在 SO(例如 this one)或通过 Google 找到有关此主题的许多线程。为了设置您的工厂实例,请将以下内容放在您的 main 方法的早期位置。这就是我的样子。
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
public class MyApplication extends Application {
// ...
public static void main(String[] args) {
URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
public URLStreamHandler createURLStreamHandler(String protocol) {
if ("http".equals(protocol)) {
return new MyUrlConnectionHandler();
}
return null; // Let the default handlers deal with whatever comes here (e.g. https, jar, ...)
}
});
launch(args);
}
}
其次,我们必须提出自己的Handler,它告诉程序何时使用哪种类型的URLConnection。
import java.io.IOException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import sun.net.www.protocol.http.Handler;
import sun.net.www.protocol.http.HttpURLConnection;
public class MyUrlConnectionHandler extends Handler {
@Override
protected URLConnection openConnection(URL url, Proxy proxy) throws IOException {
if (url.toString().contains("ajax=1")) {
return new AjaxHttpURLConnection(url, proxy, this);
}
// Return a default HttpURLConnection instance.
return new HttpURLConnection(url, proxy);
}
}
最后但同样重要的是,AjaxHttpURLConnection 来了。
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.net.Proxy;
import java.net.URL;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.io.IOUtils;
import sun.net.www.protocol.http.Handler;
import sun.net.www.protocol.http.HttpURLConnection;
public class AjaxHttpURLConnection extends HttpURLConnection {
private PipedInputStream pipedIn;
private ReentrantLock lock;
protected AjaxHttpURLConnection(URL url, Proxy proxy, Handler handler) {
super(url, proxy, handler);
this.pipedIn = null;
this.lock = new ReentrantLock(true);
}
@Override
public InputStream getInputStream() throws IOException {
lock.lock();
try {
// Do we have to set up our own input stream?
if (pipedIn == null) {
PipedOutputStream pipedOut = new PipedOutputStream();
pipedIn = new PipedInputStream(pipedOut);
InputStream in = super.getInputStream();
/*
* Careful here! for some reason, the getInputStream method seems
* to be calling itself (no idea why). Therefore, if we haven't set
* pipedIn before calling super.getInputStream(), we will run into
* a loop or into EOFExceptions!
*/
// TODO: timeout?
new Thread(new Runnable() {
public void run() {
try {
// Pass the original data on to the browser.
byte[] data = IOUtils.toByteArray(in);
pipedOut.write(data);
pipedOut.flush();
pipedOut.close();
// Do something with the data? Decompress it if it was
// gzipped, for example.
// Signal that the browser has finished.
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
} finally {
lock.unlock();
}
return pipedIn;
}
}
进一步考虑
- 如果您使用多个
WebEngine 对象,可能很难判断哪个对象实际打开了 URLConnection 以及哪个浏览器已完成加载。
- 您可能已经注意到我只处理 http 连接。我还没有测试我的方法可以在多大程度上转移到 https 等(这里不是专家:O)。
- 如您所见,我唯一知道何时真正使用我的
AjaxHttpURLConnection 的方法是相应的url 包含ajax=1。就我而言,这就足够了。但是,由于我不太擅长 html 和 http,所以我不知道 WebEngine 是否可以以任何不同的方式发出 AJAX 请求(例如,标头字段?)。如果有疑问,您总是可以简单地返回我们修改后的 url 连接的实例,但这当然意味着一些开销。
- 如开头所述,如果您愿意,可以在从输入流中检索到数据后立即处理数据。您可以获取您的
WebEngine 以类似方式发送的请求数据。只需包装 getOutputStream() 函数并放置另一个中间流来抓取正在发送的任何内容,然后将其传递给原始输出流。