2011-10-02 6 views

답변

3

HTTPClient의 기존 URIUtil을 포함하여 몇 가지 라이브러리를 테스트했지만 실행 가능한 솔루션을 찾지 못했습니다.

/** 
* Tries to construct an url by breaking it up into its smallest elements 
* and encode each component individually using the full URI constructor: 
* 
* foo://example.com:8042/over/there?name=ferret#nose 
* \_/ \______________/\_________/ \_________/ \__/ 
*  |   |   |   |  | 
* scheme  authority  path  query fragment 
*/ 
public URI parseUrl(String s) throws Exception { 
    URL u = new URL(s); 
    return new URI(
     u.getProtocol(), 
     u.getAuthority(), 
     u.getPath(), 
     u.getQuery(), 
     u.getRef()); 
} 

다음 루틴 조합을 사용할 수 있습니다 : 일반적으로, 비록 java.net.URI 구조 이러한 유형의 충분한 성공을 거두었습니다. 디코딩 된 문자열이 변경되지 않을 때까지 URL을 반복적으로 디코딩하며, 이는 예를 들어 더블 인코딩에 유용 할 수 있습니다. 간단하게하기 위해, 참고,이 샘플은 기능하지 않는 안전 장치 등

public String urlDecode(String url, String encoding) throws UnsupportedEncodingException, IllegalArgumentException { 
    String result = URLDecoder.decode(url, encoding); 
    return result.equals(url) ? result : urlDecode(result, encoding); 
} 
1
내가 %로 인코딩 된 URI에 대한 java.net.URLEncoder를 사용하여 무리를 줄 것이다

. 이름에도 불구하고, 그것이 rfc3986 표준을 따르지 않는 URL을 인코딩 크지 않다 대신 application/x-www-form-urlencoded MIME 형식 (read more here) 스칼라에서의 URI 나는 스프레이 HTTP에서 Uri 클래스를 추천 할 것입니다 인코딩

로 인코딩합니다. scala-uri은 대안입니다 (면책 조항 : 저자입니다).

관련 문제