A Simple Http Server with Java/Socket?

后端 未结 5 1222
耶瑟儿~
耶瑟儿~ 2020-11-29 03:11

I am currently creating a small HTTP server that returns a static page

Hello!

... I tried with sockets with Java:

  public sta         


        
5条回答
  •  一向
    一向 (楼主)
    2020-11-29 03:20

    What machine are you using? What OS? If you're running a UNIX machine, then println won't work because it only sends a LF character. HTTP require CR and LF for its headers. Try adding \r to the end of your strings and see if that works.

    Oh, also, your:

      out.println("HTTP/1.0 200 OK"+
    "Date: Fri, 31 Dec 1999 23:59:59 GMT"+
    "Server: Apache/0.8.4"+
    "Content-Type: text/html"+
    "Content-Length: 59"+
    "Expires: Sat, 01 Jan 2000 00:59:59 GMT"+
    "Last-modified: Fri, 09 Aug 1996 14:21:40 GMT"+
    

    It's printing a single, long string.

    Change those to a println for each string, or add \r\n in to the string.

提交回复
热议问题