java httpclient cookie 登录的方法,续上面的帖子 iehttpheaders 的用法

     对于带有cooki'e的网站就稍微麻烦点,我在一个论坛上看到了解决办法,现下个iehttpheaders,利用他可以抓取到你和服务器通信是的cookie,再用程序里面模拟ie去给fuwuq发cookie信息就可以了。

   像这样,你可以看到cookie信息,在程序里可以这样 :

  HttpClient httpClient = new HttpClient();

       httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

              // 创建GET方法的实例

       GetMethod getMethod = new GetMethod("http://kyxk.net/wForum/disparticle.php?boardName=LifeScience&ID=2114");

    getMethod.setRequestHeader("Accept"," image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");

    getMethod.setRequestHeader("Referer","http://kyxk.net/wForum/disparticle.php?boardName=LifeScience&ID=2114");

    getMethod.setRequestHeader("Accept-Language","zh-cn");

    getMethod.setRequestHeader("Accept-Encoding"," gzip, deflate");

    getMethod.setRequestHeader("If-Modified-Since","Thu, 29 Jul 2004 02:24:49 GMT");

    getMethod.setRequestHeader("If-None-Match","'3014d-1d31-41085ff1'");

    getMethod.setRequestHeader("User-Agent"," Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.2)");

    getMethod.setRequestHeader("Host","kyxk.net");

    getMethod.setRequestHeader("Connection"," Keep-Alive");

    getMethod.setRequestHeader("Cookie"," iscookies=0; UTMPUSERID=guest; UTMPKEY=37127828; UTMPNUM=18016; LOGINTIME=1180521529");

5.3.13 )

getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,

                            new DefaultHttpMethodRetryHandler());

              try {

                     // 执行getMethod

                     int statusCode = httpClient.executeMethod(getMethod);

                     if (statusCode != HttpStatus.SC_OK) {

                            System.err.println("Method failed: "

                                          + getMethod.getStatusLine());

                     }

                     // 读取内容                        

                     byte[] responseBody = getMethod.getResponseBody();                

                        String content = new String(responseBody);

                        lexer = new Lexer(content);

                          // 处理内容

                      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 {

                     // 释放连接

              //     getMethod.releaseConnection();

              }     上面黑题标出来的就是发送cookie的代码,很有用,在这里谢谢给出这个方法的哥们,呵呵 ! 好了就这些了。

猜你喜欢

转载自jlins.iteye.com/blog/1666177
今日推荐