记录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 + "\r\n"); // 边界符开始。【☆】右侧必须要有 \r\n 。 byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n"); // 边界符结束。【☆】两侧必须要有 --\r\n 。 byte[] newLineBytes = Encoding.UTF8.GetBytes("\r\n"); //换一行 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}\"\r\n\r\n" + "{1}\r\n"; 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); }
本文来自博客园,作者:jevan,转载请注明原文链接:https://www.cnblogs.com/DoNetCShap/p/14310194.html
分类:
C#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端