开发可以处理HTTPS连接的代理servlet [英] Developing a proxy servlet that can handle HTTPS connections

查看:297
本文介绍了开发可以处理HTTPS连接的代理servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个Java servlet,它可以代理来自浏览器的HTTP请求。
我遇到了代理HTTPS请求的问题。

servlet似乎没有收到来自浏览器的任何HTTPS请求。

进一步调查后我注意到了HTTP请求似乎从一个简单的GET请求开始,而HTTPS请求以CONNECT请求开始,如下面的日志提取所示:

I've developed an Java servlet that can proxy HTTP requests from a browser. I am having an issue proxying HTTPS requests.
The servlet doesn't appear to receive any HTTPS requests from the browser.
Upon investigating this further I noticed that HTTP requests seem to start with a simple GET request whereas the HTTPS requests start with a CONNECT request as shown by the log extract below:

"CONNECT ajax.googleapis.com:443 HTTP/1.1" 200

我的问题是,是否有可能使用我的servlet来处理这个请求?

My question is, is it possible to handle this request using my servlet?

public class MyProxyServlet extends HttpServlet {
    @Override
    public void init(final ServletConfig config) throws ServletException {
        super.init(config);
    }

    @Override
    protected void doGet(final HttpServletRequest request,
            final HttpServletResponse response) throws ServletException,
            IOException {
    }

    @Override
    protected void doPost(final HttpServletRequest request,
            final HttpServletResponse response) throws ServletException,
            IOException {
    }
}

如果是,在哪里以及如何?

If so where and how?

推荐答案

由于CONNECT谓词不是由默认的 HttpServlet 实现处理的,因此您必须覆盖服务 servlet中 javax.servlet.http.HttpServlet 的方法,并自己处理 CONNECT 方法。原始实现似乎忽略了这个 resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED,errMsg); 。看看HttpServlet源代码 http:// www .docjar.com / html / api / javax / servlet / http / HttpServlet.java.html

Since the CONNECT verb is not handled by the default HttpServlet implementation, you will have to override the Service method of the javax.servlet.http.HttpServlet in your servlet and handle the CONNECT method yourself. The original implementation seems to ignore this with this resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);. Take a look at the HttpServlet source code http://www.docjar.com/html/api/javax/servlet/http/HttpServlet.java.html

这篇关于开发可以处理HTTPS连接的代理servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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