cookie

 用了Apache Jakarta Common 下的HttpClient库,把原来的BatchImageFetch程序完全修改了一番,烦人的cookie问题终于解决了。Discuz论坛是用cookie验证用户是否登录,并且时常会更新cookie。在原来初始的代码中,在socket层面上操作,维护cookie真是一件麻烦的事情。
     HttpClient可以自动的处理cookie,并且封装cookie类,要手动操作也很方便。在实际中发现,若让HttpClient自动处理cookie,还是无法维持论坛的登录状态的,因为虽然HttpClient接收了服务器端的的Http响应头中的“Set-cookie”,但并没有将Cookie应用到新的请求当中,另外通过用iehttpheaders截包就会发现,httpclient会在header里构件多个cookie项,每一项只含有一个cookie,这同IE是不一样的。IE和Firefox会把所有的cookie打包成一个,然后在这个cookie里按照分号把每一项隔开,中间有个空格。因此,在代码中,先是获取HttpClient接收到的Cookie,然后再将之打包成IE那种形式,直接设置请求头“Cookie:....”。
     用HttClient真的很方便,论坛登录,网页下载,图片下载,仅仅几行代码即可,大大的减少了工作量。

 

------------------------------------------------------------------------------------------------------

Httpclient是一个很方便地http客户端,本身支持自动重定向功能。但是由于在做一些认证网站时重定向页面很可能会进行一些cookie的设置。
       比如163邮箱中跳转时就在重定向页面设置Coremail的值,而3.1版本存在一个缺陷。就是无法将重定向页面中的cookie值追加到当前cookie中。
       我对源代码进行了修改,解决了此问题。希望帮忙遇到此问题的朋友。
       下载commons-httpclient-3.1-src.zip,然后编辑 HttpMethodDirector.java 文件的 processRedirectResponse 方法。
       以下是我的修改:
private boolean processRedirectResponse(final HttpMethod method)
     
throws RedirectException {

        
//wcy set cookie start
         Header newCookieHeader = method.getResponseHeader("Set-Cookie");
         Header currentCookieHeader
= method.getRequestHeader("Cookie");
        
if(newCookieHeader!=null){
            
if(currentCookieHeader==null){
                 method.setRequestHeader(
"Cookie", newCookieHeader.getValue());                
             }
else{
                 method.setRequestHeader(
"Cookie", currentCookieHeader.getValue()+"; "+newCookieHeader.getValue());
             }
            
         }

        
//wcy set cookie over
        
      }

---------------------------------------------------------------------------------------------------------

网上有一些相册的下载工具,设置好帐号密码就可以下图片到本地。现在用java代码模拟一下。
HTTPClient的包有类似的例子。
    public static void test() throws HttpException, IOException ...{
        final String HOST = "photo.server.net";
        final int PORT    = 80;
        final String getMethodStr= "/userAccount/1524712";
        HttpClient client = new HttpClient();
        client.getHostConfiguration().setHost(HOST, PORT, "http");
        client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
       
        //Get first
        GetMethod getMethod = new GetMethod(getMethodStr);
        client.executeMethod(getMethod);
        System.err.println("------ Get content /userAccount/1524712: ");
        printOutStream(getMethod.getResponseBodyAsStream(), getMethod.getRequestCharSet());
        Header[] headers = getMethod.getResponseHeaders();
        for (Header header : headers) ...{
            System.err.println(header.toString());
        }
        getMethod.releaseConnection();
       
        //Get the useful session
        //CookieSpec cookieSpec = CookiePolicy.getDefaultSpec();
        Cookie[] getCookies = client.getState().getCookies();
        System.err.println("----- -Useful cookies: ");
        for (Cookie cookie : getCookies) ...{
            System.err.println(cookie.toString());
        }
       
        //POST password
           //其实无论客户端的页面怎么复杂,但是发过去的也只是包,可以使用一些抓包工具获取。
        final String url = "http://" + HOST + getMethodStr;
        final String encodedUrl = url.replaceAll(":", "%3A");
        final String postUrl = "/@restrict?furl=" + encodedUrl;
        PostMethod postMethod = new PostMethod(postUrl);
        NameValuePair text = new NameValuePair("text", "");
        NameValuePair pwd = new NameValuePair("pwd", "hello");
        NameValuePair abId = new NameValuePair("ab_id", "");
        postMethod.setRequestBody(new NameValuePair[]...{text, pwd, abId});
        client.executeMethod(postMethod);
        System.err.println("----------------");
        System.err.println("get post feedback:");
        printOutStream(postMethod.getResponseBodyAsStream(), postMethod.getResponseCharSet());
        postMethod.releaseConnection();
       
        //Redirect to other page.
        int statusCode = postMethod.getStatusCode();
        System.err.println(statusCode);
        if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_MOVED_PERMANENTLY)...{
            Header locationHeader = postMethod.getResponseHeader("location");
            if (locationHeader != null) ...{
                String redirectUri = locationHeader.getValue();
                if (redirectUri == null || "".equals(redirectUri)) ...{
                    redirectUri = "/";
                }
                getMethod = new GetMethod(redirectUri);
                client.executeMethod(getMethod);
                System.err.println("get redirect:");
                headers = getMethod.getResponseHeaders();
                for (Header header : headers) ...{
                    System.err.println(header.toString());
                }
                printOutStream(getMethod.getResponseBodyAsStream(), getMethod.getResponseCharSet());
                getMethod.releaseConnection();
            }
        }
       
       
    }     可以看到在一般的网站里面session-id是一直传递的。这里面的很都步骤都是可以使用java的URLConnection的,但有一种情况它不能很好处理. 例如, 登陆成功后, 服务器response.sendRedirect到一个新的URL, 这个时候返回的HTTP相应包应该只有HTTP头的, 里面包含一些返回状态码和要导向的URL地址.
HTTP/1.0 302 Moved Temporarily
Date: Sat, 03 Mar 2007 00:00:27 GMT
Server: Microsoft-IIS/6.3
Vary: Accept-Encoding
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: http://photo.xxxxxx.com/newurl
Content-Type: text/html; charset=UTF-8
Via: 1.0 photo.xxxx.com:8000 (Microsoft-IIS/7.1)
Connection: close
碰到(statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_MOVED_PERMANENTLY)...这些返回码的时候, URLConnection会主动跳转到这个location, 但是却没传递任何的cookies, 服务器肯定不认帐了. URLConnection虽然有设置requestProperty设置请求的属性, 但上面这种情况好像是无法设置cookies头的, 而且他提供的设置setDefaultProperty好像也不管用.  甚至你连location也无法获取到.
    顺便在这里提一下, 一些防盗链接其实可以用cookies头, 如果是经过当前网站来的请求, 藏点东西在cookies头里面; 还有一种可能是用Referer, 请求一个页面的时候, 不敢确定是不是因为返回了Cache-Control这些头,之后再去该网站拿其他东西的时候都要发送个Referer过去.
//第一次请求的返回包
HTTP/1.0 200 OK
Date: Fri, 02 Mar 2007 23:54:04 GMT
Server: Microsoft-IIS/6.3
Vary: Accept-Encoding
Pragma: no-cache
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Set-Cookie: FOTOSSID=a2d1a5c9f34308c3b49ad88813f11491; path=/; domain=.photo.xxxxx.net
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Content-Type: text/html; charset=UTF-8
Content-Encoding: gzip
Content-Length: 2762
Via: 1.0 photo.xxxx.net:8000 (Microsoft-IIS/7.1)
Connection: keep-alive

//再次请求包
Host: s.photo.xxxxx.net
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2
Accept: image/png,*/*;q=0.5
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://photo.xxx.net/userName/1524712
Cookie:  FOTOSSID=a2d1a5c9f34308c3b49ad88813f11491

可以看到HttpClient还是很好用的, 用它甚至可以做一些简单的程序做点自动化的web页面的测试d.
    用这个破解别人的相册? 估计效率是不怎么行的, 服务器狠些,再加个随机图片验证,或者几次失败之后封杀你的连接:)


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zealvampire/archive/2007/04/05/1553098.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值