C# WebRequest 请求HTTPS 基础连接已关闭:发送时发生错误,远程主机强迫关闭了一个现有有连接
2022/6/16 1:21:35
本文主要是介绍C# WebRequest 请求HTTPS 基础连接已关闭:发送时发生错误,远程主机强迫关闭了一个现有有连接,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
internal static class SendRequest { private static string GetBody(Dictionary<string, string> senddata) { if (senddata == null) { senddata = new Dictionary<string, string>(); } StringBuilder buffer = new StringBuilder(); int i = 0; foreach (string k in senddata.Keys) { if (i > 0) { buffer.AppendFormat("&{0}={1}", k, senddata[k]); } else { buffer.AppendFormat("{0}={1}", k, senddata[k]); } i++; } string bodys = buffer.ToString(); return bodys; } public static string Send(string url, Dictionary<string, string> headerdata, Dictionary<string, string> senddata) { return Send(url, "POST", headerdata, senddata, "application/x-www-form-urlencoded;charset=UTF-8"); } public static string Send(string url, string method, Dictionary<string, string> headerdata, Dictionary<string, string> senddata) { return Send(url, method, headerdata, senddata, "application/x-www-form-urlencoded;charset=UTF-8"); } public static string Send(string url, string method, Dictionary<string, string> headerdata, Dictionary<string, string> senddata, string contentType) { string bodys = GetBody(senddata); //new internalErrorLog(new Exception(), "SendBody:" + bodys); HttpWebRequest httpRequest = null; HttpWebResponse httpResponse = null; if (url.Contains("https://")) { //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; /* 请求某些接口一直返回基础连接已关闭:发送时发生错误 远程主机强迫关闭了一个现有的连接 */ ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; ServicePointManager.Expect100Continue = false; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url)); httpRequest.ProtocolVersion = HttpVersion.Version10; } else { httpRequest = (HttpWebRequest)WebRequest.Create(url); } httpRequest.Proxy = null; httpRequest.Method = method; foreach (string k in headerdata.Keys) { httpRequest.Headers.Add(k, headerdata[k]); } //new internalErrorLog(new Exception(), "SendHeader:" + Newtonsoft.Json.JsonConvert.SerializeObject(headerdata)); //根据API的要求,定义相对应的Content-Type httpRequest.ContentType = contentType; if (0 < bodys.Length) { byte[] data = Encoding.UTF8.GetBytes(bodys); using (Stream stream = httpRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } } try { httpRequest.ServicePoint.Expect100Continue = false; httpResponse = (HttpWebResponse)httpRequest.GetResponse(); Stream st = httpResponse.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); string json = reader.ReadToEnd(); return json; } catch (WebException ex) { new internalErrorLog(ex, "请求URL:" + url); httpResponse = (HttpWebResponse)ex.Response; return ex.Message; } } private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; } public static string SendGet(string url, Dictionary<string, string> headerdata) { HttpWebRequest httpRequest = null; HttpWebResponse httpResponse = null; if (url.Contains("https://")) { ServicePointManager.SecurityProtocol =(SecurityProtocolType)3072; ServicePointManager.Expect100Continue = false; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url)); httpRequest.ProtocolVersion = HttpVersion.Version10; } else { httpRequest = (HttpWebRequest)WebRequest.Create(url); } httpRequest.Proxy = null; httpRequest.Method = "Get"; httpRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; foreach (string k in headerdata.Keys) { httpRequest.Headers.Add(k, headerdata[k]); } try { httpResponse = (HttpWebResponse)httpRequest.GetResponse(); Stream st = httpResponse.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); string json = reader.ReadToEnd(); return json; } catch (WebException ex) { httpResponse = (HttpWebResponse)ex.Response; return ex.Message; } } public static string SendJSON(string url, JObject objdata) { string jsondata = objdata.ToString(); return SendJSON(url, jsondata); } public static string SendJSON(string url, string jsondata) { HttpWebRequest httpRequest = null; HttpWebResponse httpResponse = null; if (url.Contains("https://")) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls; ServicePointManager.Expect100Continue = false; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url)); httpRequest.ProtocolVersion = HttpVersion.Version10; httpRequest.KeepAlive = false; ServicePointManager.DefaultConnectionLimit = 500; } else { httpRequest = (HttpWebRequest)WebRequest.Create(url); } httpRequest.Proxy = null; httpRequest.Method = "POST"; httpRequest.ContentType = "application/json"; //WebClient.UploadString("", jsondata); try { httpRequest.ServicePoint.Expect100Continue = false; byte[] buffer = Encoding.UTF8.GetBytes(jsondata); //new internalErrorLog(new Exception(jsondata)); httpRequest.ContentLength = buffer.Length; httpRequest.GetRequestStream().Write(buffer, 0, buffer.Length); httpResponse = (HttpWebResponse)httpRequest.GetResponse(); Stream st = httpResponse.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); string json = reader.ReadToEnd(); return json; } catch (WebException ex) { new internalErrorLog(ex, "发送地址:" + url); httpResponse = (HttpWebResponse)ex.Response; return ex.Message; } } public static string SendJSON(string url, Dictionary<string, string> header, string jsondata) { HttpWebRequest httpRequest = null; HttpWebResponse httpResponse = null; if (url.Contains("https://")) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls; ServicePointManager.Expect100Continue = false; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url)); } else { httpRequest = (HttpWebRequest)WebRequest.Create(url); } httpRequest.Method = "POST"; httpRequest.ContentType = "application/json"; foreach (string k in header.Keys) { httpRequest.Headers.Add(k, header[k]); } //WebClient.UploadString("", jsondata); try { httpRequest.ServicePoint.Expect100Continue = false; byte[] buffer = Encoding.UTF8.GetBytes(jsondata); new internalErrorLog(new Exception(jsondata)); httpRequest.ContentLength = buffer.Length; httpRequest.GetRequestStream().Write(buffer, 0, buffer.Length); httpResponse = (HttpWebResponse)httpRequest.GetResponse(); Stream st = httpResponse.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); string json = reader.ReadToEnd(); return json; } catch (WebException ex) { httpResponse = (HttpWebResponse)ex.Response; return ex.Message; } } public static string Submit(string url, Hashtable ht, string type) { return Submit(url, ht, type, Encoding.Default); } public static string Submit(string url, NameValueCollection ht, string type) { return Submit(url, ht, type, Encoding.Default); } public static string Submit(string url, Hashtable ht, string type, Encoding encoding) { IDictionaryEnumerator enumerator = ht.GetEnumerator(); NameValueCollection keyValue = new NameValueCollection(); while (enumerator.MoveNext()) { keyValue.Add(enumerator.Key.ToString(), (enumerator.Value == null) ? "" : enumerator.Value.ToString()); } return Submit(url, keyValue, type, encoding); } private static string Submit(string url, NameValueCollection keyValue, string type, Encoding encoding) { string str = string.Empty; WebClient client = new WebClient(); try { byte[] bytes = client.UploadValues(url, type.ToString(), keyValue); str = encoding.GetString(bytes); } catch (Exception exception) { throw exception; } finally { client.Dispose(); } return str; } }
这篇关于C# WebRequest 请求HTTPS 基础连接已关闭:发送时发生错误,远程主机强迫关闭了一个现有有连接的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具
- 2024-01-24.NET集成IdGenerator生成分布式全局唯一ID
- 2024-01-23用CI/CD工具Vela部署Elasticsearch + C# 如何使用
- 2024-01-23.NET开源的简单、快速、强大的前后端分离后台权限管理系统