SslStream を使用して Web サイトに接続し、リクエストを渡そうとしていますが、次の IOException が発生します。
トランスポート接続からデータを読み取ることができません: 接続先が一定時間後に適切に応答しなかったために接続の試行が失敗したか、接続されたホストが応答しなかったために確立された接続が失敗しました。
これをより効率的にテストするために、スタンドアロン プログラムを作成し、ここからクライアント コードのサンプル コードをコピーしました。 .110).aspxを作成し、相手側の API に基づいて要求を渡しました。
string Request = "The Request";
TcpClient client = new TcpClient();
client.ReceiveTimeout = 10000; //10 seconds
SslStream sslStream;
string server = "ssl.website.com";
client.Connect(server, 12345);
sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null );
sslStream.AuthenticateAsClient(server,null, SslProtocols.Tls12, false);
StringBuilder messageData = new StringBuilder();
byte[] len = BitConverter.GetBytes(SslClient.GetLen(Request));
messageData.AppendLine("Writing length of data " + SslClient.GetLen(Request).ToString());
sslStream.Write(len);
byte[] msg = Encoding.UTF8.GetBytes(Request);
sslStream.Write(msg);
sslStream.Flush();
byte[] buffer = new byte[2048];
messageData.AppendLine("Writing data " + System.Text.Encoding.UTF8.GetString(msg));
messageData.AppendLine("Is Authenticated? " + sslStream.IsAuthenticated.ToString());
int bytes = -1;
try
{
do
{
bytes = sslStream.Read(buffer, 0, buffer.Length);
Decoder decoder = Encoding.UTF8.GetDecoder();
char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)];
decoder.GetChars(buffer, 0, bytes, chars, 0);
messageData.AppendLine(chars.ToString());
if (messageData.ToString().IndexOf("<EOF>") != -1)
{
break;
}
} while (bytes != 0);
}
catch(Exception ex)
{
messageData.AppendLine(ex.Message);
}
return messageData.ToString();
}
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
return false;
}
public static Int16 GetLen(string Request)
{
return Convert.ToInt16(Encoding.UTF8.GetBytes(Request).Length);
}
失敗している部分はsslStream.Read(buffer, 0, buffer.Length)です。これは決して返されず、IOException の発生元です。
私が接続しようとしている人々は、SSL ハンドシェイクが失敗していると言っていますが、私たちの側ではその兆候は見られません。常に sslStread.Read になり、失敗します。彼らは時々、リクエストを受け取ってレスポンスを送信していると言っていますが、レスポンスが返されるのをまだ見たことがありません.
ありがとう