Java Proxy Servlet 在浏览器上显示原始 html

标签 java html servlets

我正在使用下面的代码

public class ProxyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private final String USER_AGENT = "Mozilla/5.0";   
    public ProxyServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                                //  Create Get request dynamically to remote server
        String url = "http://internalserver/path";

        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // optional default is GET
        con.setRequestMethod("GET");

        //add request header
        con.setRequestProperty("User-Agent", USER_AGENT);

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response1 = new StringBuffer();

        ServletOutputStream sout = response.getOutputStream();

        while ((inputLine = in.readLine()) != null) {
            response1.append(inputLine);
            sout.write(inputLine.getBytes());
        }
        in.close();

        sout.flush();

    }

---- 代码的其他部分 --- 没有粘贴到这里

From : ProxyServlet.java http:blog.sodhanalibrary.com/2014/05/proxy-servlet-to-forward-requests-to.html

我将网址直接更改为内部站点

当我访问 servlet 时,它看起来像是从远程站点获取 html,但它没有渲染它,而是以纯文本形式显示 html。

尝试更改 USER_AGENT 值,但没有帮助..

有什么指示吗?

最佳答案

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {                 
        String url = "http://internalserver/path";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", USER_AGENT);
        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);
        //=============================
        response.setContentType(con.getContentType());
        int r=0;PrintWriter out=response.getWriter();
        while((r=con.getInputStream().read())!=-1){out.write(r);}
        //=============================
    }

关于Java Proxy Servlet 在浏览器上显示原始 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32215816/

相关文章:

java - RDD join : After joining two different pair RDDs, 结果RDD键值和顺序发生了变化?

html - Safari 忽略输入复选框的定位和宽度/高度,并在选中后略微移动

javascript - 使用 IF block 后 JSP Servlet 不工作

java - 弹跳球永远不会在地面上保持平静

java - Tesseract/Tess4J 在 Mac OS X 上崩溃:有问题的框架:C [libtesseract.dylib+0xcf72] tesseract::TessResultRenderer::~TessResultRenderer()+0x10

java - 如何在按钮中显示图像?

html - IE8/9 + Quirks 模式不透明度不起作用

html - 图像缩放在光滑 slider 中不起作用

java - 字符串数组空值检查失败

rest - ContainerRequestFilter ContainerResponseFilter 不会被调用