• 数据采集


    public static string PostWebRequest(string Data, string URL)
        {
            CookieContainer cc = new CookieContainer();
            string postData = Data;
            byte[] byteArray = Encoding.Default.GetBytes(postData); // 转化
    
            HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri(URL));
            webRequest2.CookieContainer = cc;
            webRequest2.Method = "POST";
            webRequest2.ContentType = "application/x-www-form-urlencoded";
            webRequest2.ContentLength = byteArray.Length;
            Stream newStream = webRequest2.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length);    //写入参数
            newStream.Close();
    
            HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
            StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);
            string text2 = sr2.ReadToEnd();
            if (text2 != null && text2.Length > 0)
            {
                return text2;
            }
            return "";
        }
    
        /// <summary>
        /// prosurfer------GET方式
        /// </summary>
        /// <param name="url">Url</param>
        /// <returns>PageHtml</returns>
        public static string GetPageHTML(string url)
        {
            WebRequest request = null;
            HttpWebResponse response = null;
            Stream stream = null;
            StreamReader sr = null;
            try
            {
                request = WebRequest.Create(url);
                request.Credentials = CredentialCache.DefaultCredentials;
                request.Timeout = 2000;
                response = request.GetResponse() as HttpWebResponse;
                stream = response.GetResponseStream();
                sr = new StreamReader(stream, Encoding.Default);
                return sr.ReadToEnd();
            }
            catch
            {
                return string.Empty;
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                    sr.Dispose();
                }
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }
            }
        }
  • 相关阅读:
    rocketMQ部署
    Redis Cluster集群搭建
    如何为nginx配置https(免费证书)
    台式机主机u盘安装centos7报错及注意事项
    docker运行模式图
    Mysql5.7版本安装
    Jumpserver安装过程
    macos系统安装mysql
    macos系统安装nginx
    一些小知识总结(虽然可能没用)
  • 原文地址:https://www.cnblogs.com/daixingqing/p/2768384.html
Copyright © 2020-2023  润新知