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