gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:47:27 25 4
gpt4 key购买 nike

我关注了this other SO question为 URL 设置参数但出现错误:

The method setQueryString(String) in the type HttpMethodBase is not applicable for the arguments (NameValuePair[])

Cannot instantiate the type NameValuePair.

我无法理解实际问题。有人可以帮我解决这个问题吗?

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

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

最佳答案

在 HttpClient 4.x 中,不再有 GetMethod。取而代之的是 HttpGet。引用 tutorial 中的示例:

url中的查询参数:

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

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

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

关于java - 将查询参数添加到 GetMethod(使用 Java commons-httpclient)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16230973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com