随笔-88  评论-77  文章-48  trackbacks-0

    /**
     * 方法名称:getPostPage<p>
     * 方法功能:自动登录请求页面<p>
     * 参数说明:登陆主机的ip地址, 端口号<p>
     * 返回:<p>
     * 作者:李明
     * 日期:2006年3月13日
     */
    public void getPostPage(String host, int port)
    {
        HttpClient httpClient = new HttpClient();
        httpClient.getHostConfiguration().setHost(host, port);
        // 创建POST方法的实例
        PostMethod postMethod = new PostMethod(url);
        // 使用系统提供的默认的恢复策略
        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        // 填入各个表单域的值
        NameValuePair[] data =
            {new NameValuePair("tUsername", "admin"), new NameValuePair("tPassword", "admin")};
        // 将表单的值放入postMethod中
        postMethod.setRequestBody(data);
        try
        {
            // 执行postMethod
            int statusCode = httpClient.executeMethod(postMethod);
            System.out.println("statusCode " + statusCode);
            // HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
            // 301或者302
            if(statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
            {
                // 从头中取出转向的地址
                Header locationHeader = postMethod.getResponseHeader("location");
                String location = null;
                if(locationHeader != null)
                {
                    location = locationHeader.getValue();
                    System.out.println("The page was redirected to:" + location);
                }
                else
                {
                    System.err.println("Location field value is null.");
                }
                return;
            }
            if(statusCode != HttpStatus.SC_OK)
            {
                System.err.println("Method failed: " + postMethod.getStatusLine());
            }
            System.out.println(statusCode);
            /*
             * //如果所请求的页面已设定cookie那么无需返回内容,可直接使用url访问登陆成功页面。
             * // 读取内容
             *  byte[] responseBody = postMethod.getResponseBody();
             * // 处理内容
             *  System.out.println(new String(responseBody));
             */
        }
        catch(HttpException e)
        {
            // 发生致命的异常,可能是协议不对或者返回的内容有问题
            System.out.println("Please check your provided http address!");
            e.printStackTrace();
        }
        catch(IOException e)
        {
            // 发生网络异常
            e.printStackTrace();
        }
        finally
        {
            // 释放连接
            postMethod.releaseConnection();
        }
    }

    /**
     * 方法名称:getPostPage163<p>
     * 方法功能:返回163信箱自动登陆页面<p>
     * 参数说明:163邮箱用户名和密码<p>
     * 返回:String 登陆成功后的页面<p>
     * 作者:李明
     * 日期:2006年3月13日
     */
    public static String getPostPage163(String userid, String passid)
    {
        HttpClient httpClient = new HttpClient();
        httpClient.getHostConfiguration().setProxy("proxynj.zte.com.cn", 80);
        String url = "http://reg.163.com/CheckUser.jsp";
        String buff = null;
        // 创建POST方法的实例
        PostMethod postMethod = new PostMethod(url);
        // 使用系统提供的默认的恢复策略
        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        // 填入各个表单域的值
        NameValuePair[] data =
            {new NameValuePair("username", userid), new NameValuePair("password", passid)};
        // 将表单的值放入postMethod中
        postMethod.setRequestBody(data);
        try
        {
            // 执行postMethod
            int statusCode = httpClient.executeMethod(postMethod);
            // HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
            // 301或者302
            if(statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
            {
                // 从头中取出转向的地址
                Header locationHeader = postMethod.getResponseHeader("location");
                String location = null;
                if(locationHeader != null)
                {
                    location = locationHeader.getValue();
                    System.out.println("The page was redirected to:" + location);
                }
                else
                {
                    System.err.println("Location field value is null.");
                }
                return null;
            }
            if(statusCode != HttpStatus.SC_OK)
            {
                System.err.println("Method failed: " + postMethod.getStatusLine());
            }
            // 读取内容
            byte[] responseBody = postMethod.getResponseBody();
            // 处理内容
            System.out.println(new String(responseBody));
            String redirect = new String(responseBody);

            System.out.println(redirect.lastIndexOf("></HEAD>"));
            System.out.println(redirect.substring(redirect.indexOf("URL") + 4, redirect.lastIndexOf("></HEAD>") - 1));
            redirect = redirect.substring(redirect.indexOf("URL") + 4, redirect.lastIndexOf("></HEAD>") - 1);
            GetMethod getMethod = new GetMethod("http://reg.163.com/" + redirect);
            httpClient.executeMethod(getMethod);
            buff = getMethod.getResponseBodyAsString();
        }
        catch(HttpException e)
        {
            // 发生致命的异常,可能是协议不对或者返回的内容有问题
            System.out.println("Please check your provided http address!");
            e.printStackTrace();
        }
        catch(IOException e)
        {
            // 发生网络异常
            e.printStackTrace();
        }
        finally
        {
            // 释放连接
            postMethod.releaseConnection();
        }
        return buff;

    }

posted on 2006-04-28 11:19 崛起的程序员 阅读(1268) 评论(0)  编辑  收藏 所属分类: java

只有注册用户登录后才能发表评论。


网站导航: