所以我有一些使用 Jakarta HttpClient 的 Java 代码,如下所示:
URI aURI = new URI( "http://host/index.php?title=" + title + "&action=edit" );
GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery());
问题是,如果title
包含任何&符号(&),它们被认为是参数分隔符并且请求变得混乱......如果我将它们替换为 URL 转义等效项%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 字符等,因此需要转义其他所有内容。