WebSocket
APIにC#コンソールアプリケーションから接続しようとしています。ClientWebSocket.ConnectAsyncがエラー情報なしでクラッシュする
ConnectAsync
メソッドでコードがクラッシュし、catch block
にあるか、エラーが発生します。ここで
public async System.Threading.Tasks.Task<Details> Get(string locationUid, string eventType)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls |
SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
var cancellationTokenSource = new CancellationTokenSource();
using (ClientWebSocket clientWebSocket = new ClientWebSocket())
{
Uri serverUri = new Uri(Endpoint.WebSocketUrl);
try
{
await clientWebSocket.ConnectAsync(serverUri, cancellationTokenSource.Token);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
while (clientWebSocket.State == WebSocketState.Open)
{
string bodyMessage = $"{{\"locationUid\":\"{locationUid}\",\"eventTypes\":[\"{eventType}\"]}}";
ArraySegment<byte> bytesToSend = new ArraySegment<byte>(Encoding.UTF8.GetBytes(bodyMessage));
await clientWebSocket.SendAsync(bytesToSend, WebSocketMessageType.Text, true, CancellationToken.None);
ArraySegment<byte> bytesReceived = new ArraySegment<byte>(new byte[1024]);
WebSocketReceiveResult result = await clientWebSocket.ReceiveAsync(bytesReceived, CancellationToken.None);
var response = Encoding.UTF8.GetString(bytesReceived.Array, 0, result.Count);
}
}
return null;
}
アプリケーションでも、私はとして
clientWebSocket.ConnectAsync(serverUri, cancellationTokenSource.Token).Wait(cancellationTokenSource.Token);
以下にconnectAsyncラインを変更し
catchブロックに該当しないawait clientWebSocket.ConnectAsync(serverUri, cancellationTokenSource.Token);
で墜落した私のコードですブロック例外:
The request was aborted: Could not create SSL/TLS secure channel.
スタックトレース
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.WebSockets.ClientWebSocket.<ConnectAsyncCore>d__21.MoveNext()
方法は
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls |
SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
新しい例外
Unable to connect to the remote server The remote server returned an error: (400) Bad Request.
スタックトレースを開始する前に、それから私は、次の行を追加しました:
を
これを解決しましたか? –
はい。私の答えを今投稿する。 – HaBo