java简单实现模拟登陆_用httpclient实现简单的模拟登陆

--%>

//编码,解决中文乱码//String str = URLEncoder.encode(request.getParameter("name"),"utf-8");//设置 name 和 url cookie

HttpClient httpClient= newDefaultHttpClient();//RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();//CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();

HttpPost httppost= new HttpPost("https://tdc.cloudiip.com/http/do.jhtml?router=systemIndexService.doLogin");

List params = new ArrayList();

params.add(new BasicNameValuePair("uuid", "7f0570ab4c704aeca037772b54b5f292"));

params.add(new BasicNameValuePair("account", "18612222442"));

params.add(new BasicNameValuePair("password", "abc123"));

params.add(new BasicNameValuePair("checkcode", "1234"));

params.add(new BasicNameValuePair("sub", "登录"));try{

httppost.setEntity(newUrlEncodedFormEntity(params));

httpClient.execute(httppost);//我们这里只是简单的打印出当前Cookie值以判断登录是否成功。

CookieStore cookieStore =((AbstractHttpClient) httpClient).getCookieStore();

List cookies =((AbstractHttpClient) httpClient).getCookieStore().getCookies();for(org.apache.http.cookie.Cookie cookiee : cookies){

javax.servlet.http.Cookie cookie= newjavax.servlet.http.Cookie(cookiee.getName(),cookiee.getValue());

cookie.setDomain(".cloudiip.com");

cookie.setPath("/");

cookie.setVersion(cookiee.getVersion());

cookie.setMaxAge(60*60*24);//设置cookie过期时间为24小时。//name.setMaxAge(60*60*24);//url.setMaxAge(60*60*24);//在响应头部添加cookie

response.addCookie(cookie);

System.out.println(cookie.getName()+"==="

+cookie.getValue()+"==="

+cookie.getDomain()+"======"+cookie.getVersion());

}

response.setStatus(200);

}catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}finally{

httppost.releaseConnection();

}//从url中获取的重定向地址

String url = request.getParameter("url");%>

设置 Cookie
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值