C# HTTP Get Post 提交数据可以指定代理IP、指定浏览器、指定来源

        /// <summary>
        /// Get读取数据
        /// </summary>
        /// <param name="myUrl">网址</param>
        /// <param name="myProxy">IP代理</param>
        /// <param name="myUserAgent">浏览器</param>
        /// <param name="myReferer">来源</param>
        /// <returns></returns>
        private string HttpGetData(string myUrl, WebProxy myProxy = null, string myUserAgent = "", string myReferer = "")
        {
            try
            {
                //创建请求
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUrl);
                if (myProxy != null) request.Proxy = myProxy; //设置代理
                if (!string.IsNullOrWhiteSpace(myReferer)) request.Referer = myReferer;
                if (!string.IsNullOrWhiteSpace(myReferer)) request.UserAgent = myUserAgent;//浏览器

                request.Method = "GET";//请求方式
                request.ContentType = "text/html;charset=UTF-8";//类型
                request.Timeout = 3000;//超时

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream stream = response.GetResponseStream();
                string responseStr = "";
                if (stream != null)
                {
                    StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                    responseStr = UnicodeToGB(reader.ReadToEnd());
                    reader.Close();
                }
                response.Close();

                return responseStr;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }


        /// <summary>
        /// Post提交数据
        /// </summary>
        /// <param name="myUrl">网址</param>
        /// <param name="myParam">参数</param>
        /// <param name="myProxy">IP代理</param>
        /// <param name="myUserAgent">浏览器</param>
        /// <param name="myReferer">来源</param>
        /// <returns></returns>
        public string HttpPostData(string myUrl, string myParam, WebProxy myProxy = null, string myUserAgent = "", string myReferer = "")
        {
            try
            {
                //创建请求
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUrl);
                if (myProxy != null) request.Proxy = myProxy; //设置代理
                if (!string.IsNullOrWhiteSpace(myReferer)) request.Referer = myReferer;
                if (!string.IsNullOrWhiteSpace(myReferer)) request.UserAgent = myUserAgent;//浏览器

                request.Method = "POST";//请求方式
                request.ContentType = "application/json;charset=UTF-8";//类型     
                request.Timeout = 3000;//超时

                byte[] payload = Encoding.UTF8.GetBytes(myParam);//参数编码
                request.ContentLength = payload.Length;

                //发送post的请求,写入参数
                Stream writer = request.GetRequestStream();
                writer.Write(payload, 0, payload.Length);
                writer.Close();

                //接受返回来的数据
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream stream = response.GetResponseStream();

                string responseStr = "";
                if (stream != null)
                {
                    StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                    responseStr = UnicodeToGB(reader.ReadToEnd());
                    reader.Close();
                }
                response.Close();

                return responseStr;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

  

posted @ 2021-06-05 10:22  懒人境界  阅读(489)  评论(0编辑  收藏  举报