How do I add query parameters to a GetMethod (using Java commons-httpclient)?

前端 未结 3 766
傲寒
傲寒 2020-12-08 19:14

Using Apache\'s commons-httpclient for Java, what\'s the best way to add query parameters to a GetMethod instance? If I\'m using PostMethod, it\'s very straightforward:

3条回答
  •  悲哀的现实
    2020-12-08 20:08

    It's not just a matter of personal preference. The pertinent issue here is URL-encoding your parameter values, so that the values won't get corrupted or misinterpreted as extra delimiters, etc.

    As always, it is best to read the API documentation in detail: HttpClient API Documentation

    Reading this, you can see that setQueryString(String) will NOT URL-encode or delimit your parameters & values, whereas setQueryString(NameValuePair[]) will automatically URL-encode and delimit your parameter names and values. This is the best method whenever you are using dynamic data, because it might contain ampersands, equal signs, etc.

提交回复
热议问题