zoukankan      html  css  js  c++  java
  • 【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));
  • 相关阅读:
    BZOJ-2462: [BeiJing2011]矩阵模板 (宇宙无敌超级大暴力~)
    BZOJ-3555: [Ctsc2014]企鹅QQ (hash)
    BZOJ-3098: Hash Killer II (未知)
    [SinGuLaRiTy] 2017 百度之星程序设计大赛 初赛A
    [SinGuLaRiTy] 树链问题
    [SinGuLaRiTy] 2017 百度之星程序设计大赛-资格赛
    [SinGuLaRiTy] NOIP模拟赛(TSY)-Day 2
    [SinGuLaRiTy] NOIP模拟赛(TSY)-Day 1
    [SinGuLaRiTy] 2017-07-26 综合性测试
    [SinGuLaRiTy] NOIP 膜你赛-Day 2
  • 原文地址:https://www.cnblogs.com/linhuide/p/12368302.html
Copyright © 2011-2022 走看看