簡體   English   中英

無法將流輸出到瀏覽器

[英]Cannot output the stream to the browser

我剛剛編寫了一個小程序來測試一些東西,如下所示:

public class Main {

    public static void main(String[] args){
        ServerSocket serverSocket = null;
        Socket clientSocket = null;
        DataInputStream dataInputStream= null;

        BufferedWriter bufferedWriter = null;
        String line ;

        try {
            serverSocket = new ServerSocket(80);
            clientSocket = serverSocket.accept();

            dataInputStream = new DataInputStream(clientSocket.getInputStream());

            while((line = dataInputStream.readLine()) != null){
                System.out.println(line);
            }

            bufferedWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
            bufferedWriter.write("HTTP/1.0 200 OK \n Date: Fri, 31 Dec 1999 23:59:59 GMT \n Content-Type: text/html \n Content-Length: 1354 \n <html>abcde<html/>");

            bufferedWriter.flush();

        } catch (IOException e) {
            System.out.println("socket port cannot be opened.");
            e.printStackTrace();
        } finally{
            try {
                serverSocket.close();
                bufferedWriter.close();
            } catch (IOException e) {
                System.out.println("socket port cannot be closed.");
                e.printStackTrace();
            }
        }


    }

}

我從互聯網上找到了http響應格式,應該是正確的。 問題是我的瀏覽器繼續等待響應數據(從旋轉徽標確定),但數據未成功返回。 我犯了什么錯誤?

我通過在瀏覽器中鍵入“localhost”連接到Java程序,我可以在Java程序中打印出請求字符串但只是無法發回響應。

它可能是Content-Length頭字段嗎? 嘗試用以下方法替換響應:

bufferedWriter.write("HTTP/1.0 200 OK \n Date: Fri, 31 Dec 1999 23:59:59 GMT \n Content-Type: text/html \n Content-Length: 18\n <html>abcde<html/>");

(注意Content-Length: 18與原始Content-Length: 1354

我懷疑瀏覽器正在等待您的應用程序發送更多數據。

編輯:這對我有用:

import java.util.*;
import java.io.*;
import java.net.*;

public class Main {

    public static void main(String[] args) throws Exception{
        ServerSocket serverSocket = null;
        Socket clientSocket = null;
        DataInputStream dataInputStream= null;

        BufferedWriter bufferedWriter = null;

        try {
            serverSocket = new ServerSocket(8080);
            clientSocket = serverSocket.accept();

            BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

            while (clientSocket.getInputStream().available() > 0) {
                String line = reader.readLine();

                if (line == null) {
                    break;
                }

                System.out.println(line);
            }

            bufferedWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
            bufferedWriter.write("HTTP/1.0 200 OK \n Date: Fri, 31 Dec 1999 23:59:59 GMT \n Content-Type: text/html \n\n <html>abcde<html/>");
            bufferedWriter.flush();
            clientSocket.close();
        } catch (IOException e) {
            System.out.println("socket port cannot be opened.");
            e.printStackTrace();
        } finally{
            try {
                serverSocket.close();
                bufferedWriter.close();
            } catch (IOException e) {
                System.out.println("socket port cannot be closed.");
                e.printStackTrace();
            }
        }


    }

}

罪魁禍首是while循環。 您調用以獲取更多數據的方法是阻塞,直到該數據可用(這將永遠不會發生)。 我不確定我所做的是否完全必要,但它確實有效。

首先,發送HTTP消息存在一些問題:HTTP消息的每一行都由CR LF分隔/結束,而不僅僅是換行(盡管我懷疑這可能是問題,你應該替換“ \\ n“with”\\ r \\ n“)。 此外,Content-Length不等於消息正文的實際大小,應該替換。 在實際正文之前,所有HTTP消息都應該有一個空行,而您沒有。 最后, <html/>的正斜杠也應該出現在html之前,如下所示: </html>

總結一下:

bufferedWriter.write("HTTP/1.0 200 OK\r\nDate: Fri, 31 Dec 1999 23:59:59 GMT\r\nContent-Type: text/html\r\nContent-Length:18\r\n\r\n<html>abcde</html>");

現在是真正的問題:閱讀循環一直在等待更多的數據。 將readLine()的結果與null進行比較實際上並不能滿足您的需求。 TCP連接產生數據流,您永遠不知道客戶端是否在特定時刻停止發送數據。 相反,您可以閱讀,直到找到一個空行,這標志着HTTP消息的結束。 Web瀏覽器通常不會在GET消息中發送其他內容,因此您不會丟失任何數據。

    BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    while(!(line = reader.readLine()).isEmpty()){
        System.out.println(line);
    }

我看到兩個問題:

內容長度標題表示將跟隨1354個字節。 你只寫20左右。 內容長度是可選的; 如果您沒有使用正確的值,請不要包含它。

在標題的末尾和內容之前需要一個空行: ".... Content-Length: xx\\n\\n<html>..."

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM