URL 添加參數(shù)的幾種方法

當(dāng)你寫一段代碼很糾結(jié)時(shí),不防停下來Google一下,一定有人已經(jīng)解決了你的煩惱!

一、背景

今天PM提出想給一個(gè)URL埋點(diǎn),統(tǒng)計(jì)一下訪問來源,需要我在路徑上加一個(gè)參數(shù)fromPage=shop_detail;本來是一個(gè)很簡單的問題,因?yàn)樵溄痈緵]參數(shù),只只需要在url后面加上?fromPage=shop_detail;可想了一下有安全隱患,萬一url變了,帶了參數(shù)怎么辦?于是想那我就自己判斷是否有參數(shù),再決定加?或/;當(dāng)我下手寫的時(shí)候又糾結(jié)了,為了這么一點(diǎn)東西要寫好幾行代碼呢,不行,還是得先google 一下,就有了如下結(jié)論。

二、 添加parameter的方式

2.1 java.net.URI

通過string 生成URI,再通過URI獲取query,再通過query判斷是添加&或?; 似乎也挺麻煩……


package urItest;
 
import java.net.URI;
import java.net.URISyntaxException;
 
/**
 * Created by fuxiangxu on 2017/1/10.
 */
public class URITest {
    public static URI appendUri(String uri, String appendQuery) throws URISyntaxException {
        URI oldUri = new URI(uri);
 
        String newQuery = oldUri.getQuery();
        if (newQuery == null) {
            newQuery = appendQuery;
        } else {
            newQuery += "&" + appendQuery;
        }
 
        URI newUri = new URI(oldUri.getScheme(), oldUri.getAuthority(),
                oldUri.getPath(), newQuery, oldUri.getFragment());
 
        return newUri;
    }
 
    public static void main(String[] args) throws Exception {
        System.out.println(appendUri("http://example.com", "name=John"));
        System.out.println(appendUri("http://example.com#fragment", "name=John"));
        System.out.println(appendUri("http://example.com?email=john.doe@email.com", "name=John"));
        System.out.println(appendUri("http://example.com?email=john.doe@email.com#fragment", "name=John"));
    }
}

2.2 javax.ws.rs.core.UriBuilder

package urItest;
 
import javax.ws.rs.core.UriBuilder;
 
import java.net.URISyntaxException;
 
/**
 * Created by fuxiangxu on 2017/1/10.
 */
public class URIBuilderTest {
 
    public static void main(String[] args) throws URISyntaxException {
        System.out.println(UriBuilder.fromUri("http://example.com").queryParam("name", "john").build().toString());
        System.out.println(UriBuilder.fromUri("http://example.com?email=john.doe@email.com").queryParam("name", "john").build().toString());
    }
}


2.3 org.apache.http.client.utils.URIBuilder

package urItest;
 
import org.apache.http.client.utils.URIBuilder;
 
import java.net.URISyntaxException;
 
/**
 * Created by fuxiangxu on 2017/1/10.
 */
public class URIBuilderTest {
 
    public static void main(String[] args) throws URISyntaxException {
        System.out.println(new URIBuilder("http://example.com").addParameter("name", "john").build().toString());
        System.out.println(new URIBuilder("http://example.com?email=john.doe@email.com").addParameter("name", "john").build().toString());
    }
}

2.4 org.springframework.web.util.UriComponentsBuilder (建議)


package urItest;
 
import org.springframework.web.util.UriComponentsBuilder;
 
import java.net.URISyntaxException;
 
/**
 * Created by fuxiangxu on 2017/1/10.
 */
public class URIBuilderTest {
 
    public static void main(String[] args) throws URISyntaxException {
        System.out.println(UriComponentsBuilder.fromUriString("http://example.com").queryParam("name", "john").build().toString());
        System.out.println(UriComponentsBuilder.fromUriString("http://example.com?email=john.doe@email.com").queryParam("name", "john").build().toString());
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,628評論 19 139
  • /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home...
    光劍書架上的書閱讀 4,194評論 2 8
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,276評論 6 342
  • 清晨的瀘沽湖在日出的印襯下顯得格外美麗而動(dòng)人,青山配綠水,使景象更為壯觀。 我們迎著美景出發(fā)了。第一站,我們到...
    Tom唐閱讀 924評論 1 6
  • 前段時(shí)間的夜里夢到好多小貓,網(wǎng)上一查說最近有可能生??! 今天在四(2)班,組織背品德科學(xué),結(jié)束后,和申主任一起辦理...
    甲午之印閱讀 257評論 0 0

友情鏈接更多精彩內(nèi)容