2013-03-20 137 views
0

我試圖表現出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"); } 
    } 
} 
} 

發現的問題是,當我鍵入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) 

如何解決這一問題?

+0

我不認爲'HTTP/1.x 404 Not Found'是一個有效的狀態行。也許用'0'或'1'代替'1.x'中的'x'? – 2013-03-20 01:28:44

+0

另外,我認爲一個HTTP響應應該至少有一個標頭,通常是「內容長度」 - 並且正文中的一些內容是一個好主意。 – 2013-03-20 01:29:47

+1

@RichardJPLeGuen JP LEGUEN ON MY SO!!? – 2013-03-20 01:34:11

回答

2

它在我看來像你的HTTP響應無效。

HTTP/1.x 404 Not Found 


您應該:

  • 更改1.x到正確的版本號,比如1.0
  • 添加響應主體
  • 添加至少一個頭 - 也許Content-LengthContent-Type

更多內容像...

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

Not Found 
+0

我得到W白頁與我寫的 – user1673892 2013-03-20 01:59:50

+0

我改變了這部分是 output.writeBytes(「HTTP/1.1 404 Not Found \ r \ n」); output.writeBytes(「Content-Type:text/html \ r \ n \ r \ n」); output.writeBytes(「Content-Length:9」); output.writeBytes(「Content-Type:text/plain」); \t output.writeBytes(「Not Found」); 與同花順,但我得到這個 Content-Length:9Content-Type:text/plainNot Found – user1673892 2013-03-20 02:00:59