C# 在winc应用程序中使用SSL
我需要使用SSL连接到服务器 说明: 我正在从事一个windows应用程序项目,其中我们使用Web服务与数据库进行数据通信。 服务器端服务是用Java编写的,我正在用C编写UI部分。现在我想使用SSL与服务器建立安全连接,但我找不到跳过尝试使用C代码建立连接时产生的警告消息的方法 一个样品对我很有帮助 这是我使用的代码:C# 在winc应用程序中使用SSL,c#,ssl,C#,Ssl,我需要使用SSL连接到服务器 说明: 我正在从事一个windows应用程序项目,其中我们使用Web服务与数据库进行数据通信。 服务器端服务是用Java编写的,我正在用C编写UI部分。现在我想使用SSL与服务器建立安全连接,但我找不到跳过尝试使用C代码建立连接时产生的警告消息的方法 一个样品对我很有帮助 这是我使用的代码: public bool UserLogIn ( User user ) { logon logIn = new logon(); lo
public bool UserLogIn ( User user )
{
logon logIn = new logon();
logIn.arg0 = new userAthenticationRq();
logIn.arg0.credential = GetCredential(user);
logIn.arg0.clientIpAddress = GetClientIP();
logIn.arg0.requstDate = DateTime.Now;
logIn.arg0.requstDateSpecified = true;
logIn.arg0.userName = user.Name;
logonResponse rsp = new logonResponse();
ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateRemoteCertificate);
rsp = service.logon(logIn);//this is where the Exception Occurs
if (rsp.@return.statusCode == statusCodeEnum.SUCCESS)
.
.
.
}
如果是关于无效SSL证书的错误,您可以使用以下方法绕过它: 将此添加到您的代码中
// callback used to validate the certificate in an SSL conversation
private static bool ValidateRemoteCertificate(object sender, X509Certificate certificate,
X509Chain chain, SslPolicyErrors policyErrors)
{
return true;
}
然后添加此项以调用它并接受所有证书:
// Adds validation of SSL Certificates
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);
这是关于无效SSL证书的错误吗?请参阅,您不需要将电子邮件作为问题的一部分发布。我已经取消了…谢谢你的回答,我使用了这个代码,但是当我尝试调用该服务时,我得到一个异常WebException,并显示一条错误消息,比如请求失败,响应为空。你使用的是https还是http地址?看看这里,我正在使用Http…当我把它改成Https时,它工作了!!!显然问题已经解决了。谢谢