java - 显示浏览器请求中未找到的页面

标签 java http dataoutputstream

我正在尝试显示未从服务器找到的 404 页面

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class ex1 {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    final int BUFFER_SIZE=1000;
    char[] buffer=new char[BUFFER_SIZE];
    ServerSocket serverSocket = null; 
    try {
        serverSocket = new ServerSocket(16000); 
    } catch (IOException e) {
        System.out.println("Could not listen on port: 16000");
        System.exit(-1); 
    }
    while (true) {
        Socket clientSocket = null; 
        try {
            System.out.println("Waiting for Connection"); 
            clientSocket = serverSocket.accept(); 
            System.out.println("Connection Accepted");
            DataOutputStream output =new DataOutputStream(clientSocket.getOutputStream());
            output.writeBytes("HTTP/1.1 404 Not Found \r\n");
            output.writeBytes("Content-Type: text/html \r\n\r\n");


            output.flush();
        } catch (IOException e) {
            System.out.println("Failed to accept connection");
            System.exit(-1); 
        }
        try {
            BufferedReader in =
            new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            in.read(buffer);
            System.out.println(buffer);
            in.close();
            clientSocket.close(); 
            System.out.println("Connection terminated");
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Problem in communicating with the client"); }
    }
}
}

问题是当我在浏览器(google chrome)中输入 localhost:16000 时,我得到“哎呀!此链接似乎已损坏。” 不是第 404 页

输出为:

Waiting for Connection
Connection Accepted
java.net.SocketException: Connection reset
Problem in communicating with the client
Waiting for Connection
Connection Accepted
GET / HTTP/1.1
Host: localhost:16000
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3


Connection terminated
Waiting for Connection
Failed to accept connection
    at java.net.SocketInputStream.read(SocketInputStream.java:189)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.BufferedReader.fill(BufferedReader.java:154)
    at java.io.BufferedReader.read1(BufferedReader.java:205)
    at java.io.BufferedReader.read(BufferedReader.java:279)
    at java.io.Reader.read(Reader.java:140)
    at ex1_Getting_your_hands_dirty.ex1.main(ex1.java:55)

如何解决这个问题?

最佳答案

在我看来,您的 HTTP 响应无效。

    HTTP/1.x 404 Not Found


你应该:

  • 1.x 更改为正确的版本号,例如 1.0
  • 添加响应正文
  • 添加至少一个 header - 可能是 Content-LengthContent-Type

更像是......

HTTP/1.1 404 Not Found
Content-Length: 9
Content-Type: text/plain

Not Found

关于java - 显示浏览器请求中未找到的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15513670/

相关文章:

java - SAP JAVA 包安装问题

java - 如何在 MacOS 上嗅探 Java 小程序 HTTP 流量?

Java 基本数据类型 byte 和 Byte 类

java - 如何保持写入文件的二进制数据的一致性?

java - 我们可以将Java中字符串中开头和结尾有符号的部分替换为Identify吗?

java - Golang 中的拆分器

java - TreeMap 内存使用

.net - 有没有办法从 WebException 中获取原始 HTTP 响应字符串?

php - ESP32 尝试使用 HTTPClient 将图像文件发送到 php

java - 向服务器发送 POST 请求