HttpPost - url和params中的参数作为名称值对

时间:2014-11-25 13:26:10

标签: java http

这有什么不同吗?在编写Web服务方法时,URL方式对我有用,但实体方式返回了一些错误。这两者是否相同?

HttpPost httpPost = new HttpPost("http://example.com/sampleservice?params=paramOne");

VS

HttpPost httpPost = new HttpPost("http://example.com/sampleservice");     
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
            nameValuePair.add(new BasicNameValuePair("params",
                    "ParamOne"));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));

1 个答案:

答案 0 :(得分:0)

HTTP请求存在差异。 在第一个示例中,您使用给定的URL和空体发送HTTP请求。 第二,发送带有较短URL的HTTP请求,但带有参数的主体。

如果服务器上存在不同的行为,则取决于服务器(或它使用的框架)如何处理此类请求。

如果您想在HTTP级别上看到差异,请使用一些工具来检查输入和输出数据包(请求),例如Wireshark的。

相关问题