C#發送post請求

概述 

   c#的net的4.5版本,支持的ssl協議為1.2,發送post請求,並接收

                //System.Net.ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
                            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

                            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("");

                            request.Method = "POST";
//攜帶請求頭
                            request.Headers[""] = "";
                            request.Headers[""] = "";
                            request.ContentType = "application/json";
                            string requestBody = total.ToString();
                            byte[] bodyBytes = Encoding.UTF8.GetBytes(requestBody);

                            // 发送请求
                            using (Stream requestStream = request.GetRequestStream())
                            {
                                requestStream.Write(bodyBytes, 0, bodyBytes.Length);
                            }

                            // 接收响应
                            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                            {
                                using (Stream responseStream = response.GetResponseStream())
                                {
                                    using (StreamReader reader = new StreamReader(responseStream))
                                    {
                                        string responseBody = reader.ReadToEnd();
                                        JObject jsonObject = JObject.Parse(responseBody);
                                        JObject job = new JObject(
                                           
                                            new JProperty("result", jsonObject));
 
                                        }

                                    }
                                }
                            }

你可能感兴趣的:(C#,c#,java,前端)