简体   繁体   English

构造函数UrlEncodedFormEntity(List <NameValuePair> ,String)是构建帖子请求时的未定义错误

[英]The constructor UrlEncodedFormEntity(List<NameValuePair>, String) is undefined error in building post request

I am trying to make a post request in java using Apache HTTP components , when I set my entity on this line 当我在这行上设置实体时,我试图使用Apache HTTP components 在Java中发出发布请求

httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

It says 它说

"The constructor UrlEncodedFormEntity(List, String) is undefined" and I am not sure why. “构造函数UrlEncodedFormEntity(List,String)未定义”,我不确定为什么。

Here is my entire code 这是我的整个代码

@Component
public class ScheduledTasks {

@Scheduled(cron="0 9 1-7 * 1 *")    //first monday of each month, at 9am
public void dataLoaderTask() throws Exception   {
    HttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost("https://erudite-master-api-awsmaui.lab.expts.net/erudite/search");
        List<NameValuePair> params = new ArrayList<NameValuePair>(3);
            params.add(new NameValuePair("action", "count"));
            params.add(new NameValuePair("fields", "Status"));
            params.add(new NameValuePair("filters", ""));
        httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

            //Execute and get the response.
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                try {
                    // do something useful
                } finally {
                    instream.close();
                }
            }
    }

Every resource that I have searched shows this is the proper way of doing so, so I am not sure why it is returning undefined. 我搜索的每个资源都表明这样做是正确的方法,因此我不确定为什么它返回未定义。

Maybe there could be conflicting Apache HTTP Components JAR files. 可能存在冲突的Apache HTTP组件JAR文件。 I tried the below code and there was no compilation error: These are the two JAR files used in the classpath: http-client-4.5.3.jar, http-core-4.4.6.jar. 我尝试了以下代码,没有编译错误:这是在类路径中使用的两个JAR文件:http-client-4.5.3.jar,http-core-4.4.6.jar。 Using spring-boot will ensure the latest version of open source JAR files in your repository. 使用spring-boot将确保您的存储库中具有最新版本的开源JAR文件。

@Scheduled(cron = "0 9 1-7 * 1 *") // first monday of each month, at 9am
public void dataLoaderTask() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost("https://erudite-master-api-awsmaui.lab.expts.net/erudite/search");
    List<NameValuePair> params = new ArrayList<NameValuePair>(3);
    params.add(new BasicNameValuePair("action", "count"));
    params.add(new BasicNameValuePair("fields", "Status"));
    params.add(new BasicNameValuePair("filters", ""));
    httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    // Execute and get the response.
    CloseableHttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        try {
            // do something useful
        } finally {
            instream.close();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM