2017-01-16 32 views
0

時使前查詢參數哈希內容我用uribuilder對象從apache.http.client創建網址如何通過uribuilder創建網址

例如:www.xxx.com /#/路徑/查詢= 123

我的代碼如下

URIBuilder uriBuilder = new URIBuilder(); 
uriBuilder.setScheme("http"); 
uriBuilder.setHost(host); 
uriBuilder.setFragment(path); 
uriBuilder.addParameter(query,123); 

但結果是www.xxx.com/?query=123#path,我怎麼能得到正確的URL如我所料的uribuilder或其他Java工具庫。

回答

1

有效的URI需要符合以下結構:

方案:[// [用戶:密碼@]主機[:端口]] [?查詢] [/]的路徑[#片段]

您嘗試創建的URI看起來像是在單個頁面應用程序中使用的URI。在這種情況下,查詢部分是片段的一部分。

你可以這樣創建:

URIBuilder uriBuilder = new URIBuilder(); 
uriBuilder.setScheme("http"); 
uriBuilder.setHost("www.xxx.com"); 
uriBuilder.setPath("/"); 
uriBuilder.setFragment("/path/?query=123"); 
URI uri = uriBuilder.build();