码迷,mamicode.com
首页 > Windows程序 > 详细

【C#】post请求

时间:2020-02-26 18:56:54      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:ota   xxxx   ati   bytes   error   job   requests   policy   write   

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));

 

【C#】post请求

标签:ota   xxxx   ati   bytes   error   job   requests   policy   write   

原文地址:https://www.cnblogs.com/linhuide/p/12368302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!