Exemplo n.º 1
0
        /// <summary>
        /// 根据url获取html
        /// </summary>
        /// <param name="url">url地址</param>
        /// <param name="headers">headers表头类容</param>
        /// <returns></returns>
        public async Task <string> GetHtmlAsync(string url, Dictionary <string, string> headers)
        {
            string result;

            byte[]       content      = null;
            NewWebClient newWebClient = new NewWebClient();

            if (headers?.Count > 0)
            {
                foreach (KeyValuePair <string, string> pair in headers)
                {
                    newWebClient.Headers.Add(pair.Key, pair.Value);
                }
            }
            content = await newWebClient.DownloadDataTaskAsync(new Uri(url));

            result = Encoding.GetEncoding("utf-8").GetString(content);
            return(result);
            //.Replace("\n", string.Empty)
            //.Replace("\r", string.Empty)
            //.Replace("\r\n", string.Empty);
        }
Exemplo n.º 2
0
        public string GetHtmlPost(string url, Dictionary <string, string> headers, Dictionary <string, string> parameter, out string html)
        {
            string result = Enums.成功;

            try
            {
                byte[]       content      = null;
                NewWebClient newWebClient = new NewWebClient();
                if (headers?.Count > 0)
                {
                    foreach (KeyValuePair <string, string> pair in headers)
                    {
                        newWebClient.Headers.Add(pair.Key, pair.Value);
                    }
                }
                NameValueCollection nvc = new NameValueCollection();
                if (parameter?.Count > 0)
                {
                    foreach (KeyValuePair <string, string> pair in parameter)
                    {
                        nvc.Add(pair.Key, pair.Value);
                    }
                }
                content = newWebClient.UploadValues(url, "Post", nvc);
                html    = Encoding.GetEncoding("utf-8").GetString(content);
                //.Replace("\n", string.Empty)
                //.Replace("\r", string.Empty)
                //.Replace("\r\n", string.Empty);
            }
            catch (Exception e)
            {
                result = e.Message;
                html   = string.Empty;
            }
            return(result);
        }