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

Jav*_*ons 1 java post get http apache-httpclient-4.x

我按照其他SO问题来设置URL的参数,但它给出了错误:

setQueryString(String)类型中的方法HttpMethodBase不适用于参数(NameValuePair[])

无法实例化该类型NameValuePair.

我无法理解实际问题.有人可以帮我吗?

我从上面的问题中使用的代码

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

Nil*_*lsH 12

在HttpClient 4.x中,已经没有GetMethod了.相反,有HttpGet.引用教程中的示例:

查询url中的参数:

HttpGet httpget = new HttpGet(
 "http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=");
Run Code Online (Sandbox Code Playgroud)

以编程方式创建查询字符串:

URIBuilder builder = new URIBuilder();
builder.setScheme("http").setHost("www.google.com").setPath("/search")
    .setParameter("q", "httpclient")
    .setParameter("btnG", "Google Search")
    .setParameter("aq", "f")
    .setParameter("oq", "");
URI uri = builder.build();
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI());
Run Code Online (Sandbox Code Playgroud)