【C#】post请求

public static string PostHttpResponse(string url, Encoding encoding, string parameters)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }
            if (encoding == null)
            {
                throw new ArgumentNullException("requestEncoding");
            }
            HttpWebRequest request = null;
            //如果是发送HTTPS请求
            if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                request = WebRequest.Create(url) as HttpWebRequest;
                request.ProtocolVersion = HttpVersion.Version10;
            }
            else
            {
                request = WebRequest.Create(url) as HttpWebRequest;
            }
            request.Method = "POST";
            request.ContentType = "application/json";
            SetHeaderValue(request.Headers, "request-id", "11"); //设置header信息
            SetHeaderValue(request.Headers, "token", "KRu1UtVTNtNruDJR2x%2fkrDXn0vOCOFgYoTa%2bLsuflWmnN7InkdFHOKR2DQGazzQx");
            //request.UserAgent = DefaultUserAgent;
            //如果需要POST数据
            if (!string.IsNullOrEmpty(parameters))
            {
                byte[] data = encoding.GetBytes(parameters);
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }

            StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream(), encoding);
            string responseData = reader.ReadToEnd().ToString().Trim();
            return responseData;
        }
        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true; //总是接受
        }
        private static void SetHeaderValue(WebHeaderCollection header, string name, string value)
        {
            var property = typeof(WebHeaderCollection).GetProperty("InnerCollection", BindingFlags.Instance | BindingFlags.NonPublic);
            if (property != null)
            {
                var collection = property.GetValue(header, null) as NameValueCollection;
                collection[name] = value;
            }
        }

调用

string url = "xxxx";
            JObject jobject = new JObject();
            jobject["name"] = "33333444";
            jobject["expire_from"] = "20200304";
            jobject["expire_to"] = "20200307";
            jobject["remark"] = "请在过期前使用";
            jobject["small_image_url"] = "https://pics1.baidu.com/feed/d058ccbf6c81800a6656267dfc3676fc828b471f.jpeg?token=9064aefb59dbc42ecddd82c1213dfa1b&s=EA9005C325223519525C64BA0300A010";
            jobject["large_image_url"] = "https://pics1.baidu.com/feed/d058ccbf6c81800a6656267dfc3676fc828b471f.jpeg?token=9064aefb59dbc42ecddd82c1213dfa1b&s=EA9005C325223519525C64BA0300A010";
            string result = PostHttpResponse(url, Encoding.UTF8, Newtonsoft.Json.JsonConvert.SerializeObject(jobject));

 

你可能感兴趣的