HttpClient Post示例

 var url = "http://11.4.64.77:8888/a/b";
            if (url.StartsWith("https"))
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

            foreach (var item in replyList)
            {
                try
                {
                    HttpContent httpContent = new StringContent(JsonUtil.Stringify(new
                    {
                        flightId = item.FlightId,
                        userId = user.Id,
                        etd = item.ReplyStd,
                        isEtdNotice = item.Notice
                    }));
                    httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    httpContent.Headers.Add("APIKey", "jjj");
                    HttpClient httpClient = new HttpClient();

                    HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;

                    if (response.IsSuccessStatusCode)
                    {
                        string result = response.Content.ReadAsStringAsync().Result;
                        item.SendFail = !(result != null && result.Contains("OK"));
                    }
                }
                catch (Exception)
                {
                    item.SendFail = true;
                }

  

posted @ 2020-10-15 16:07  狂乱贵公子  阅读(166)  评论(0编辑  收藏  举报