Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/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 Restlet客户端在请求中设置的cookie在Restlet Servlet端被删除_Java_Servlets_Cookies_Activiti_Restlet 2.0 - Fatal编程技术网

Java Restlet客户端在请求中设置的cookie在Restlet Servlet端被删除

Java Restlet客户端在请求中设置的cookie在Restlet Servlet端被删除,java,servlets,cookies,activiti,restlet-2.0,Java,Servlets,Cookies,Activiti,Restlet 2.0,我有一个测试客户端,它发出Restlet请求,如下所示: 公共类TestRestlet{ public static void main(String[] args) throws IOException { CookieHandler.setDefault(new CookieManager( null, CookiePolicy.ACCEPT_ALL )); ClientResource resource = getClientResource(); Represen

我有一个测试客户端,它发出Restlet请求,如下所示: 公共类TestRestlet{

public static void main(String[] args) throws IOException {
    CookieHandler.setDefault(new CookieManager( null, CookiePolicy.ACCEPT_ALL ));
    ClientResource resource = getClientResource();
    Representation rep = resource.get();
    System.out.println(rep.getText());

}

private static ClientResource getClientResource() {
    String resouceURL = "http://localhost:8080/ActivitiSampleProject/service/process-definitions?suspended=false";
    CookieSetting cookie1 = new CookieSetting("USER", "qdny6HjWY0HONvWoyufBWemrDE+5IcdsssssK0E8UGmu5RKPF7h0BWKvBPSn+Kucb82Aq");
    cookie1.setDomain(".abc.com");
    cookie1.setPath("/");
    cookie1.setMaxAge(-1);
    ClientResource resource = new ClientResource(resouceURL);
    resource.getRequest().getCookies().add(cookie1);
    return resource;
}
}

在服务器端,我想从请求中读取这些cookie,并将它们发送回调用客户端,以便根据cookie获取一些信息

但我无法检索cookies: 我已将调试器设置为org.restlet.ext.servlet.ServerServlet的服务方法,并且请求没有cookie

public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    .......
}
这是在请求中设置cookies的正确方法吗?我做错了什么

通过使用以下代码进行一些点击和试用后,我能够在服务器端检索cookie。Restlet的文档说ClientResource在内部调用客户机。是否有一种方法可以通过设置一些选项来使用ClientResource实现相同的功能?。我想使用ClientResource,因为我计划对大多数代码进行更改,使用ClientResource,而且所有示例源代码都使用ClientResource

public static void main(String[] args) throws IOException {
    Client c = new Client(Protocol.HTTP);
    Request request = new Request(Method.GET,
            "http://localhost:8080/ActivitiSampleProjectNonSpring/service/hello");
    request.getCookies().add("USER", "TESTCOOKIE");

    Response response = c.handle(request);
    Representation rep =  response.getEntity();
    System.out.println(rep.getText());
}

我使用以下Maven配置对Restlet 2.3.1进行了一些测试,您的用例在我这边使用独立的Restlet引擎工作。我使用了你在问题中提供的代码。您可以注意到,您需要显式地将cookies添加到响应中,以便将它们发送回客户端

以下是Maven配置文件:

<project (...)>
    <modelVersion>4.0.0</modelVersion>
    <groupId>stackoverflow</groupId>
    <artifactId>test-restlet-cookies</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java-version>1.7</java-version>
        <restlet-version>2.3.1</restlet-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.restlet.jse</groupId>
            <artifactId>org.restlet</artifactId>
            <version>${restlet-version}</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>maven-restlet</id>
            <name>Public online Restlet repository</name>
            <url>http://maven.restlet.com</url>
        </repository>
    </repositories>
</project>
使用命令
mvnclipse:eclipse
将其转换为eclipsejava项目,或使用命令
mvnpackage
构建相应的war文件。您可以注意到Eclipse项目是WTP兼容的,因此可以连接到Eclipse中的服务器

您可以在此处找到一个示例项目:

希望它能帮助你, 蒂埃里

<project (...)>
    <modelVersion>4.0.0</modelVersion>
    <groupId>stackoverflow</groupId>
    <artifactId>test-restlet-cookies-servlet</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java-version>1.7</java-version>
        <restlet-version>2.3.1</restlet-version>
        <wtp-version>2.0</wtp-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet</artifactId>
            <version>${restlet-version}</version>
        </dependency>

        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet.ext.servlet</artifactId>
            <version>${restlet-version}</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>maven-restlet</id>
            <name>Public online Restlet repository</name>
            <url>http://maven.restlet.com</url>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>install</id>
                        <phase>install</phase>
                        <goals>
                            <goal>sources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <configuration>
                    <wtpapplicationxml>true</wtpapplicationxml>
                    <wtpversion>${wtp-version}</wtpversion>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>