打印HttpParams / HttpUriRequest的内容? [英] Print contents of HttpParams / HttpUriRequest?
问题描述
我有一个 HttpUriRequest 实例,有没有办法打印它包含的所有参数?例如,我几乎可以得到它们:
I have an HttpUriRequest instance, is there a way to print all the parameters it contains? For example, I can almost get them like:
HttpUriRequest req = ...;
HttpParams params = req.getParams();
for (int i = 0; i < params.size(); i++) { // ?
println(params.getParam(i); // ?
}
有没有办法做到这一点?
Is there a way to do this?
谢谢
推荐答案
是你在寻找HTTP参数或URI参数吗?
Are you looking for the HTTP parameters or the URI parameters?
HTTP参数类似于用户代理,套接字缓冲区大小,协议版本等。
The HTTP parameters are things like user-agent, socket buffer size, protocol version, etc.
URI参数是作为请求的一部分传递的客户端值,例如
The URI parameters are the client values that get passed as part of the request, e.g.
?param1=value1¶m2=value2
如果您正在寻找URI参数,请尝试像这样:
If you're looking for the URI parameters, try something like this:
List<String> uriParams = Arrays.asList(request.getURI().getQuery().split("&"));
但是如果你确实想要原始的HTTP参数,那就复杂一点了因为并非HttpParams的所有实现都支持 getNames()
。你必须做这样的事情:
But if you do want the raw HTTP params, it's a little more complicated because not all implementations of HttpParams supports getNames()
. You'd have to do something like this:
HttpParamsNames params = (HttpParamsNames)request.getParams();
Set<String> names;
if (params instanceof ClientParamsStack) {
names = new HashSet<>();
// Sorted by priority, see ClientParamsStack
ClientParamsStack cps = (ClientParamsStack)params;
if (cps.getApplicationParams() != null) {
names.addAll(((HttpParamsNames)cps.getApplicationParams()).getNames());
}
if (cps.getClientParams() != null) {
names.addAll(((HttpParamsNames)cps.getClientParams()).getNames());
}
if (cps.getRequestParams() != null) {
names.addAll(((HttpParamsNames)cps.getRequestParams()).getNames());
}
if (cps.getOverrideParams() != null) {
names.addAll(((HttpParamsNames)cps.getOverrideParams()).getNames());
}
} else {
names = params.getNames();
}
for (String name : names) {
System.out.println(name + ": " + request.getParams().getParameter(name));
}
这篇关于打印HttpParams / HttpUriRequest的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!