【发布时间】:2010-09-18 00:57:45
【问题描述】:
使用 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")
});
但是,我见过的大多数示例要么将参数直接硬编码到 URL 中,例如:
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 方法打算用于什么?
【问题讨论】:
标签: java httpclient getmethod