如何将查询参数添加到 GetMethod(使用 Java commons-httpclient)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/217070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:31:51  来源:igfitidea点击:

How do I add query parameters to a GetMethod (using Java commons-httpclient)?

javahttpclientgetmethod

提问by Ross

Using Apache's commons-httpclient for Java, what's the best way to add query parameters to a GetMethod instance? If I'm using PostMethod, it's very straightforward:

使用 Apache 的 commons-httpclient for Java,将查询参数添加到 GetMethod 实例的最佳方法是什么?如果我使用 PostMethod,它非常简单:

PostMethod method = new PostMethod();
method.addParameter("key", "value");

GetMethod doesn't have an "addParameter" method, though. I've discovered that this works:

但是,GetMethod 没有“addParameter”方法。我发现这有效:

GetMethod method = new GetMethod("http://www.example.com/page");
method.setQueryString(new NameValuePair[] {
    new NameValuePair("key", "value")
});

However, most of the examples I've seen either hard-code the parameters directly into the URL, e.g.:

但是,我见过的大多数示例都将参数直接硬编码到 URL 中,例如:

GetMethod method = new GetMethod("http://www.example.com/page?key=value");

or hard-code the query string, e.g.:

或硬编码查询字符串,例如:

GetMethod method = new GetMethod("http://www.example.com/page");
method.setQueryString("?key=value");

Is one of these patterns to be preferred? And why the API discrepancy between PostMethod and GetMethod? And what are all those other HttpMethodParams methods intended to be used for?

这些模式之一是首选吗?为什么 PostMethod 和 GetMethod 之间的 API 存在差异?那些其他的 HttpMethodParams 方法是用来做什么的?

采纳答案by Ryan Guest

Post methods have post parameters, but get methods do not.

Post 方法有 post 参数,但get 方法没有

Query parameters are embedded in the URL. The current version of HttpClient accepts a string in the constructor. If you wanted to add the key, value pair above, you could use:

查询参数嵌入在 URL 中。当前版本的 HttpClient 在构造函数中接受一个字符串。如果你想添加上面的键值对,你可以使用:

String url = "http://www.example.com/page?key=value";
GetMethod method = new GetMethod(url);

A good starting tutorial can be found on the Apache Jakarta Commons page.

可以在Apache Jakarta Commons 页面上找到一个很好的入门教程。

Update: As suggested in the comment, NameValuePair works.

更新:正如评论中所建议的,NameValuePair 有效。

GetMethod method = new GetMethod("example.com/page"); 
method.setQueryString(new NameValuePair[] { 
    new NameValuePair("key", "value") 
}); 

回答by Steve Jones

It's not just a matter of personal preference. The pertinent issue here is URL-encoding your parameter values, so that the values won't get corrupted or misinterpreted as extra delimiters, etc.

这不仅仅是个人喜好的问题。这里的相关问题是对参数值进行 URL 编码,这样这些值就不会被破坏或被误解为额外的分隔符等。

As always, it is best to read the API documentation in detail: HttpClient API Documentation

一如既往,最好详细阅读 API 文档: HttpClient API 文档

Reading this, you can see that setQueryString(String)will NOT URL-encode or delimit your parameters & values, whereas setQueryString(NameValuePair[])will automatically URL-encode and delimit your parameter names and values. This is the best method whenever you are using dynamic data, because it might contain ampersands, equal signs, etc.

阅读本文,您可以看到它setQueryString(String)不会对您的参数和值进行 URL 编码或分隔,而setQueryString(NameValuePair[])会自动对您的参数名称和值进行 URL 编码和分隔。无论何时使用动态数据,这都是最好的方法,因为它可能包含与号、等号等。

回答by Randal Harleigh

Try it this way:

试试这个方法:

    URIBuilder builder = new URIBuilder("https://graph.facebook.com/oauth/access_token")
            .addParameter("client_id", application.getKey())
            .addParameter("client_secret", application.getSecret())
            .addParameter("redirect_uri", callbackURL)
            .addParameter("code", code);

    HttpPost method = new HttpPost(builder.build());