繁体   English   中英

HttpClient 4.3.1的org.apache.http.client.utils.URIBuilder中`removeQuery()`和`clearParameters()`有什么区别?

[英]What is the difference between `removeQuery()` and `clearParameters()` in org.apache.http.client.utils.URIBuilder of HttpClient 4.3.1?

在我看来,这两个方法( URIBuilder.removeQueryURIBuilder.clearParameters )完全相同,所以我不确定为什么有两个选择。 在什么情况下我应该使用一个而不是另一个?

我注意到相应的URIBuilder.setQuery方法被标记为已弃用,有利于URIBuilder.setParameters ,但URIBuilder.removeQuery不是。 我错误地认为也许应该是这样吗?

更新: Oleg在dev邮件列表中提供了以下说明:

弃用#setQuery方法的原因是它与该类的所有其他方法的契约不一致。 #setQuery期望输入是URL编码而所有其他方法都期望未转义的输入。 选择是在更改方法的合同和通过这样做打破几乎所有依赖于URIBuilder或方法弃用的应用程序,而选择另一种方法稍微不那么直观的方法。 所以,应该使用[set | clear | add]参数[s]方法用于查询参数,#setCustomQueury方法用于设置自定义查询。

那么,如果你看一下来源,确切的差异非常小:

public URIBuilder removeQuery() {
    this.queryParams = null;
    this.query = null; // <- this is the only difference
    this.encodedQuery = null;
    this.encodedSchemeSpecificPart = null;
    return this;
}

VS

public URIBuilder clearParameters() {
    this.queryParams = null;
    this.encodedQuery = null;
    this.encodedSchemeSpecificPart = null;
    return this;
}

因此, clearParameters将保留您的查询对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM