Creating a UrlEncodedFormEntity from a List of NameValuePairs throws a NullPointerException

后端 未结 2 1648
不知归路
不知归路 2021-02-05 06:39

I\'m creating a unit test to try out the servlet I just created.

@Test
public void test() throws ParseException, IOException {

  HttpClient client = new Defaul         


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-02-05 07:20

    Not a stupid question at all. I think the confusion is that in httpclient 4.1, no encoding format was required- This worked:

    HttpEntity entity = new UrlEncodedFormEntity(params);
    method.setEntity(entity);
    

    When I changed the dependency to httpclient 4.2 in order to access URIBuilder, I got:

    java.lang.NullPointerException
    at org.apache.http.entity.StringEntity.(StringEntity.java:70)
    at org.apache.http.client.entity.UrlEncodedFormEntity.(UrlEncodedFormEntity.java:78)
    at org.apache.http.client.entity.UrlEncodedFormEntity.(UrlEncodedFormEntity.java:92)...
    

    With 4.2, it seems the constructor requires the encoding, as you noted. Confusingly, the doc specifies that the old constructor is still available, but it doesn't seem to work anymore.

    public UrlEncodedFormEntity(List parameters) doc

提交回复
热议问题