简体   繁体   English

在Java代理服务器中生成HTTP响应

[英]Generate HTTP response in java proxy server

How would I display an HTTP 403 response to the client when the client attempts to access a forbidden page? 当客户端尝试访问禁止页面时,如何显示对客户端的HTTP 403响应? I have the code below, but it doesn't display anything on the clients browser. 我有以下代码,但在客户端浏览器上未显示任何内容。 May be a trivial error, but I'm new to this so anything helps. 可能是一个微不足道的错误,但是我对此并不陌生,所以任何帮助都可以。

                            // If webpage is not allowed, then redirect user to a blocked
            // URL page
            if (!checkHost(fin, requestSplit[3].split("\r")[0])) {
                System.out.println("DON'T SHOW");

                OutputStream to_client = clientSocket.getOutputStream();
                PrintStream ps = new PrintStream(to_client);
                ps.print("HTTP/1.1  403 Forbidden\r\n");
                ps.print("Connection: close\r\n");
                ps.print("\r\n");
                ps.close();
  1. You should close the PrintStream, not the Socket. 您应该关闭PrintStream,而不是Socket.
  2. The line terminator in HTTP is defined as \\r\\n , not whatever println() does, which is platform-dependent. HTTP中的行终止符定义为\\r\\n ,而与平台相关的println()无关。
  3. There's no point in setting a Content-type , a Date , or an Accept-Ranges header in a 404 response. 在404响应中设置Content-typeDateAccept-Ranges标头没有意义。
  4. incomingOS is a terrible name for an output stream. incomingOS是输出流的可怕名称。

Your edit doesn't comply with (2) above. 您的修改不符合上面的(2)。

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

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