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

时间:2015-08-25 23:39:48

标签: 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();

    }

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

  

来自:ProxyServlet.java   HTTP:blog.sodhanalibrary.com/2014/05/proxy-servlet-to-forward-requests-to.html

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

当我访问servlet时,看起来它从远程站点获取html,但它不是呈现它,而是以纯文本显示html。

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

任何指针?

0 个答案:

没有答案