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

FYJ_jie 2018-03-05 05:34:35
具体代码如下:
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版本
...全文
443 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
聪头 2018-03-06
  • 打赏
  • 举报
回复
把jar放到编译路径里
  • 打赏
  • 举报
回复
这是我之前写的 一个你可以看看 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(); } }

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧