代码之家  ›  专栏  ›  技术社区  ›  lambshaanxy

HttpClient请求的URL中的转义符号

  •  5
  • lambshaanxy  · 技术社区  · 15 年前

    我有一些Java代码使用Jakarta HttpClient,如下所示:

    URI aURI = new URI( "http://host/index.php?title=" + title + "&action=edit" );
    GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery());
    

    title %26 ,然后通过getEscapedPathQuery()将其双转义到 %2526 .

    我目前正在解决这个问题,基本上是在之后修复损坏:

    URI aURI = new URI( "http://host/index.php?title=" + title.replace("&", "%26") + "&action=edit" );
    GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery().replace("%2526", "%26"));
    

    但是在那里 做这件事的好方法,对吗?注意,标题可以包含任意数量的不可预知的UTF-8字符等,因此转义其他所有字符是必需的。

    3 回复  |  直到 13 年前
        1
  •  14
  •   Strelok    15 年前

    干得好:

    import java.net.URLEncoder;
    ...
    ...
    URI aURI = new URI( "http://host/index.php?title=" + URLEncoder.encode(title,"UTF-8") + "&action=edit" );
    GetMethod aRequest = new GetMethod( aURI.getPathQuery());
    

    检查 java.net.URLEncoder 更多信息。

        2
  •  1
  •   Julian Reschke    15 年前

    如果不想转义,为什么要调用getEscapedPathQuery()?只要决定谁的责任并保持一致。

        3
  •  0
  •   Jacob Schoen    12 年前

    使用 URLEncoder 班级。

    用于HTML表单编码的实用程序类。此类包含静态 将字符串转换为 应用程序/x-www-form-urlencoded MIME格式。了解更多信息