Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java通过流设置cookies_Java_Sockets_Cookies - Fatal编程技术网

Java通过流设置cookies

Java通过流设置cookies,java,sockets,cookies,Java,Sockets,Cookies,我正在编写一个web服务器,需要在客户端的web浏览器上设置cookie。 我通过Socket.getOutputStream获得一个OutputStream,但是我找不到一种方法来发送一个头。我尝试发送javascript代码来在客户端设置cookie,但这样设置的cookie不会被发送回服务器。 如何通过标题/其他方式设置cookie。您将响应标题发送到正文的正前方: PrintWriter writer = new PrintWriter(socket.getOutputStream())

我正在编写一个web服务器,需要在客户端的web浏览器上设置cookie。 我通过Socket.getOutputStream获得一个OutputStream,但是我找不到一种方法来发送一个头。我尝试发送javascript代码来在客户端设置cookie,但这样设置的cookie不会被发送回服务器。
如何通过标题/其他方式设置cookie。

您将响应标题发送到正文的正前方:

PrintWriter writer = new PrintWriter(socket.getOutputStream());
writer.println("HTTP/1.0 200 Ok");
writer.println("Set-Cookie key=value Max-Age=8640");
writer.println("Content-Type text/html");
writer.println()//important
//send your body here
writer.flush();//send message
下面是HTTP响应头字段列表

请阅读帮助,尤其是,然后回来问一个可以回答的问题。