现在问题是这样的:
    我想通过HttpClient实现实现一个网站的登录,那种web形式的登录已经实现了,比如开心网那种。但是这个网站的登录是使用的windows自带的那种登录,这种怎么实现呢?

解决方案 »

  1.   

     String fullName = "HttpClient:executeHTTP";
            PostMethod postMethod = new PostMethod(httpurl);
            // 设置头文件的编码
            postMethod.setRequestHeader("ContentType", "text/html;charset=UTF-8");
            //设置参数
            NameValuePair etn = new NameValuePair("exten", exten);
            NameValuePair phone = new NameValuePair("call_num", call_num);
            postMethod.setRequestBody(new NameValuePair[] { etn, phone });
            //设置参数的字符编码
            postMethod.getParams().setContentCharset("utf-8");
            // 多线程模式模式下使用httpclient
            MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
            org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient(manager);
            InputStream retStream = null;
            HttpClientParams httpClientParams = new HttpClientParams();
            // 设置读取内容时间超时
            httpClientParams.setSoTimeout(10000);
            // 设置系统提供的默认恢复策略,在发生异常时候将自动重试3次
            httpClientParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
            httpClient.setParams(httpClientParams);
            // 设置连接超时
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
            
            httpClient.getHttpConnectionManager().getParams().setDefaultMaxConnectionsPerHost(32);
            // 设置最大连接数
            httpClient.getHttpConnectionManager().getParams().setMaxTotalConnections(256);
            // 统计调用接口时间
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            try
            {
                // 返回的状态码
                httpClient.executeMethod(postMethod);
                // 检查是否重定向
                int statuscode = postMethod.getStatusCode();
                // 301 SC_MOVED_PERMANENTLY 页面已经永久移到另外一个新地址
                // 302 SC_MOVED_TEMPORARILY 页面暂时移动到另外一个新的地址
                // 303 SC_SEE_OTHER 客户端请求的地址必须通过另外的 URL 来访问
                // 307 SC_TEMPORARY_REDIRECT 同 SC_MOVED_TEMPORARILY
                if (statuscode == HttpStatus.SC_MOVED_PERMANENTLY || statuscode == HttpStatus.SC_MOVED_TEMPORARILY
                        || statuscode == HttpStatus.SC_SEE_OTHER || statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)
                {
                    // 读取新的地址
                    Header header = postMethod.getResponseHeader("location");
                    if (null != header)
                    {
                        String newuri = header.getValue();
                        if (null == newuri || "".equals(newuri))
                        {
                            newuri = "/";
                            PostMethod post = new PostMethod(newuri);
                            httpClient.executeMethod(post);
                            retStream = post.getResponseBodyAsStream();
                        }
                        else
                        {
                            LogManager.runLog(RunLevel.INFO(), "invalid rediect");
                        }
                    }
                }
                stopWatch.stop();
                
                retStream = postMethod.getResponseBodyAsStream();
            }
            catch (HttpException e)
            {
                LogManager.runLog(RunLevel.ERROR(), fullName + "http 协议异常  failed", e);
            }
            catch (IOException e)
            {
                LogManager.runLog(RunLevel.ERROR(), fullName + "http io exeception", e);
            }
            finally
            {
                // 释放连接
                // /postMethod.releaseConnection();
            }
            return retStream;
      

  2.   

    lz 指需要知道登录的url即可在后台通过上面的代码实现登录功能。
      

  3.   

    谢谢各位,其实问题是这样的,就是我通过httpClient对url进行解析,但如果当前这个url需要web form的登录,那么httpClient也提供了登录验证的功能,但是不知道如果登录方式是windows那种登录,如果验证,后来自己查了下,问题解决了,顺便分享下,windows验证其实就是basic验证,
    以下是apache对basic验证模式的解释
    BasicBasic authentication is the original and most compatible authentication scheme for HTTP. Unfortunately, it is also the least secure as it sends the username and password unencrypted to the server. Basic authentication requires an instance of UsernamePasswordCredentials (which NTCredentials extends) to be available, either for the specific realm specified by the server or as the default credentials.apache提供的实例代码地址如下:
    http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/examples/BasicAuthenticationExample.java?view=up
    谢谢各位!