0
点赞
收藏
分享

微信扫一扫

c# PUT POST HTTPS 基础连接已经关闭: 发送时发生错误

小布_cvg 2023-01-09 阅读 47


public void Put()
{
string data = "";
//Web访问对象,构造请求的url地址
string serviceUrl = string.Format("https://");
//构造http请求的对象
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //一定要有这一句

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
//转成网络流
byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
//设置

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;


myRequest.Method = "PUT";
myRequest.ContentLength = buf.Length;
myRequest.ContentType = "application/json";
myRequest.MaximumAutomaticRedirections = 1;
myRequest.AllowAutoRedirect = true;
// 发送请求
Stream newStream = myRequest.GetRequestStream();
newStream.Write(buf, 0, buf.Length);
newStream.Close();
// 获得接口返回值
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string ReturnXml = reader.ReadToEnd();
reader.Close();
myResponse.Close();
Key.token= ReturnXml;
}

 

举报

相关推荐

0 条评论