restlet get没有发送完成确认

时间:2013-06-11 00:13:26

标签: http rest get restlet restlet-2.0

您好我有一个简单的restlet get方法,它返回一个静态字符串。它看起来如下:

@Get
    public String represent() {
        return "mystring\r\n";
    }

低级别的c应用程序通过进入读取循环来调用此get。它从未收到完成确认信号,表示没有更多数据可供读取,并且在20秒后超时。是否需要发送代码以提醒客户端应用程序不再有数据?或者说得到了结束?

1 个答案:

答案 0 :(得分:1)

[注意:下面编写的代码部分基于http://www.restlet.org]

上提供的样本

HTTP 1.0和1.1有一个名为Content-Length的标头。无论该标头的数值是什么,都是HTTP响应主体的长度。在HTTP 1.1中更进一步,还有另一个头名称值Tranfer-Encoding: chunked,表示响应主体被分成部分(块),并且每个部分的长度都在在部分交付之前的行。(我没有包括Transfer-Encoding的其他值来保持这个答案的简洁。)

如果这是我的重定时服务器:

package restletapp;

import org.restlet.Component;
import org.restlet.data.Protocol;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

public class RestletApp extends ServerResource {

    public static void main(String[] args) throws Exception {
        Component component = new Component();
        component.getServers().add(Protocol.HTTP, 8182);
        component.getDefaultHost().attach("/trace", RestletApp.class);
        component.start();
    }

    @Get
    public String toAtGet() {
        return  "Resource URI  : " + getReference() + '\n'
              + "Root URI      : " + getRootRef() + '\n'
              + "Routed part   : " + getReference().getBaseRef() + '\n'
              + "Remaining part: " + getReference().getRemainingPart()
                ;
    }

}

这是我的客户端(使用Java中的套接字编写。只需发送一个最小的HTTP请求,并在控制台上打印响应的每个字符。)

package restletapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class Requester {
    public static void main(String[] args) throws UnknownHostException, IOException {
        Socket s=new Socket("localhost", 8182);
        OutputStream os = s.getOutputStream();
        os.write((
                  "GET /trace HTTP/1.1\r\n" //request
                + "host: localhost:8182\r\n" //request
                + "Connection-type: close\r\n\r\n" //request
                ).getBytes());
        InputStream is = s.getInputStream();
        for(int ch;(ch=is.read())!=-1;System.out.flush())
            System.out.write(ch); //response, one char at a time.
        is.close();
        os.close();
        s.close();
    }
}

客户端流程永远不会结束。但是,如果我将我的客户端程序更改为:

package restletapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class Requester {
    public static void main(String[] args) throws UnknownHostException, IOException {
        Socket s=new Socket("localhost", 8182);
        OutputStream os = s.getOutputStream();
        os.write((
                  "GET /trace HTTP/1.1\r\n"
                + "host: localhost:8182\r\n"
                + "Connection-type: close\r\n\r\n"
                ).getBytes());
        InputStream is = s.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        int bytesRead=0;
        int contentLength=0;
        //response headers.
        for(String line;!(line=br.readLine()).isEmpty();System.out.flush()){
            System.out.println(line);
            String[] tokens = line.split(":| ");
            if(tokens[0].equalsIgnoreCase("content-length")){
                contentLength=Integer.parseInt(tokens[2]);
            }
        }
        //response separator, between headers and body.
        System.out.println();
        //response body.
        while(bytesRead<contentLength){
            System.out.write(br.read());
            System.out.flush();
            bytesRead++;
        }
        is.close();
        os.close();
        s.close();
    }
}

Requester的第二个版本中,您可以看到当响应正文的content-length - 读取字符数时,客户端已关闭连接。

这是我使用 curl

command line $ curl -i "http://localhost:8182/trace"
HTTP/1.1 200 OK
Date: Fri, 14 Jun 2013 11:54:32 GMT
Server: Restlet-Framework/2.0.15
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Content-Length: 148
Content-Type: text/plain; charset=UTF-8

Resource URI  : http://localhost:8182/trace
Root URI      : http://localhost:8182/trace
Routed part   : http://localhost:8182/trace
Remaining part:
command line $ 

您可以看到, curl 在阅读完内容后退出。