基础连接已关闭:(HttpWebRequest)-C#

基础连接已关闭:(HttpWebRequest)-C#,c#,authentication,post,get,httpwebrequest,C#,Authentication,Post,Get,Httpwebrequest,我正在编写一个代码,通过POST请求验证用户名和密码,但我收到一个错误,它说“基础连接已关闭” 我正在尝试将带有GET请求的旧代码转换为带有POST请求的新代码 我的GET代码工作正常(旧代码): 引发错误的我的POST请求代码(新代码): 错误: 因此,添加回ServicePointManager.ServerCertificateValidationCallback。由于它是一个身份验证服务,我假设它使用的是Https连接。否则,为什么要设置ServicePointManager.Secur

我正在编写一个代码,通过POST请求验证用户名和密码,但我收到一个错误,它说“基础连接已关闭”

我正在尝试将带有GET请求的旧代码转换为带有POST请求的新代码

我的GET代码工作正常(旧代码): 引发错误的我的POST请求代码(新代码): 错误:
因此,添加回
ServicePointManager.ServerCertificateValidationCallback
。由于它是一个身份验证服务,我假设它使用的是
Https
连接。否则,为什么要设置
ServicePointManager.SecurityProtocol
(对于
Http
连接)呢。如果您实际上不需要在新证书上验证pass的服务器证书,只需返回
true
。首先要了解的是:使用服务器http或https。如果http:不需要SecureProtocol或证书。如果https:将url更改为https,则添加ServerCertificateValidationCallback,如@jimi所述。@H.G.Sandhagen我正在使用https进行请求。我在评论中更新了这一点
string url = "https://www.example.com/?username=" + username + "&password=" + password;

XmlDocument xmldoc = new XmlDocument();
ServicePointManager.ServerCertificateValidationCallback += new 
RemoteCertificateValidationCallback(customXertificateValidation);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

xmldoc.Load(url);
XmlNode name = xmldoc.SelectSingleNode("/Node/Name");
var request = (HttpWebRequest)WebRequest.Create("https://www.example.com/authenticate.aspx");
ServicePointManager.ServerCertificateValidationCallback += new 
RemoteCertificateValidationCallback(customXertificateValidation);    
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
var postData = "user=username";
postData += "&pass=password";
var data = Encoding.ASCII.GetBytes(postData);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;

using (var stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}

var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.ComponentModel.Win32Exception: The client and server cannot communicate, because they do not possess a common algorithm
at System.Net.SSPIWrapper.AcquireCredentialsHandle(SSPIInterface SecModule, String package, CredentialUse intent, SecureCredential scc)
at System.Net.Security.SecureChannel.AcquireCredentialsHandle(CredentialUse credUsage, SecureCredential& secureCredential)
at System.Net.Security.SecureChannel.AcquireClientCredentials(Byte[]& thumbPrint)
at System.Net.Security.SecureChannel.GenerateToken(Byte[] input, Int32 offset, Int32 count, Byte[]& output)
at System.Net.Security.SecureChannel.NextMessage(Byte[] incoming, Int32 offset, Int32 count)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.ConnectStream.WriteHeaders(Boolean async)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
at OdlumBrown.Members.AuthenticateUser(String username, String password) in c:\Website\example.com\App_Code\Membership.cs:line 148