致力于技术进步

专注于编程艺术

博客园 首页 新随笔 联系 订阅 管理

 public static UC_RESOURCE GetDownLoadUrl(string resourceURL)
        {
            string loginUrl = ConfigurationManager.AppSettings["deres_loginUrl"].ToString();
            string domainId = ConfigurationManager.AppSettings["deres_domainID"].ToString();
            string domainPassword = ConfigurationManager.AppSettings["deres_domainPassword"].ToString();
            string loginName = ConfigurationManager.AppSettings["deres_loginName"].ToString();
            string password = ConfigurationManager.AppSettings["deres_password"].ToString();
            string realName = ConfigurationManager.AppSettings["deres_realName"].ToString();

            ASCIIEncoding encoding = new ASCIIEncoding();
            string indata = "domainId=" + domainId + "&domainPassword=" + domainPassword + "&loginName=" + loginName + "&password=" + password + "&realName=" + realName;
            string outdata = "";
            //用来存放登陆后的Cookie
            CookieContainer myCookieContainer = new CookieContainer();
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(loginUrl);
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
            myHttpWebRequest.ContentLength = indata.Length;
            myHttpWebRequest.Method = "POST";
            myHttpWebRequest.CookieContainer = myCookieContainer;
            //设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer
            using (Stream myRequestStream = myHttpWebRequest.GetRequestStream())
            {
                using (StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312")))
                {
                    //发送数据
                    myStreamWriter.Write(indata);                 
                }
            }
            //myRequestStream.Close();
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            using (Stream myResponseStream = myHttpWebResponse.GetResponseStream())
            {
                using (StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312")))
                {
                    //把数据从HttpWebResponse的Response流中读出
                    outdata = myStreamReader.ReadToEnd();                  
                } 
            }
            myHttpWebRequest = (HttpWebRequest)WebRequest.Create(resourceURL);
            myHttpWebRequest.CookieContainer = myCookieContainer;
            myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            //myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
            using (Stream myResponseStream = myHttpWebResponse.GetResponseStream())
            {
                using (StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312")))
                {
                    outdata = myStreamReader.ReadToEnd();
                }              
            }
            //return outdata;
            UC_RESOURCE resource = new UC_RESOURCE();
            //获取下载链接地址
            string download = RegCutStr(outdata, "onclick=", ";>下载");
            download = download.Replace("quickDown(\"","").Replace("\",\"DOWNLOAD\")","");
            //去除所有HTML标签
            string temp = Regex.Replace(outdata, "<[^>]*>", "");
            //去除所有空格
            temp = Regex.Replace(temp, @"\s+", "");
            //string resourceDetails = "资源名称:[匣字的笔划]摘要:这是一个教学生如何写“匣”字的动画,通过红黑    的颜色差别让学生轻松了解该字的笔画笔顺,便于学生学习。学科:小学语文版本:通用年级:所有年级通用适用对象:学生,教师教育类型:小学教育文件格式:zip文件大小:261.41K&nbsp;";

            resource.RESOURCE_TITLE = CutStr(temp, "资源名称:", "摘要");
            string xueke = CutStr(temp, "学科:", "版本");
            string banben = CutStr(temp, "版本:", "年级");
            string nianji = CutStr(temp, "年级:", "适用对象");
            resource.RESOURCE_DEDCRIPTION = CutStr(temp, "摘要:", "学科");
            resource.RESOURCE_STUDY_TYPE = CutStr(temp, "教育类型:", "文件格式");
            resource.RESOURCE_SAVEPATH = xueke + "|" + banben + "|" + nianji;
            resource.RESOURCE_FORMAT = CutStr(temp, "文件格式:", "文件大小");

            string fileSize=CutStr(temp, "文件大小:", "&nbsp;下载");//fileSize=300k OR 400M
            fileSize=fileSize.Substring(0,fileSize.Length-1);
            resource.RESOURCE_SIZE = Convert.ToInt32((Decimal.Parse(fileSize) * 1024));           
            resource.RESOURCE_SAVEFOLDER = download;
            return resource;
        }

 

 

 

 

 

 

 

 

 

 

 

 

/******************************************************************************************************************

更新版本***** 根据requestUrl地址截获生成标准xml字符串,再对其进行解析

/******************************************************************************************************************

 

string loginUrl = ConfigurationManager.AppSettings["deres_loginUrl"].ToString();
            string domainId = ConfigurationManager.AppSettings["deres_domainID"].ToString();
            string domainPassword = ConfigurationManager.AppSettings["deres_domainPassword"].ToString();
            string loginName = ConfigurationManager.AppSettings["deres_loginName"].ToString();
            string password = ConfigurationManager.AppSettings["deres_password"].ToString();
            string realName = ConfigurationManager.AppSettings["deres_realName"].ToString();

            ASCIIEncoding encoding = new ASCIIEncoding();
            string indata = "domainId=" + domainId + "&domainPassword=" + domainPassword + "&loginName=" + loginName + "&password=" + password + "&realName=" + realName;
            string outdata = "";
            //http://www.cbern.com.cn/derscn/portal2/SearchAction.do?method=getResDetail
            //String requestUrl = "http://www.cbern.com.cn/derscn/portal2/SearchAction.do?method=getResDetail&resource_id=EB03FC32-D04C-CA15-28E4-A8F97ADBCFF3&loginType=remote&domain_id=ahedu.cn&key=FB640A627F292837683F1D4EE9FB500FF24E6CB29B5BA286D89E724AD2D23C6ED51A7E8F6E258433AAF1BF6050D523A2EA6FEB1E578BD125AF85C91F8DD2B24DB3B854AD8B0E117D7B33ABEBDBD387A6228DE277B5B1CD3833B9D1F7D798D2481EC601A346640212A7F93819D5565744206A2B26CC4D74764B0E241884E053584D8BC08B88831A89BCED89FBAE1D5EDA93A59FA1652293478078034C2D63A57F9593736C5F12EC34B77E4DA5C633E685FD4AE859C19A53B23256E2149216551D19D45F1D3E8209EF23C0D3004D4F64600FF4B85EB9C14BEE24D5C3F33AC0DB5A12B185D6BF66506C47DF8B52FC0C3B03079F188F97F8EECE48F2A9BFF21108A0545E95ED18CCB36F4BDD02C1CAC07FD46163DA9B8F8DF908";
             String requestUrl = "http://www.cbern.com.cn/derscn/portal2/SearchAction.do?method=getResDetail&resource_id=EB03FC32-D04C-CA15-28E4-A8F97ADBCFF3&loginType=remote&domain_id=ahedu.cn";
             CookieContainer myCookieContainer = new CookieContainer();
             HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(requestUrl);
             myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
             myHttpWebRequest.ContentLength = indata.Length;
             myHttpWebRequest.Method = "POST";
             myHttpWebRequest.CookieContainer = myCookieContainer;

             using (Stream myRequestStream = myHttpWebRequest.GetRequestStream())
             {
                 using (StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gbk")))
                 {
                     myStreamWriter.Write(indata);
                 }
             }

             HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
             using (Stream myResponseStream = myHttpWebResponse.GetResponseStream())
             {
                 using (StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gbk")))
                 {
                     outdata = myStreamReader.ReadToEnd();
                 }
             }

             Console.WriteLine(outdata);

 

     if(!String.IsNullOrEmpty(outdata))

    {

      /************************

        解析xml文档

      *************************/
               XmlDocument doc = new XmlDocument();
               doc.LoadXml(outdata);
               XmlElement element = doc.DocumentElement;
               XmlNode node = element.SelectSingleNode("TITLE");
               Console.WriteLine(node.InnerText);

        }

posted on 2009-12-02 10:58  stephen&amp;#183;周  阅读(348)  评论(0)    收藏  举报