0

我有以下得到:

@Get
public String represent(Variant variant) throws ResourceException
{
    String text = "returntext";

    text+="\r\n";
    return text;
}

调用此服务的响应:

CFG - HTTP/1.1 200 OK Accept-Ranges: bytes Content-Type: text/plain;charset=UTF-8 Date: Mon, 29 Jul 2013 19:59:37 GMT 服务器:Restlet-Framework/2.0.9 变化:接受-Charset、Accept-Encoding、Accept-Language、Accept Content-Length: 118 Connection: keep-alive

如何将连接标头值更改为 close ?

我认为这可能是一个restlet错误。

4

1 回答 1

0

服务器是否关闭连接取决于客户端请求是否要求关闭连接。

这是一个示例服务器代码:

import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Parameter;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
import org.restlet.util.Series;

public class TestRestlet extends ServerResource {
    @Get
    public String getImpl(){
        return "Sample Response Text\r\n";
    }
}

这是我在 linux 命令行上得到的(仅使用 telnet):[请注意,每个请求的 request-header 中的最后一行后跟 2 个换行符] [为避免混淆,一些请求不包含 request -身体。]

[root@mylinuxserver]# telnet 172.16.101.34 6060
Trying 172.16.101.34...
Connected to win7comp01 (172.16.101.34).
Escape character is '^]'.
GET /TestRestlet HTTP/1.1
Host: 172.16.101.34:6060

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=C2E77F4D0437E525A0FC66498EF09E8B; Path=/hotelSoft
Date: Wed, 31 Jul 2013 08:25:44 GMT
Accept-Ranges: bytes
Server: Restlet-Framework/2.0.15
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Content-Type: application/json;charset=UTF-8
Content-Length: 22

Sample Response Text

GET /TestRestlet HTTP/1.1
Host: 172.16.101.34:6060
Connection: Keep-Alive

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=1873DE26443F5DF62379B895AEA0F004; Path=/hotelSoft
Date: Wed, 31 Jul 2013 08:25:48 GMT
Accept-Ranges: bytes
Server: Restlet-Framework/2.0.15
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Content-Type: application/json;charset=UTF-8
Content-Length: 22

Sample Response Text

GET /TestRestlet HTTP/1.1
Host: 172.16.101.34:6060
Connection: close

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=43EC7C9AACC6C0CEF6FAC8F608B1D79C; Path=/hotelSoft
Date: Wed, 31 Jul 2013 08:25:57 GMT
Accept-Ranges: bytes
Server: Restlet-Framework/2.0.15
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Content-Type: application/json;charset=UTF-8
Content-Length: 22
Connection: close

Sample Response Text
Connection closed by foreign host.
[root@mylinuxserver]# telnet 172.16.101.34 6060
Trying 172.16.101.34...
Connected to win7comp01 (172.16.101.34).
Escape character is '^]'.
GET /TestRestlet HTTP/1.0

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=2C879A91F2501DD9D3B39EF50C3F46CA; Path=/hotelSoft
Date: Wed, 31 Jul 2013 08:26:09 GMT
Accept-Ranges: bytes
Server: Restlet-Framework/2.0.15
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Content-Type: application/json;charset=UTF-8
Content-Length: 22
Connection: close

Sample Response Text
Connection closed by foreign host.
[root@mylinuxserver]# telnet 172.16.101.34 6060
Trying 172.16.101.34...
Connected to win7comp01 (172.16.101.34).
Escape character is '^]'.
GET /TestRestlet

Sample Response Text
Connection closed by foreign host.
[root@mylinuxserver]#

在上面的示例中,建立了几种类型的 HTTP 连接。

对第一个请求的响应:

GET /TestRestlet HTTP/1.1
Host: 172.16.101.34:6060

[注意:该行Host: 172.16.101.34:6060后面是 2 \r\n: \r\n\r\n]

是:

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=C2E77F4D0437E525A0FC66498EF09E8B; Path=/hotelSoft
Date: Wed, 31 Jul 2013 08:25:44 GMT
Accept-Ranges: bytes
Server: Restlet-Framework/2.0.15
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Content-Type: application/json;charset=UTF-8
Content-Length: 22

Sample Response Text

连接还没有关闭,我们发送另一个请求:

GET /TestRestlet HTTP/1.1
Host: 172.16.101.34:6060
Connection: Keep-Alive

得到响应:

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=1873DE26443F5DF62379B895AEA0F004; Path=/hotelSoft
Date: Wed, 31 Jul 2013 08:25:48 GMT
Accept-Ranges: bytes
Server: Restlet-Framework/2.0.15
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Content-Type: application/json;charset=UTF-8
Content-Length: 22

仍然没有关闭连接。

但是在第三个请求中:

GET /TestRestlet HTTP/1.1
Host: 172.16.101.34:6060
Connection: close

连接已关闭,因为请求包含Connection: close标头。显示消息后,您可以看到 telnet 退出:Connection closed by foreign host.

在上面给出的示例中还有 2 个示例请求响应:

1.HTTP 1.0 请求:

GET /TestRestlet HTTP/1.0

回应:

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=2C879A91F2501DD9D3B39EF50C3F46CA; Path=/hotelSoft
Date: Wed, 31 Jul 2013 08:26:09 GMT
Accept-Ranges: bytes
Server: Restlet-Framework/2.0.15
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Content-Type: application/json;charset=UTF-8
Content-Length: 22
Connection: close

Sample Response Text

telnet 显示后退出:Connection closed by foreign host.

2.没有提到HTTP版本的请求:

GET /TestRestlet

响应是:(没有标题)

Sample Response Text

telnet 退出并显示一条消息: Connection closed by foreign host.

结论:

无论您的客户端/客户端程序是什么,都让它发送一个 HTTP-1.0 请求或带有Connection: close标头的 HTTP-1.1 请求。

在 Java 中,您可以通过以下方式实现:

import java.net.HttpURLConnection;
import java.net.URL;
.
.
.
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("http://....").openConnection();
httpURLConnection.setRequestProperty("Connection","close");
// rest of the code here....

还要检查是否有这样的语句:

httpURLConnection.disconnect();

可以帮助您断开连接。

于 2013-07-31T09:24:59.333 回答