Exemplo n.º 1
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("");
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
                }
            }
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
 public static string GetHost(String Url)
 {
     try
     {
         return(new Uri(Url).Host);
     }
     catch (Exception ex)
     {
         PrintLog.Log(ex);
     }
     return("");
 }
Exemplo n.º 7
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>
Exemplo n.º 8
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);
        }
Exemplo n.º 9
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);
        }