网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C#应用
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.用C#下的Raw Socket编程实现网络.
.C#中使用SendMessage.
.利用网页强制重启服务器(C#).
.C#導出Excel源碼.
.分享动态生成文字图片解决方案.
..NET下对二进制文件进行加密解密.
.C#中计算两个时间的差.
.使用C#与NNTP服务器交互!.
.C#中使用存储过程中的返回值.
.C#用代码打开软键盘.
.用C#+WMI实现获取w3wp进程对应的.
.关于C#中枚举打印机.
.关于VS 2008和.NET 3.5 Beta2新特.
.发送邮件程序(Csharp2005).
.用C#做ScreenSaver.
.C# 中启动进程的三种方法.
.C#使用指针.
.操作 SQL Server Mobile 2005 数.
.一个产生中文累计数的代码片断.
.C#中屏蔽窗体关闭的消息 .

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

发表日期:2007-1-12


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

上一篇:c# MD5加密算法的一个实例 人气:14642
下一篇:C# 参考之命名空间关键字:namespace、using 人气:5684
浏览全部c#的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐