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));
  • 相关阅读:
    2019-10-14-云之幻-UWP-视频教程
    2019-10-14-云之幻-UWP-视频教程
    2018-2-13-win10-uwp-自定义控件-SplitViewItem
    2018-2-13-win10-uwp-自定义控件-SplitViewItem
    2019-9-19-dotnet-找不到-PostAsJsonAsync-方法
    2019-9-19-dotnet-找不到-PostAsJsonAsync-方法
    2018-5-28-WPF-Process.Start-出现-Win32Exception-异常
    2018-5-28-WPF-Process.Start-出现-Win32Exception-异常
    Java实现 LeetCode 606 根据二叉树创建字符串(遍历树)
    Java实现 LeetCode 606 根据二叉树创建字符串(遍历树)
  • 原文地址:https://www.cnblogs.com/linhuide/p/12368302.html
Copyright © 2011-2022 走看看