带SSL的C#Websocket客户端 [英] C# Websocket client with SSL

查看:1172
本文介绍了带SSL的C#Websocket客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我想问一下,有人曾在C#中编写过Web套接字客户端,例如:控制台应用。我确实找到了一些外部库,但要么他们没有SSL支持,要么没有任何区别。我使用的是.NET 3.5,我无法改变它。不幸的是我的客户端正在使用WebSocket,我别无选择。

解决方案

要发送请求并收到回复,您可以使用:

 HttpWebRequest req =(HttpWebRequest)WebRequest.Create(url); 





其中url是查询字符串https:/ / ...。



与SSL的主要区别在于您必须验证证书:



  //  在您的代码中只需要一次的任何请求之前 
// 你可以放在应用程序的开头

RemoteCertificateValidationCallback remote = ValidateServerCertificate;
ServicePointManager.ServerCertificateValidationCallback = remote;


public bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true ; // 始终接受......真的应该检查
}


Hello,
I would like to ask of anyone ever wrote web socket client in C# as e.g. console application. I did find some external libraries but either they had no SSL support or it didn't make any difference. I'm using .NET 3.5 and I can't change it. Unfortunately my client is using WebSocket and I have no other choice.

解决方案

To send requests and receive responses you can use:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);



where url is the query string "https://...".

The main difference with SSL is you must verify the certificate:

//before any request you need this only once in your code
//you can put in beginning of you app

RemoteCertificateValidationCallback remote = ValidateServerCertificate;
ServicePointManager.ServerCertificateValidationCallback = remote;
                

public bool ValidateServerCertificate(
            object sender,
            X509Certificate certificate, 
            X509Chain chain,
            SslPolicyErrors sslPolicyErrors)
        {
            return true; //always accept... really should check it
        }


这篇关于带SSL的C#Websocket客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆