打印 HttpParams / HttpUriRequest 的内容?

发布于 2024-10-19 18:04:23 字数 460 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花之痕靓丽 2024-10-26 18:04:23

您可以简单地迭代所有标头字段。

HttpUriRequest req = ...;
.......
Header[] headerFields = request.getAllHeaders();
for(int e = 0; e<header.length; e++){                                    
    System.out.println(headerFields[e].getName() + ": " + headerFields[e].getValue());
}

建议的方法 params.toString() 不起作用。

You can simply iterate over all Header fields.

HttpUriRequest req = ...;
.......
Header[] headerFields = request.getAllHeaders();
for(int e = 0; e<header.length; e++){                                    
    System.out.println(headerFields[e].getName() + ": " + headerFields[e].getValue());
}

The suggested method params.toString() does not work.

泅人 2024-10-26 18:04:23

您是在寻找 HTTP 参数还是 URI 参数?

HTTP 参数是用户代理、套接字缓冲区大小、协议版本等。URI

参数是作为请求一部分传递的客户端值,例如,

?param1=value1¶m2=value2

如果您正在查找 URI 参数,请尝试这样的操作:

List<String> uriParams = Arrays.asList(request.getURI().getQuery().split("&"));

但是如果您确实想要原始 HTTP 参数,那就有点复杂了,因为并非所有 HttpParams 的实现都支持 getNames()。你必须做这样的事情:

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));
}

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.

?param1=value1¶m2=value2

If you're looking for the URI parameters, try something like this:

List<String> uriParams = Arrays.asList(request.getURI().getQuery().split("&"));

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));
}
铁憨憨 2024-10-26 18:04:23

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

还在原地等你 2024-10-26 18:04:23

您可以通过将其转换为 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 a HashSet<String> of the parameters

You can then iterate through it printing out the parameter name alongside it's value.

See
https://stackoverflow.com/a/57600124/5622596
for possible solution

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文