简体   繁体   English

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

[英]Java Proxy Servlet shows raw html on browser

I am using below code 我正在使用下面的代码

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

    }

---- other part of code --- didnt paste here ----代码的其他部分---不在此处粘贴

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

I changed the url directly to the internal site 我直接将网址更改为内部网站

when I access the servlet, it looks like its getting the html from the remote site, but instead of rendering it, it just shows the html in plain text. 当我访问servlet时,它看起来像是从远程站点获取html,但是它没有呈现它,而是仅以纯文本形式显示html。

tried changing USER_AGENT value, didnt help.. 尝试更改USER_AGENT值,没有帮助。

Any pointers ? 有指针吗?

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);}
        //=============================
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM