POST请求

/// <summary>
        /// POST请求获取信息
        /// </summary>
        /// <param name="url"></param>
        /// <param name="paramData"></param>
        /// <returns></returns>
        public string POST(string url, string paramData, int timeout = 5000, List<System.Net.Cookie> cookies = null)
        {
            string ret = string.Empty;
            try
            {
                byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(paramData); //转化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
                if (url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate); //rcvc;
                    webReq.ProtocolVersion = HttpVersion.Version10;
                }
                SetProxy(ref webReq);
                webReq.Method = "POST";
                webReq.ContentType = "application/json; charset=utf-8";
                webReq.ServicePoint.Expect100Continue = false;
                //webReq.ContentType = "text/html;charset=utf-8";
                webReq.Timeout = timeout;
                webReq.ContentLength = byteArray.Length;

                if (!string.IsNullOrEmpty(UserAgent))
                    webReq.UserAgent = UserAgent;
                if (cookies != null && cookies.Count > 0)
                {
                    webReq.CookieContainer = new CookieContainer();

                    string host = new Uri(url).Host;
                    foreach (System.Net.Cookie c in cookies)
                    {
                        c.Domain = host;
                        webReq.CookieContainer.Add(c);
                    }
                }
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
            }
            catch (Exception ex)
            {
                ClassLoger.Error(ex, "HttpUtils/POST", url);
                throw ex;
            }
            return ret;
        }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-20
  • 2021-08-30
  • 2021-08-14
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案