打印 HttpParams / HttpUriRequest 的内容?
我有一个 HttpUriRequest实例,有没有办法打印它包含的所有参数?例如,我几乎可以得到他们这样的信息:
HttpUriRequest req = ...;
HttpParams params = req.getParams();
for (int i = 0; i < params.size(); i++) { // ?
println(params.getParam(i); // ?
}
有没有办法做到这一点?
谢谢
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?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以简单地迭代所有标头字段。
建议的方法 params.toString() 不起作用。
You can simply iterate over all Header fields.
The suggested method params.toString() does not work.
您是在寻找 HTTP 参数还是 URI 参数?
HTTP 参数是用户代理、套接字缓冲区大小、协议版本等。URI
参数是作为请求一部分传递的客户端值,例如,
如果您正在查找 URI 参数,请尝试这样的操作:
但是如果您确实想要原始 HTTP 参数,那就有点复杂了,因为并非所有 HttpParams 的实现都支持
getNames()
。你必须做这样的事情:Are you looking for the HTTP parameters or the URI parameters?
The HTTP parameters are things like user-agent, socket buffer size, protocol version, etc.
The URI parameters are the client values that get passed as part of the request, e.g.
If you're looking for the URI parameters, try something like this:
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:HttpUriRequest 扩展了 HttpMessage,它有 getAllHeaders()、getParams() 等。
看这里: http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/index.html,搜索 HttpMessage
HttpUriRequest extends HttpMessage, which has getAllHeaders(), getParams() etc.
Take a look here: http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/index.html, search for HttpMessage
您可以通过将其转换为 BasicHttpParams 对象来创建解决方法。
BasicHttpParams basicParams = (BasicHttpParams) params;
这使您可以访问
getNames()
,其中包含参数的HashSet
您可以遍历它,打印出参数名称及其值。
看
https://stackoverflow.com/a/57600124/5622596
可能的解决方案
You could create a work around by casting it to BasicHttpParams object.
BasicHttpParams basicParams = (BasicHttpParams) params;
This give you access to
getNames()
which contains aHashSet<String>
of the parametersYou can then iterate through it printing out the parameter name alongside it's value.
See
https://stackoverflow.com/a/57600124/5622596
for possible solution