用http代理下载sourceforge的cvs仓库[原理+C#代码]

12月的地震震断了几根光缆,麻烦的事情接踵而至,直连sourceforge上不去了,只好用代理。虽然能够下载到打包好的代码,但某些代码已显得陈旧,而cvs最新的代码确要用工具checkout,但很郁闷的事情cvs不支持http代理。有一下一些解决办法:
1、找sockets代理,然后用eborder等软件使cvs能够用。明显,网络上提供sockets代理的少之又少。
2、通过工具把http代理变成sockets代理。当然此法能够行得通,但cvs checkout的速度慢的惊人,没有可行性。
3、找联通的网络,他们出国没有受到损坏,速度很快。
4、等待网络修好:)
5、另:感谢A.E告诉我eclipse也可以支持!
……
由于急需一些开源项目的cvs代码,以上途径又不太现实,所以还是另想办法。
但令人高兴的是,我可以用http代理通过浏览器查看sourceforge的ViewVC工具所提供的cvs代码,这给我了很大的启发,准备利用 ViewVC来下载源代码。随后就分析ViewVC生成的页面,我们这里以lib3ds.cvs.sourceforge.net作为例子。
打开页面以后呈现在面前的是一个目录结构,点击进入下一层目录,可以看到ViewVC为我们输出了目录和文件。每一个目录和文件都有一个超链接,如果单击目录的话会进入下一层目录,而点击文件会进入文件的详细说明(例http: //lib3ds.cvs.sourceforge.net/lib3ds/lib3ds/3ds-utils.spec.in?view=log),包括CVS Tags等等。
在http://lib3ds.cvs.sourceforge.net/lib3ds/lib3ds/3ds-utils.spec.in?view=log 页面里,会发现有一个download超链接,这个超链接可以让我们下载到这个文件,点击这个文件以后,地址栏会变为:http: //lib3ds.cvs.sourceforge.net/*checkout*/lib3ds/lib3ds/3ds-utils.spec.in?revision =1.1,文件的详细内容也在眼前了,这就是我们需要的源代码。
请注意地址里面的/*checkout*/,这将是我们的入手点,只要找到文件的相对路径,我们在前面加上/*checkout*/就可以下载这个文件了。而后面的参数可以忽略,默认会得到最新的版本。
很好,下一步就是分析如何得到相对地址。由于ViewVC工具生成的网页代码很有规律,一个目录的超链接类似于:
<a name="examples" href="/lib3ds/lib3ds/examples/" title="View directory contents">
而一个文件的超链接类似于:
<a href="/lib3ds/lib3ds/lib3ds/viewport.h?revision=1.6&amp;view=markup" title="View file contents">
和<a href="/*checkout*/lib3ds/lib3ds/autogen.sh?revision=1.14" title="Download file contents">
只需要通过正则表达式就可以把地址抓出来,剩下的工作应该知道了吧:)
我做了一个小小的程序来实现最基本的功能,对于更多的功能,比如更多的错误恢复、多线程下载等等请自己实现。
VS2005演示工程和下载地址:http://www.hesicong.net/aspx/fileuploader/Upload/Internet_ViewVC_CVS_Checkout.rar
最后不要忘了到我的个人主页来凑个热闹哦http://www.hesicong.net
下面是完整的源代码,在VS2005下编译运行成功。
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Net;
using  System.IO;
using  System.Text.RegularExpressions;

namespace  Internet_ViewVC_CVS_Checkout
{
    
///   <summary>
    
///  A simple ViewVC CVS checkout class
    
///  Author: hesicong 
    
///  Homepage: www.hesicong.net hesicong.cnblogs.com
    
///   </summary>
     public   class  ViewVC_CVS_Checkout
    {
        
public   static  Regex regFindDir  =   new  Regex(
            
@" href=""(?<DIRURL>.*)?""\s*title=""View\sdirectory\scontents""> " ,
            RegexOptions.IgnoreCase
            
|  RegexOptions.CultureInvariant
            
|  RegexOptions.IgnorePatternWhitespace
            
|  RegexOptions.Compiled
            );

        
public   static  Regex regFindFiles  =   new  Regex(
             
@" href=""(?<FILEURL>.*)?\?(.*)""\s*title=""(View|Download)\sfi "
    
+   @" le\scontents""> " ,
            RegexOptions.IgnoreCase
            
|  RegexOptions.CultureInvariant
            
|  RegexOptions.IgnorePatternWhitespace
            
|  RegexOptions.Compiled
            );

        
public   class  DirList
        {
            
public  List < DirList >  dir;
            
public  List < string >  file;
        };
        
static   string  store;
        
static  WebClient wc  =   new  WebClient();
        
static  WebClient wcFileDown  =   new  WebClient();
        
        
public   static   void  Main()
        {
            Console.ForegroundColor 
=  ConsoleColor.White;
            WebProxy proxy 
=   new  WebProxy();
            Console.WriteLine(
" ====================================================================== " );
            Console.WriteLine(
"                       ViewVC CVS Checkout                              " );
            Console.WriteLine(
"      Author:hesicong Homepage:www.hesicong.net hesicong.cnblogs.com    " );
            Console.WriteLine(
" ====================================================================== " );
            Console.Write(
" Enter your Proxy:(IP:PORT):  " );
            proxy.Address 
=   new  Uri( " HTTP:// " + Console.ReadLine());
            Console.Write(
@" ViewVC Start URL:(HTTP:///):  " );
            wc.BaseAddress 
=  Console.ReadLine();
            Console.Write(
@" Where to store your files? (Driver:\Dir):  " );
            store 
=  Console.ReadLine();
            wcFileDown.Proxy 
=  proxy;
            wc.Proxy 
=  proxy;
            Console.WriteLine(
" Start downloading " );
            DirList dl;
            dl 
=  getTree( " / " );
            Console.WriteLine(
" Done! Press Enter Key to Finish " );
            Console.ReadLine();
        }

        
///   <summary>
        
///  Search the newest files and download them from CVS
        
///   </summary>
        
///   <param name="address"> Relative url where ViewVC page </param>
        
///   <returns></returns>
         public   static  DirList getTree( string  address)
        {
            DirList dir 
=   new  DirList();
            dir.dir
= new  List < DirList > ();
            dir.file 
=   new  List < string > ();
            Console.ForegroundColor 
=  ConsoleColor.DarkGreen;
            Console.WriteLine(
" [Getting Content]  "   +  address);
            
string  src;
            
try
            {
                 src 
=  wc.DownloadString(address);
            }
            
catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                
return   null ;
            }
            MatchCollection dirMc 
=  regFindDir.Matches(src);
            
// Add dirs
            Console.WriteLine( " Find  "   +  dirMc.Count  +   "  Dirs " );
            
foreach  (Match aDirMatch  in  dirMc)
            {
                
string  aDir  =  aDirMatch.Groups[ " DIRURL " ].Value;
                Console.ForegroundColor 
=  ConsoleColor.Green;
                Console.WriteLine(
" [Enter DIR]  "   +  aDir);
                dir.dir.Add(getTree(aDir));
            }

            MatchCollection fileMc 
=  regFindFiles.Matches(src);
            Console.WriteLine(
" Find  "   +  fileMc.Count  +   "  Files " );
            
foreach  (Match aFileMatch  in  fileMc)
            {
                
string  aFile  =  aFileMatch.Groups[ " FILEURL " ].Value;
                aFile 
=  aFile.Replace( " /*checkout* " "" );    // Replace download file content url to normal
                dir.file.Add(aFile);
                
string  b  =  wc.BaseAddress.ToString();
                
string  urlToDown  =  b  +   " *checkout* "   +  aFile;
                Console.ForegroundColor 
=  ConsoleColor.Yellow;
                Console.WriteLine(
"   Downloading:  " + urlToDown);
                
string  fileStore  =  store  +  aFile.Replace( ' / ' , ' \\ ' );
                
string  fileStoreDir = Path.GetDirectoryName(fileStore);
                
if (Directory.Exists(fileStoreDir) == false )
                {
                    Directory.CreateDirectory(fileStoreDir);
                }
                
if (File.Exists(fileStore) == true )
                {
                    Console.ForegroundColor 
=  ConsoleColor.Red;
                    Console.Write(
"    File already exist, rewrite?(Y/N) " );
                    
if (Console.ReadKey().Key == ConsoleKey.Y)
                    {
                        File.Delete(fileStore);
                    }
                    Console.WriteLine();
                }
                
try
                {
                    wcFileDown.DownloadFile(urlToDown, fileStore);
                }
                
catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    
return   null ;
                }
            }
            Console.ResetColor();
            
return  dir;
        }
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值