• 用httpclient实现简单的模拟登陆


    <%@page import="javax.swing.RepaintManager"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ page import="java.net.*" %>
    <%@ page import="java.util.*" %>
    <%@ page import = "org.apache.http.HttpEntity"%>
    <%@ page import = "org.apache.http.HttpResponse"%>
    <%@ page import = "org.apache.http.NameValuePair"%>
    <%@ page import = "org.apache.http.client.CookieStore"%>
    <%@ page import = "org.apache.http.client.HttpClient"%>
    <%@ page import = "org.apache.http.client.entity.UrlEncodedFormEntity"%>
    <%@ page import = "org.apache.http.client.methods.CloseableHttpResponse"%>
    <%@ page import = "org.apache.http.client.methods.HttpGet"%>
    <%@ page import = "org.apache.http.client.methods.HttpPost"%>
    <%-- <%@ page import = "org.apache.http.cookie.Cookie"%> --%>
    <%@ page import = "org.apache.http.impl.client.AbstractHttpClient"%>
    <%@ page import = "org.apache.http.impl.client.DefaultHttpClient"%>
    <%@ page import = "org.apache.http.message.BasicNameValuePair"%>
    <%@ page import = "org.apache.http.util.EntityUtils"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    
    <%
       // 编码,解决中文乱码   
      // String str = URLEncoder.encode(request.getParameter("name"),"utf-8");  
       // 设置 name 和 url cookie 
        
              HttpClient httpClient = new DefaultHttpClient();
    //        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<NameValuePair> params = new ArrayList<NameValuePair>();
            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(new UrlEncodedFormEntity(params));
                httpClient.execute(httppost);
                 // 我们这里只是简单的打印出当前Cookie值以判断登录是否成功。
                CookieStore cookieStore = ((AbstractHttpClient) httpClient).getCookieStore();
                List<org.apache.http.cookie.Cookie> cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies();
                for (org.apache.http.cookie.Cookie cookiee : cookies){
               
               javax.servlet.http.Cookie  cookie= new javax.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");
    %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>设置 Cookie</title>
    </head>
    <body>
     <%response.sendRedirect(url);  %>
    </body>
    </html>

     报错:java.lang.IllegalArgumentException: An invalid domain [.cloudiip.com] was specified for this cookie

    我用的tomcat 8.5 改成Tomcat 7 就可以了

  • 相关阅读:
    Tomcat 7 自动加载类及检测文件变动原理
    ElasticSearch查询
    ElasticSearch集群的基本原理
    ElasticSearch基础
    hbase时间不同步问题引起的bug
    IDEA运行异常java.lang.NoClassDefFoundError: org/apache/spark/api/java/function/Function
    spark任务提交之SparkLauncher
    spark调优(二)-Apache Spark 内存管理详解
    spark调优(一)-开发调优,数据倾斜,shuffle调优
    spark内核源码深度剖析(1)--Spark内核架构深度剖析
  • 原文地址:https://www.cnblogs.com/brokencolor/p/9020054.html
Copyright © 2020-2023  润新知