用http代理下载sourceforge的cvs仓库[原理+c#代码]_c#应用
6169 点击·0 回帖
![]() | ![]() | |
![]() | 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;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; } } } | |
![]() | ![]() |