2011-10-02 9 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標準、代わりに私はスプレーのhttpからUriクラスをお勧めしますScalaでURIをエンコードするためのapplication/x-www-form-urlencoded MIME形式(read more here

にエンコードに従わないとして名前にもかかわらず、それは、URLを符号化するための素晴らしいではありません。 scala-uriは代わりです(免責事項:私は著者です)。

関連する問題