Http 如果Url包含保留字符,则Url重定向错误
我正在尝试访问特定的urlHttp 如果Url包含保留字符,则Url重定向错误,http,url,redirect,httpclient,mediawiki,Http,Url,Redirect,Httpclient,Mediawiki,我正在尝试访问特定的url DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials(new AuthScope("abc.com", 443), new UsernamePasswordCredentials("user", "Passwd")); HTTPHelper http = new HTTPHel
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(new AuthScope("abc.com", 443),
new UsernamePasswordCredentials("user", "Passwd"));
HTTPHelper http = new HTTPHelper(httpclient);
http.get("http://abc.com/**aaa**/w/api.php?param=timestamp%7Cuser&format=xml");
其中%7C=|
这会在内部将我重定向到以下url
http://abc.com/**bbb**/w/api.php?param=timestamp%257Cuser&format=xml
正因为如此,我无法得到正确的输出
| ==>%7C
%==> %25
%7C == %257C
我希望查询是时间戳|用户
但由于循环重定向,它被更改为时间戳%7Cuser
有没有办法避免这种情况
我还编写了自己的自定义重定向策略
httpclient.setRedirectStrategy(new DefaultRedirectStrategy() {
public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
boolean isRedirect = false;
try {
isRedirect = super.isRedirected(request, response, context);
LOG.info(response.getStatusLine().getStatusCode());
LOG.info(request.getRequestLine().getUri().replaceAll("%25", "%"));
} catch (ProtocolException e) {
e.printStackTrace();
}
if (!isRedirect) {
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode == 301 || responseCode == 302) {
return true;
}
}
return isRedirect;
}
});
但是我不知道如何用重定向url中的%7C替换%25Chttp://abc.com/**aaa**/w/api.php?param=timestamp | user&format=xml“;
工作
“在内部将我重定向到以下url”是什么意思?就像你的url被再次编码一样。你能在HTTPHelper中发布代码吗?我下面的测试代码工作正常
客户端代码:
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://localhost:8080/test/myservlet?param=timestamp%7Cuser&format=xml");
client.execute(get);
Servlet代码:
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String param = request.getParameter("param"); //get timestamp|user
String format = request.getParameter("format");
}
看起来网站的URL重写规则被破坏了。如果不是您的站点,您可能需要联系其维护人员并将问题告知他们
同时,有什么原因不能简单地使用目标URL(即
http://abc.com/**bbb**/w/api.php?..
)直接,避免重定向?否。它正在抛出IllegalArgumentException主机“abc.com/**aaa**”被永久移动到其他服务器。我从它那里得到301回复,然后它被重定向到“abc.com/**bbb**”。在重定向时,url再次被编码。我试图拦截重定向,并再次在中间进行URL解码。但是不能这样做…感谢IImari的回复…但是我无法控制该网站…我正在根据该网站的信息构建url,该网站为我提供了url**aaa**/。。。。我可以通过编写自己的自定义重定向处理程序来做些什么吗?你可以尝试一下,但老实说,你只需要编写一个kluge来解决特定站点被破坏的特定方式。您的自定义处理程序不太可能对任何其他站点有用(甚至可能会破坏其中一些站点),而且如果被破坏的站点更改其重写规则,也不能保证它会继续工作。因此,考虑到您将实施特定于站点的黑客攻击,为什么不添加一些代码,这样当站点告诉您使用abc.com/**aaa**
,您就可以将其替换为abc.com/**bbb**
?