方案:我正在使用浏览器启动身份验证流程。用户进行身份验证后,浏览器将重定向到http://localhost:1234,我在听。然后,我从URI中提取一个身份验证代码。
问题是有时TCPListner.AcceptTcpClientAsync()
返回一个TcpClient,表明已建立连接,但没有数据?我应该重复打TCPListner.AcceptTcpClientAsync
吗?
listener.Start();
// Gets triggered when an incomming connection occurs
TcpClient client = await listener.AcceptTcpClientAsync().ConfigureAwait(false);
NetworkStream networkStream = client.GetStream();
// But sometimes data just isn't available?
Console.WriteLine(" -> DataAvailable? " + networkStream.DataAvailable);
// Standard pattern of reading data from a stream
byte[] readBuffer = new byte[1024];
StringBuilder stringBuilder = new StringBuilder();
do
{
// this just waits indefinitely as no timeout is set
int numberOfBytesRead = await networkStream.ReadAsync(readBuffer, 0, readBuffer.Length, ct)
.ConfigureAwait(false);
string s = Encoding.ASCII.GetString(readBuffer, 0, numberOfBytesRead);
stringBuilder.Append(s);
}
while (networkStream.DataAvailable);
此代码似乎在Edge以外的大多数浏览器中都可以正常工作,其中Edge大约有50%的时间AcceptTcpClientAsync
返回TcpClient,但是该客户端没有数据。
我已经使用TcpView检查了网络,似乎正在形成多个连接,其中一个连接发送了0个字节。