簡體   English   中英

在Restlet Servlet端刪除了Restlet Client在Request中設置的Cookie

[英]Cookies set in Request by Restlet Client removed at Restlet Servlet end

我有一個測試客戶端,它發出如下的Restlet請求:public class 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獲取一些信息。

但是我無法檢索cookie:我在org.restlet.ext.servlet.ServerServlet的服務方法中設置了調試器,並且該請求沒有cookie。

public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    .......
}

這是在請求中設置Cookie的正確方法嗎?

通過使用以下代碼進行一些嘗試后,我能夠在服務器端檢索cookie。 Restlet的文檔說ClientResource內部調用Client。 有沒有一種方法可以通過設置一些選項使用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引擎的情況下可以正常工作。 我使用了您在問題中提供的代碼。 您會注意到,您需要顯式添加cookie來進行響應,以將ml發送回客戶端。

這是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>

使用命令mvn eclipse:eclipse將其轉換為Eclipse Java項目或mvn package以構建相應的jar文件。

您可以在此處找到示例項目: https : //github.com/templth/restlet-stackoverflow/tree/master/restlet/test-restlet-cookies

似乎您使用了Restlet的servlet擴展。 我還對嵌入在servlet容器中的Restlet應用程序進行了測試,它對我有用。 我使用以下Maven文件:

<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>

使用命令mvn eclipse:eclipse將其轉換為Eclipse Java項目或mvn package以構建相應的war文件。 您會注意到Eclipse項目是WTP兼容的,因此您可以將其連接到Eclipse中的服務器。

您可以在此處找到示例項目: https : //github.com/templth/restlet-stackoverflow/tree/master/restlet/test-restlet-cookies-servlet

希望對您有幫助,蒂埃里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM