Esempio n. 1
0
        /// <summary>
        /// 文件下载 文件名默认自动生成
        /// </summary>
        /// <param name="FileUrl"></param>
        /// <param name="DefaultName"></param>
        /// <returns></returns>
        public static String FileDownload(String FileUrl, WebHeaderCollection headers, String DefaultFileName = "")
        {
            Console.WriteLine(FileUrl);
            String Filename = "";

            if (DefaultFileName != "")
            {
                Filename = DefaultFileName;
            }
            else
            {
                Filename = FileDownNameHelper(StaticValue.ImgTemp1, FileUrl);
            }

            using (WebClient DownloadWebClient = new WebClient())
            {
                DownloadWebClient.Headers = headers;
                try
                {
                    DownloadWebClient.DownloadFile(new Uri(FileUrl), Filename);
                }
                catch (Exception ex)
                {
                    PrintLog.Log(ex);
                    Filename = "";
                }
            }
            return(Filename);
        }
Esempio n. 2
0
        public static string PostDataForAjax(String Url, string PostData)
        {
            using (WebClient PostWebClient = new WebClient())
            {
                String Host = GetHost(Url);
                if (Host != "")
                {
                    PostWebClient.Headers.Add("Host", Host);
                }

                PostWebClient.Headers.Add("User-Agent", StaticValue.UserAgent);

                PostWebClient.Headers.Add("Accept", "*/*");
                PostWebClient.Headers.Add("Accept-Language", "zh-TW,zh;q=0.8,en-US;q=0.5,en;q=0.3");

                PostWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                PostWebClient.Headers.Add("X-Requested-With", "XMLHttpRequest");

                try
                {
                    byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(PostData);


                    byte[] responseArray = PostWebClient.UploadData(Url, byteArray);


                    return(Encoding.Default.GetString(responseArray));
                }
                catch (Exception ex)
                {
                    PrintLog.Log(ex);
                }
            }
            return("");
        }
Esempio n. 3
0
        /// <summary>
        /// 获取图片src的辅助方法
        /// </summary>
        /// <param name="Htmlnode"></param>
        /// <param name="Xpath"></param>
        /// <returns></returns>

        public static List <String> GetAttributesVlaueList(String Htmlnode, String Attributes, String Xpath)
        {
            List <string>      ValueList          = new List <string>();
            HtmlNodeCollection htmlNodeCollection = SelectNodes(Htmlnode, Xpath);



            if (htmlNodeCollection != null)
            {
                foreach (HtmlNode SingleNode in htmlNodeCollection)
                {
                    try
                    {
                        ValueList.Add(SingleNode.Attributes[Attributes].Value);
                    }
                    catch (Exception ex)
                    {
                        PrintLog.Log(ex);
                        PrintLog.Log("获取GetAttributesVlaueList错误");
                    }
                }
            }
            else
            {
                Console.WriteLine("null");
            }
            return(ValueList);
        }
Esempio n. 4
0
        /// <summary>
        /// 下载图片的方法
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="FileUrl"></param>
        /// <param name="headers"></param>
        /// <param name="DownStringCompletedCallBack"></param>
        /// <param name="ProgressCountCallBack"></param>
        /// <param name="FilstFlag"></param>
        public static void ImgDownload(String FloderPath, String FileUrl, WebHeaderCollection headers, AsyncCompletedEventHandler DownStringCompletedCallBack = null,
                                       DownloadProgressChangedEventHandler ProgressCountCallBack = null, bool FilstFlag = true)
        {
            using (WebClient webClient = new WebClient())
            {
                webClient.Headers = headers;


                String SavePath = FileDownNameHelper(FloderPath, FileUrl);
                Console.WriteLine(SavePath);
                if (SavePath == "")
                {
                    return;
                }
                if (DownStringCompletedCallBack != null)
                {
                    webClient.DownloadFileCompleted += DownStringCompletedCallBack;
                }
                if (ProgressCountCallBack != null)
                {
                    webClient.DownloadProgressChanged += ProgressCountCallBack;
                }

                try
                {
                    webClient.DownloadFileAsync(new Uri(FileUrl), SavePath);
                }
                catch (Exception ex)
                {
                    PrintLog.Log(ex);
                    PrintLog.Log("下载出错的链接:" + FileUrl);
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 获取JSON,使用手机UA
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static String HttpGetJsonForMobile(String url)
        {
            String HtmlCode = "";

            Console.WriteLine(url);
            using (WebClient webClient = new WebClient())
            {
                webClient.Headers.Add("Host", new Uri(url).Host);
                webClient.Headers.Add("User-Agent", StaticValue.MoblieUserAgent);
                webClient.Headers.Add("Accept-Language", "zh-TW,zh;q=0.8,en-US;q=0.5,en;q=0.3");
                webClient.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                webClient.Headers.Add("Accept-Language", "zh-TW,zh;q=0.8,en-US;q=0.5,en;q=0.3");
                webClient.Headers.Add("Pragma", "no-cache");
                webClient.Headers.Add("Cache-Control", "no-cache");

                try
                {
                    HtmlCode = new StreamReader(webClient.OpenRead(url)).ReadToEnd();
                }
                catch (Exception ex)
                {
                    PrintLog.Log(ex);
                }
            }
            return(HtmlCode);
        }
Esempio n. 6
0
 /// <summary>
 /// 删除文件夹  包括里面的文件
 /// </summary>
 /// <param name="FloderPath"></param>
 public static void FolderDelete(String FloderPath)
 {
     try {
         Directory.Delete(FloderPath, true);
     }catch (Exception ex) {
         PrintLog.Log(ex);
     }
 }
Esempio n. 7
0
        /// <summary>
        /// 获取文件夹路径
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string GetFloderPath(String FilePath)
        {
            string Floderpath = "";

            try { Floderpath = Path.GetDirectoryName(FilePath) + "\\"; } catch (Exception ex) {
                PrintLog.Log(ex);
            }
            return(Floderpath);
        }
Esempio n. 8
0
 /// <summary>
 /// 删除文件夹
 /// </summary>
 /// <param name="FloderPath"></param>
 public static void FolderCreate(String FloderPath)
 {
     try
     {
         Directory.CreateDirectory(FloderPath);
     }
     catch (Exception ex)
     {
         PrintLog.Log(ex);
     }
 }
Esempio n. 9
0
 /// <summary>
 /// 覆盖源文件,写入text
 /// </summary>
 /// <param name="FilePath"></param>
 /// <param name="Content"></param>
 public static void WriteUTF8Text(String FilePath, string Content)
 {
     try
     {
         FloderHelper.FloderExits(FloderHelper.GetFloderPath(FilePath), true);
         File.WriteAllText(FilePath, Content, Encoding.UTF8);
     }
     catch (Exception ex)
     {
         PrintLog.Log(ex);
     }
 }
Esempio n. 10
0
 public static string GetHost(String Url)
 {
     try
     {
         return(new Uri(Url).Host);
     }
     catch (Exception ex)
     {
         PrintLog.Log(ex);
     }
     return("");
 }
Esempio n. 11
0
 /// <summary>
 /// 删除文件的方法,会告诉你删除是否成功
 /// </summary>
 /// <param name="Filename"></param>
 /// <returns></returns>
 public static bool FileDelete(String Filename)
 {
     try
     {
         File.Delete(Filename);
         return(true);
     }
     catch (Exception ex)
     {
         PrintLog.Log(ex);
         return(false);
     }
 }
Esempio n. 12
0
        /// <summary>
        /// 获取目录下文件列表,带路径
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="Pattern"></param>
        /// <returns></returns>
        public static List <String> GetFullFileNameList(String FloderPath, String Pattern = "*.*")
        {
            List <String> FileList = new List <String>();

            try
            {
                FileList = Directory.GetFiles(FloderPath, Pattern).ToList <String>();
            }
            catch (Exception ex)
            {
                PrintLog.Log("当前是没有任何文件需要下载的情况,文件夹没有创建。");
                PrintLog.Log(ex);
            }
            return(FileList);
        }
Esempio n. 13
0
        /// <summary>
        /// 选择并且返回HtmlNodeCollection
        /// </summary>
        /// <param name="Htmlnode"></param>
        /// <param name="Xpath"></param>
        /// <returns></returns>
        public static HtmlNodeCollection SelectNodes(String Htmlnode, String Xpath)
        {
            HtmlNodeCollection NodeCollectionvalue = null;

            try
            {
                NodeCollectionvalue = LoadHtml(Htmlnode).DocumentNode.SelectNodes(Xpath);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
                PrintLog.Log("选择器错误");
            }
            return(NodeCollectionvalue);
        }/// <summary>
Esempio n. 14
0
    {/// <summary>
     /// 加载HTML
     /// </summary>
     /// <param name="Htmlnode"></param>
     /// <returns></returns>
        public static HtmlDocument LoadHtml(String Htmlnode)
        {
            HtmlDocument doc = new HtmlDocument();

            try
            {
                doc.LoadHtml(Htmlnode);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
                PrintLog.Log("加载HTML错误 ");
            }
            return(doc);
        }
Esempio n. 15
0
        /// <summary>
        /// 获取文件名 不带路径和后缀
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="Pattern"></param>
        /// <returns></returns>
        public static string GetFileNameWithoutExtension(String FilePath)
        {
            String FileName = "";

            try
            {
                FileName = Path.GetFileNameWithoutExtension(FilePath);
            }
            catch (Exception ex)
            {
                PrintLog.Log("当前是没有任何文件需要下载的情况,文件夹没有创建。");
                PrintLog.Log(ex);
            }
            return(FileName);
        }
Esempio n. 16
0
 /// <summary>
 /// 移动文件
 /// </summary>
 /// <param name="FilePath"></param>
 /// <param name="NewFilePath"></param>
 public static void FileMove(String FilePath, String NewFilePath)
 {
     try
     {
         FloderHelper.FloderExits(Path.GetDirectoryName(NewFilePath), true);
         if (File.Exists(NewFilePath))
         {
             NewFilePath = FloderHelper.GetFloderPath(NewFilePath) + DateTime.Now.ToFileTime() + GetFileName(NewFilePath);
         }
         File.Move(FilePath, NewFilePath);
     }
     catch (Exception ex)
     {
         PrintLog.Log(ex);
     }
 }
Esempio n. 17
0
        /// <summary>
        /// 获取文件夹列表
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <returns></returns>
        public static List <string> GetFloderNameList(string FloderPath)
        {
            List <string> FloderList = new List <string>();

            if (!FloderExits(FloderPath, true))
            {
                Console.WriteLine("创建新文件夹失败");
                return(FloderList);
            }
            try
            {
                FloderList = Directory.GetDirectories(FloderPath).ToList <string>();
            }
            catch (Exception ex) {
                PrintLog.Log(ex);
            }
            return(FloderList);
        }
Esempio n. 18
0
 /// <summary>
 /// 用utf8读取文件
 /// </summary>
 /// <param name="path"></param>
 /// <returns></returns>
 public static string ReadContextUtf8(string path)
 {
     FileExits(path, true);
     try
     {
         using (FileStream fs = new FileStream(path, FileMode.Open))
         {
             using (StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8))
             {
                 return(sr.ReadToEnd());
             }
         }
     }
     catch (Exception ex) {
         PrintLog.Log(ex);
     }
     return("");
 }
Esempio n. 19
0
        /// <summary>
        /// 同步获取html代码
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        public static string HttpGet(String Url)
        {
            String HtmlCode = "";

            using (WebClient webClient = new WebClient())
            {
                try
                {
                    webClient.Headers.Add("User-Agent", StaticValue.UserAgent);
                    webClient.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
                    HtmlCode = new StreamReader(webClient.OpenRead(Url)).ReadToEnd();
                }
                catch (Exception ex)
                {
                    PrintLog.Log(ex);
                }
            }
            return(HtmlCode);
        }
Esempio n. 20
0
 /// <summary>
 /// 判断文件夹是否存在
 /// </summary>
 /// <param name="FloderPath"></param>
 /// <param name="CreatFlag"></param>
 /// <returns></returns>
 public static bool FloderExits(String FloderPath, bool CreatFlag)
 {
     if (!CreatFlag)
     {
         return(false);
     }
     if (!Directory.Exists(FloderPath))
     {
         Console.WriteLine("已创建" + FloderPath);
         try { Directory.CreateDirectory(FloderPath); }
         catch (Exception ex)
         {
             PrintLog.Log(ex);
             PrintLog.Log("异常文件夹名字" + FloderPath);
             return(false);
         }
     }
     return(true);
 }
Esempio n. 21
0
        }/// <summary>

        /// 获取A标签里的href属性值
        /// </summary>
        /// <param name="Htmlnode"></param>
        /// <param name="Xpath"></param>
        /// <returns></returns>

        public static List <String> GetLinkVlaueList(String Htmlnode, String Xpath)
        {
            List <string>      ValueList          = new List <string>();
            HtmlNodeCollection htmlNodeCollection = SelectNodes(Htmlnode, Xpath);

            if (htmlNodeCollection != null)
            {
                foreach (HtmlNode SingleNode in htmlNodeCollection)
                {
                    try
                    {
                        ValueList.Add(SingleNode.Attributes["href"].Value);
                    }
                    catch (Exception ex)
                    {
                        PrintLog.Log(ex);
                        PrintLog.Log("获取超链接错误");
                    }
                }
            }
            return(ValueList);
        }/// <summary>
Esempio n. 22
0
 /// <summary>
 /// 修改文件md5
 /// </summary>
 /// <param name="Filename"></param>
 public static void ModifyMD5(String Filename)
 {
     if (!File.Exists(Filename))
     {
         Console.WriteLine("文件不存在");
         return;
     }
     else
     {
         //转化byte[]到list
         List <byte> ByteList = File.ReadAllBytes(Filename).ToList <byte>();
         ///添加一个二进制随机数
         ByteList.AddRange(Encoding.Default.GetBytes(Convert.ToString(new Random().Next(), 2)).ToList <byte>());
         try
         {
             File.WriteAllBytes(Filename, ByteList.ToArray());
         }
         catch (Exception ex)
         {
             PrintLog.Log(ex);
         };
     }
 }
Esempio n. 23
0
        /// <summary>
        /// 文件是否存在,是否需要创建
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="CreateFlag"></param>
        /// <returns></returns>
        public static bool FileExits(String FilePath, bool CreateFlag = false)
        {
            if (File.Exists(FilePath))
            {
                return(true);
            }

            else
            {
                if (CreateFlag)
                {
                    try
                    {
                        File.AppendAllText(FilePath, "");
                        return(true);
                    }
                    catch (Exception ex) {
                        PrintLog.Log(ex);
                    }
                }
            }
            return(false);
        }