【问题标题】:URL inside of another URL (as path)另一个 URL 内的 URL(作为路径)
【发布时间】:2015-11-18 05:47:38
【问题描述】:

我想在 Restlet 中使用以下类型的 URL:http://url.com/http://www.anotherurl.com/path 结果,我想将http://www.anotherurl.com/path 作为参数。 但是它什么也没做。

另外,如果我使用 http://url.com/path ,那么我会毫无问题地收到“路径”。 http://url.com/www.anotherurl.com 给了我www.anotherurl.com。但是http://url.com/www.anotherurl.com/path 是 404。

【问题讨论】:

    标签: java rest url restlet


    【解决方案1】:

    您需要正确编码参数特殊字符。使用URLEncoder 这样做。

    【讨论】:

    • 你说的是服务器端还是客户端?如果服务器,我不明白我应该做什么,最重要的是为什么..
    • 客户端。在服务器上,您必须获取 URL 的路径部分,并使用 URLDecoder 进行类似的解码。
    【解决方案2】:

    其实这里有两部分。

    • 使用Reference 类构建 URL:

      Reference ref = new Reference();
      ref.setScheme("http");
      ref.setHostDomain("localhost");
      ref.setHostPort(8182);
      ref.addSegment("test");
      ref.addSegment("http://test");
      
      ClientResource cr = new ClientResource(ref);
      cr.get();
      
    • 获取值作为路径参数并对其进行解码。下面是应用类中的路由配置:

      public Restlet createInboundRoot() {
          Router router = new Router(getContext());
          router.attach("/test/{id}", MyServerResource.class);
          return router;
      }
      

      以及服务器资源中对应的代码:

      public class MyServerResource extends ServerResource {
          @Get
          public Representation get() {
              String id = getAttribute("id");
              // Prints http%3A%2F%2Ftest
              System.out.println("id = "+id);
              // Prints http://test
              System.out.println("id = "+Reference.decode(id));
              return new EmptyRepresentation();
          }
      }
      

    希望对你有帮助 蒂埃里

    【讨论】:

    • 它没有帮助,404 错误。这是日志(我在 Apache Tomcat 上运行):Dec 02, 2015 4:38:24 AM org.restlet.engine.log.LogFilter afterHandle INFO: 2015-12-02 04:38:24 127.0.0.1 - 127.0.0.1 8080 GET /test/http://www.com - 404 439 0 20 http://localhost:8080 curl/7.35.0 - 但没有 http:// 工作正常:id = www.com id = www.com Dec 02, 2015 4:36:52 AM org.restlet.engine.log.LogFilter afterHandle INFO: 2015-12-02 04:36:52 127.0.0.1 - 127.0.0.1 8080 GET /test/www.com - 204 0 0 89 http://localhost:8080 curl/7.35.0 -
    • 嗨。我真的很惊讶,因为我让它与 2.3.1 版本一起工作。我刚刚在 github repo (github.com/templth/restlet-stackoverflow/tree/master/restlet/…) 中提交了我的项目,所以你可以尝试一下......
    猜你喜欢
    • 2019-06-11
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 2016-12-08
    • 2015-09-01
    • 2012-03-09
    相关资源
    最近更新 更多