HttpClient总是报java.lang.NoClassDefFoundError:错误

时间:2023-01-19 21:14:00
具体代码如下:
public static boolean login()throws Exception{

        HttpPost httpPost = new HttpPost(loginURL);
        List<BasicNameValuePair> list = new ArrayList<>();
        list.add(new BasicNameValuePair("username",""));
        list.add(new BasicNameValuePair("password",""));
        list.add(new BasicNameValuePair("utf-8",""));
        list.add(new BasicNameValuePair("authenticity_token",getAuthenticityToken()));
        list.add(new BasicNameValuePair("back_url",""));
        list.add(new BasicNameValuePair("autologin",""));

         HttpClient httpClient = HttpClients.createDefault();

        httpPost.setEntity(new UrlEncodedFormEntity(list));
        HttpResponse response = httpClient.execute(httpPost);

        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity,"utf-8");
        System.out.println(result);


        return true;
    }

其中httpClient和httpCore JAR包均为4.4.1版本

2 个解决方案

#1


这是我之前写的 一个你可以看看
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class TestPost {
    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("你的网址");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("username", "maoxiangyi"));
        nvps.add(new BasicNameValuePair("password", "maoxiangyi"));
        nvps.add(new BasicNameValuePair("reURL", "http://shop.itcast.cn/item/itemList.html"));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        CloseableHttpResponse response = httpclient.execute(httpPost);
        System.out.println(response.getStatusLine());
        //如果登录之后需要重定向,获取重定向的值
        Header[] headers = response.getAllHeaders();
        for (Header header : headers) {
            System.out.println(header);
        }
        if (response.getStatusLine().getStatusCode() == 302) {
            Header[] locations = response.getHeaders("Location");
            String url = locations[0].getValue();
            System.out.println(url);
            HttpGet httpGet = new HttpGet(url);
            CloseableHttpResponse response1 = httpclient.execute(httpGet);
            if (response1.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response1.getEntity();
                String html = EntityUtils.toString(entity, Charset.forName("utf-8"));
                System.out.println(html);
            }
            response1.close();
        }
        response.close();
    }
}

#2


把jar放到编译路径里

#1


这是我之前写的 一个你可以看看
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class TestPost {
    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("你的网址");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("username", "maoxiangyi"));
        nvps.add(new BasicNameValuePair("password", "maoxiangyi"));
        nvps.add(new BasicNameValuePair("reURL", "http://shop.itcast.cn/item/itemList.html"));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        CloseableHttpResponse response = httpclient.execute(httpPost);
        System.out.println(response.getStatusLine());
        //如果登录之后需要重定向,获取重定向的值
        Header[] headers = response.getAllHeaders();
        for (Header header : headers) {
            System.out.println(header);
        }
        if (response.getStatusLine().getStatusCode() == 302) {
            Header[] locations = response.getHeaders("Location");
            String url = locations[0].getValue();
            System.out.println(url);
            HttpGet httpGet = new HttpGet(url);
            CloseableHttpResponse response1 = httpclient.execute(httpGet);
            if (response1.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response1.getEntity();
                String html = EntityUtils.toString(entity, Charset.forName("utf-8"));
                System.out.println(html);
            }
            response1.close();
        }
        response.close();
    }
}

#2


把jar放到编译路径里