Java UrlEncodedFormEntity在Apache HttpClient 4中做什么?

Java UrlEncodedFormEntity在Apache HttpClient 4中做什么?,java,http-post,apache-httpclient-4.x,urlencode,Java,Http Post,Apache Httpclient 4.x,Urlencode,如果您希望使用参数执行HTTP Post,并使用“x-www-form-urlencoded”的内容类型发送它,那么在ApacheHTTP客户端3中执行此操作的方法是 HttpMethod method = new PostMethod(myUrl) method.setParams(mp) method.addParameter("user_name", username) method.addParameter("password", password)

如果您希望使用参数执行HTTP Post,并使用“x-www-form-urlencoded”的内容类型发送它,那么在ApacheHTTP客户端3中执行此操作的方法是

    HttpMethod method = new PostMethod(myUrl)

    method.setParams(mp)
    method.addParameter("user_name", username)
    method.addParameter("password", password)

    method.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')

    int responseCode = httpClient.executeMethod(method)
但是ApacheHTTP客户端4引入了UrlEncodedFormEntity对象,因此实现相同功能的新方法是

HttpPost post = new HttpPost(url);

List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("user_name", username));
urlParameters.add(new BasicNameValuePair("password", password));;

post.setEntity(new UrlEncodedFormEntity(urlParameters));

HttpResponse response = client.execute(post);
HttpPost=newhttppost(url);
List urlParameters=new ArrayList();
添加(新的BasicNameValuePair(“用户名”),用户名);
添加(新的BasicNameValuePair(“密码”,password));;
setEntity(新的UrlEncodedFormEntity(urlParameters));
HttpResponse response=client.execute(post);
此UrlEncodedFormEntity对象除了将内容类型设置为“x-www-form-urlencoded”之外还有什么用途


假设它创建了一个“由url编码对列表组成的实体”,但这不能通过设置内容类型来实现吗?

HttpEntity接口是控制如何处理请求/响应主体的顶级接口。在这种情况下,您使用的是
UrlEncodedFormEntity
,它知道如何对参数进行编码并以所需格式输出参数。

检查此项也可以检查