.Net 6.0 发送http请求

/// 
        /// GET方法
        /// 
        /// 链接
        /// 参数,?xx=
        /// 超时时间
        /// 
        public string GetSendMessage(string url, string param, double timeout = 10.0)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.Timeout = TimeSpan.FromSeconds(timeout);

                return httpClient.GetStringAsync(url + param).Result;
            }
        }

        /// 
        /// Post方法
        /// 
        /// 链接
        /// 参数,?xx=
        /// 超时时间
        /// 
        public string PostSendMessage(string url, string param, double timeout = 10.0)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.Timeout = TimeSpan.FromSeconds(timeout);

                //发送的内容
                HttpContent content = new StringContent(string.Empty);
                content.Headers.ContentType =
                    new MediaTypeHeaderValue("application/json");

                return httpClient.PostAsync(url + param, null).Result.Content.ReadAsStringAsync().Result;
            }
        }

你可能感兴趣的:(.net,http)