2013-03-14 120 views
0

沒有數據,我創建了一個使用HTTP協議通過端口4567接受連接和簡單的Java服務器(至少目前如此)發送到瀏覽器中的硬編碼響應:從自己的HTTP服務器發送到瀏覽器

public class Connection extends Thread { 
    private boolean stop; 
    private Socket socket; 
    private BufferedWriter output; 
    private BufferedReader input; 

    public Connection(Socket socket) { 
     try { 
      this.socket = socket; 
      output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); 
      input = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
     } catch (IOException ex) { 
      System.out.println("Could not create input/output readers: " + ex); 
     } 
    } 

    @Override 
    public void run() { 
     //while (!stop) { 
      try { 
       String data = "{'success': 'yes'}".replaceAll("'", "\""); 
       String read; 

       // printing request on screen; it's ignored 
       while (!(read = input.readLine()).equals("")) { 
        System.out.println(read); 
       } 

       // Last empty line send by browser won't be printed 
       System.out.println(); 

       // Writing hardcoded response 
       writeln("HTTP/1.1 200 OK"); 
       writeln("Content-Type: application/json; charset=utf-8"); 
       writeln("Content-Length: " + data.length()); 
       writeln(); 
       writeln(data); 
       output.flush(); 
       input.close(); 
       output.close(); 
      } catch (IOException ex) { 
       System.out.println("IO Error: " + ex); 
      } 
     //} 
    }  

    private void writeln(String... lines) throws IOException {   
     if (lines.length == 0) { 
      System.out.println("Writing:"); 
      output.write("\n"); 
     } 

     for (String line : lines) { 
      System.out.println("Writing: " + line); 
      output.write(line + '\n'); 
     } 
    } 

    public void setStop(boolean stop) { 
     this.stop = stop; 
    } 
} 

連接實例在另一個線程創建的,它只是new Connection(serversocket.accept()).start();

在瀏覽器端,有按照代碼發送Ajax請求:

$(document).ready(function() { 
    $.ajax({ 
     url: "http://192.168.1.212:4567", 
     crossDomain: true, 
     success: function(data) { 
      console.log("Success: " + data); 
     }, 
     error: function (xhr, ajaxOptions, thrownError) { 
      console.log("Text: " + xhr.responseText); 
      console.log("Status: " + xhr.status); 
      console.log("Error: " + thrownError); 
     } 
    }); 
}); 

釷在什麼樣的瀏覽器說:

enter image description here

而這正是服務器說:

GET/HTTP/1.1 
Host: 192.168.1.212:4567 
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.4) Gecko/20120425 Firefox/10.0.4 
Accept: */* 
Accept-Language: pl,en-us;q=0.7,en;q=0.3 
Accept-Encoding: gzip, deflate 
Connection: keep-alive 
Referer: http://localhost:8383/Client/index.html 
Origin: http://localhost:8383 

Writing: HTTP/1.1 200 OK 
Writing: Content-Type: application/json; charset=utf-8 
Writing: Content-Length: 18 
Writing: 
Writing: {"success": "yes"} 

我知道,有跨域請求,這是我的項目中非常重要的一部分。 發送所有標題,但沒有數據作爲迴應。

回答

0

你粘貼爲「什麼服務器說」是一個正常的瀏覽器請求,而不是響應。 最小響應看起來是這樣的:

HTTP/1.0 200 OK 
Content-Type: text/html; charset=utf-8 
Cache-Control: no-cache 
Date: Sun, 01 Mar 2012 00:00:00 GMT 
Server: IAMME 
Content-Length: 14444 
Connection: Close 

some content here... 

我也能看到你沒有發送任何狀態(爲「0」)..看到上面塊的第一線。

+0

我的服務器輸出瀏覽器的請求,然後它向瀏覽器發送什麼(「Writing:[..]」)。我會嘗試添加一些響應字段。 – 2013-03-14 08:34:28

+0

你應該在嘗試寫這樣的東西之前閱讀HTTP rfc :) – 2013-03-14 08:38:06

相關問題