2013-07-30 36 views
0

我有以下得到:如何更改restlets中的標準標題值?

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

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

從調用該服務的響應:

CFG - HTTP/1.1 200 OK 接受-範圍:字節 的Content-Type:text/plain的;字符集= UTF-8 日期:2013年7月29日星期一19:59:37 GMT 服務器:Restlet-Framework/2.0.9 不同:Accept-Charset,Accept-Encoding,Accept-Language,Accept Content-Length:118 連接:保持活躍

如何更改連接標題值以關閉?

我認爲這可能是一個restlet錯誤。

回答

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上條命令行(僅使用遠程登錄): [請注意,請求頭中的最後一行中的每個請求接着2個換行符] [爲了避免任何混亂,一些請求不包含請求體。]

[[email protected]]# 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. 
[[email protected]]# 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. 
[[email protected]]# 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. 
[[email protected]]# 

在上述例子中,幾種類型的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報頭。 可以顯示消息之後,會顯示遠程登錄退出:Connection closed by foreign host.

有在上述給出的實施例2多個樣品的請求 - 響應:

1.An 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.A沒有HTTP版本請求中提到:

GET /TestRestlet 

和響應是:(無標題)

Sample Response Text 

和具有消息的遠程登錄退出:Connection closed by foreign host.

結論:

無論是客戶機/客戶機程序,使它發送一個HTTP-1.0請求,或與HTTP-1.1請求Connection: close標題。

在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(); 

可以幫助您斷開連接。