使用Apache的commons-httpclient for Java,向GetMethod实例添加查询参数的最好方法是什么?如果我使用PostMethod,它是非常简单:
PostMethod method = new PostMethod();
method.addParameter("key", "value");
GetMethod没有“addParameter”方法,但。我发现这个工作:
GetMethod method = new GetMethod("http://www.example.com/page");
method.setQueryString(new NameValuePair[] {
new NameValuePair("key", "value")
});
但是,我看到的大多数示例都是将参数直接硬编码到网址中,例如:
GetMethod method = new GetMethod("http://www.example.com/page?key=value");
或硬编码查询字符串,例如:
GetMethod method = new GetMethod("http://www.example.com/page");
method.setQueryString("?key=value");
这些模式之一是首选吗?为什么PostMethod和GetMethod之间的API差异?和所有那些其他HttpMethodParams方法打算用于?