【问题标题】:How to get restlet to follow 301 redirects如何让 restlet 遵循 301 重定向
【发布时间】:2014-02-25 04:31:43
【问题描述】:

我目前正在尝试获取可能位于多个重定向之后的资源。我的代码如下所示:

        Client client = new Client(new Context(), Protocol.HTTP);
        Context ctx = client.getContext();
        ctx.getParameters().add("followRedirects", "true");      
        ClientResource cr = new ClientResource(ctx,u); 
        Representation r = cr.get();

但是不遵循重定向。我做错了什么?

这是 Google App Engine 上的 Restlet 2.1

【问题讨论】:

    标签: java google-app-engine restlet http-redirect


    【解决方案1】:
    Series<Parameter> params = ctx.getParameters();
    params.add("followRedirects", "true");
    ctx.setParameters(params);
    

    【讨论】:

    • 谢谢,这行得通,虽然第一行有一个小错字,应该修正以使其成为完美的答案。
    【解决方案2】:

    您也可以使用ClientResource 类进行设置:

    clientResource.setFollowingRedirects(true);
    

    就我而言(使用 Restlet 2.3.9),这似乎是默认行为。

    此方法在您的情况下也很有用:

    setMaxRedirects(int maxRedirects) 
    

    设置单个呼叫可以自动跟随的最大重定向次数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      • 2015-03-01
      • 2017-07-17
      相关资源
      最近更新 更多