zoukankan      html  css  js  c++  java
  • 记录Post Form Data

    if (string.IsNullOrEmpty(ConfigMgr.WxPlatformToKen))
                {
                    string result = string.Empty;
                    CommonConfigWxPlatformElement wxPlatformConfig = CommonConfigManager.Instance.Root.WxPlatform;
    
                    if (!string.IsNullOrEmpty(wxPlatformConfig.BaseUrl) && wxPlatformConfig.BaseUrl.StartsWith("https",StringComparison.OrdinalIgnoreCase))
                    {
                        System.Console.WriteLine("https connections.....");
                        ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
                        // 这里设置了协议类型。
                        //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;// (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2; 
                        ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072; //(SecurityProtocolType)768 | (SecurityProtocolType)3072
    
                        ServicePointManager.CheckCertificateRevocationList = true;
                        ServicePointManager.DefaultConnectionLimit = 100;
                        ServicePointManager.Expect100Continue = false;
                    }
    
                    Uri urlUri = new Uri(wxPlatformConfig.BaseUrl + wxPlatformConfig.TokenPath);
    
                    NameValueCollection nvc = new NameValueCollection();
                    nvc.Add("grant_type", "password");
                    nvc.Add("username", wxPlatformConfig.TokenUser);
                    nvc.Add("password", wxPlatformConfig.TokenPwd);
    
                    string boundary = CreateFormDataBoundary();                                       // 边界符
                    byte[] beginBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "
    ");     // 边界符开始。【☆】右侧必须要有 
     。
                    byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "--
    "); // 边界符结束。【☆】两侧必须要有 --
     。
                    byte[] newLineBytes = Encoding.UTF8.GetBytes("
    "); //换一行
    
    
                    HttpWebRequest httpWebRequest = null;
                    try
                    {
                        httpWebRequest = WebRequest.Create(urlUri) as HttpWebRequest; // 创建请求
                        httpWebRequest.Headers.Add("Authorization", wxPlatformConfig.TokenSalt);
                        httpWebRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
                        httpWebRequest.Method = WebRequestMethods.Http.Post;
                        httpWebRequest.KeepAlive = true;
                        httpWebRequest.Timeout = -1;
                        httpWebRequest.ServicePoint.Expect100Continue = false;
                        httpWebRequest.Proxy = null;
    
                        string formDataTemplate = "Content-Disposition: form-data; name="{0}"
    
    " + "{1}
    ";
                        MemoryStream memoryStream = new MemoryStream();
                        foreach (string key in nvc.Keys)
                        {
                            string formItem = string.Format(formDataTemplate, key, nvc[key]);
                            byte[] formItemBytes = Encoding.UTF8.GetBytes(formItem);
    
                            memoryStream.Write(beginBoundaryBytes, 0, beginBoundaryBytes.Length); // 1.1 写入FormData项的开始边界符
                            memoryStream.Write(formItemBytes, 0, formItemBytes.Length);           // 1.2 将键值对写入FormData项中
                        }
    
                        memoryStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);             // 2.4 写入FormData的结束边界符
    
                        httpWebRequest.ContentLength = memoryStream.Length;
    
                        Stream requestStream = httpWebRequest.GetRequestStream();
    
                        memoryStream.Position = 0;
                        byte[] tempBuffer = new byte[memoryStream.Length];
                        memoryStream.Read(tempBuffer, 0, tempBuffer.Length);
                        memoryStream.Close();
    
                        requestStream.Write(tempBuffer, 0, tempBuffer.Length);        // 将内存流中的字节写入 httpWebRequest 的请求流中
                        requestStream.Close();
    
                        HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; // 获取响应
                        if (httpWebResponse != null)
                        {
                            //GetHeaders(ref httpResult, httpWebResponse);
    
                            Stream stream2 = httpWebResponse.GetResponseStream();
                            StreamReader reader2 = new StreamReader(stream2);
                            result = reader2.ReadToEnd();
                            httpWebResponse.Close();
    
                            var obj = Common.JsonToObject(result);
                            if (obj != null && obj.access_token != null)
                            {
                                ConfigMgr.WxPlatformToKen = obj.access_token.ToString();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        HHTech.Log.Error("GetTonken Error", ex);
                    }
  • 相关阅读:
    CSS filter属性
    css过渡的使用
    关于2D、3D的转换
    3D立体旋转
    css的规范命名
    html标签的简单总结
    css之简单动画
    几种动态的学习链接
    css之规范命名
    css
  • 原文地址:https://www.cnblogs.com/DoNetCShap/p/14310194.html
Copyright © 2011-2022 走看看