commons-httpclient-3.1 中的 URIUtil.encodePath 发生了什么?

发布于 2024-08-28 05:21:50 字数 615 浏览 4 评论 0原文

我想做问题 724043 中描述的操作,即对以下路径组件进行编码一个 URI。建议这样做的类是 URIUtil。不幸的是,该类似乎已从最新版本的 HttpClient 中消失。 HttpClient 4.1 中类似命名的类, URIUtils,不提供相同的功能。这个类/方法是否已转移到我不知道的其他库中,或者它已经消失了?我最好只是将 3.1 版本中的类复制到我的代码中还是有更简单的方法?

I want to do what's described in question 724043, namely encode the path components of a URI. The class recommended to do that is URIUtil from Commons HttpClient 3.1. Unfortunately, that class seems to have disappeared from the most recent version of HttpClient. A similarly named class from HttpClient 4.1, URIUtils, doesn't provide the same functionality. Has this class/method been moved to some other library that I'm not aware of or is it just gone? Am I best off just copying the class from the 3.1 release into my code or is there a simpler way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独留℉清风醉 2024-09-04 05:21:50

该模块的维护者已规定您应该使用标准 JDK URI 类:

URI 和 URIUtils 被标准 Java URI 取代的原因是
很简单:没有人愿意维护这些类。

有许多实用方法可以帮助解决各种问题
java.net.URI 实现存在问题,但标准除外
JRE 类应该足够了,不是吗?

因此,最简单的方法是查看 3.1 版本中的encodePath 源代码,并在您自己的代码中复制它的功能(或者只是将方法/类复制到您的代码库中)。

或者您可以对您提到的问题采用已接受的答案(但似乎您必须首先将 URL 分成几部分):

new URI(
    "http", 
    "search.barnesandnoble.com", 
    "/booksearch/first book.pdf",
    null).toString();

The maintainers of the module have decreed that you should use the standard JDK URI class instead:

The reason URI and URIUtils got replaced with the standard Java URI was
very simple: there was no one willing to maintain those classes.

There is a number of utility methods that help work around various
issues with the java.net.URI implementation but otherwise the standard
JRE classes should be sufficient, should not they?

So, the easiest is to look at the source of encodePath from the 3.1 release and duplicate what it does in your own code (or just copy the method/class into your codebase).

Or you could go with the accepted answer on the question you referred to (but it seems you have to break the URL into parts first):

new URI(
    "http", 
    "search.barnesandnoble.com", 
    "/booksearch/first book.pdf",
    null).toString();
浸婚纱 2024-09-04 05:21:50

这可以使用 httpclient-4.X () 中的 org.apache.http.client.utils.URIBuilder 实用程序来实现,如下所示。

    public static String encodePath(final String path) {
       if(path.length() == 0)
          return "";
       else
          return new URIBuilder().setPath(path).toString();
    }

This can be achieved using org.apache.http.client.utils.URIBuilder utility in httpclient-4.X () as follows.

    public static String encodePath(final String path) {
       if(path.length() == 0)
          return "";
       else
          return new URIBuilder().setPath(path).toString();
    }
寻找我们的幸福 2024-09-04 05:21:50

您可以使用 标准 JDK 函数,例如

public static String encodeURLPathComponent(String path) {
    try {
        return new URI(null, null, path, null).toASCIIString();
    } catch (URISyntaxException e) {
        // do some error handling
    }
    return "";
}

You can use Standard JDK functions, e.g.

public static String encodeURLPathComponent(String path) {
    try {
        return new URI(null, null, path, null).toASCIIString();
    } catch (URISyntaxException e) {
        // do some error handling
    }
    return "";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文