网络软件 系统工具 应用软件 图形图像 多媒体类 免费游戏 安全相关 免费音乐 网页素材 电子书籍 考试考题 建站源码
教育教学 多媒体类 编程开发 操作系统 游戏天地 娱乐天地 简历求职 站长专区 网页设计 安全技术 图形图像 文学驿站
业界资讯 | 图形图像 | 操作系统 | 网络冲浪 | 工具软件 | 办公软件 | 媒体动画 | 精文荟萃 | 认证考试 | 网页设计 | 技术开发 | 专栏
当前位置:热点网络学院技术开发.Net 专栏.NET使用用http代理下载sourceforge的cvs仓库[原理+C#代码]
精品推荐
热点TOP10
·ASP.Net实现将Word转换PDF格式
·ASP.NET IIS 注册工具 (Aspnet_regiis.exe)
·安装ASP.NET完全手册
·ASP.Net+XML打造留言薄
·Asp.Net中使用水晶报表
·asp.net结合html,javascript实现无刷新跨域数据提交
·Autodesk官方最新的.NET教程(一)(C#版)
·ASP.NET中为DataGrid添加单选框
·一个ASP.NET的进度条
·用Asp.Net创建基于Ajax的聊天室程序
·在ASP.net中保存/取出图片入/从SQL数据库(可用于上传图片)
·解决ASP.NET的进程帐户没有访问 IIS的权限
·VB.Net基本语句
·Asp.net cache 简述
·Web应用程序ASP.NET开发电子商务网站购物车
·关于ASP.NET在IIS一些问题的经验总结
·跳出封装剖析ASP.NET脚本回调的原理
·一个高效判断字符串是否全数字的函数
·在ASP.NET应用中插入flash动画
·ASP.NET 拒绝访问
用http代理下载sourceforge的cvs仓库[原理+C#代码]
日期:2007年1月15日 作者: 查看:[大字体 中字体 小字体]

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=""(ViewDownload)\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;
        }
    }
}
http://www.cnblogs.com/hesicong/archive/2007/01/11/617734.html

(出处:http://www.vipcn.com)

关于我们 | 帮助(?) | 版权声明 | 友情连接 
Copyright 2005-2005 viphot.com All Rights Reserved.
Powered by:mesky