我正在尝试为 .nl 注册商 SIDN 的项目制作 EPP XML 客户端。该项目是在 Windows 窗体中创建的。一旦我点击一个按钮,客户端必须先连接到服务器,然后发送问候语,然后登录。我尝试了很多,但似乎没有任何效果。我也在这个线程上尝试了接受的答案,但没有给出示例,所以我不确定如何
连接。阅读响应(您收到问候语)发送登录命令。读取响应(您得到登录响应)发送命令。阅读响应。发送断开连接。阅读响应。
需要实施。
连接后我从服务器收到了一个问候,但是一旦我发送了一个问候,ReadMessage 方法上的代码就会冻结。我究竟做错了什么?
这是我的代码:
private void button3_Click(object sender, EventArgs e)
{
//Connect with registry and read server greet
if (Connect(new Uri("https://drs.domain-registry.nl:700")))
{
//Send greeting back to the server and read server response
if (Command(Greeting()))
{
//Send login command to the server and read server response
if (Command(Login()))
{
Console.WriteLine("Successfully logged in...");
}
}
}
}
private static XDocument Greeting()
{
var ns = XNamespace.Get("urn:ietf:params:xml:ns:epp-1.0");
var doc = new XDocument(new XDeclaration("1.0", "utf-8", "no"));
var root = new XElement(ns + "epp", new XElement(ns + "hello"));
doc.Add(root);
return doc;
}
private string ReadMessage()
{
// The first four bytes will be the the content length as a network order (big-endian) 32-bit number.
var lengthBytes = new byte[4];
_sslStream.Read(lengthBytes, 0, 4);
Array.Reverse(lengthBytes);
var length = BitConverter.ToInt32(lengthBytes, 0) - 4;
// Create a byte array of the correct size for the response.
var messageBytes = new byte[length];
var returned = 0;
while (returned != length)
{
returned += _sslStream.Read(messageBytes, 0, length);
}
return Encoding.UTF8.GetString(messageBytes);
}
public bool Command(XContainer requestData)
{
//Encode to byte[]
byte[] bytedata = Encoding.UTF8.GetBytes(requestData.ToString());
//Get the request stream
_sslStream.Write(bytedata, 0, bytedata.Length);
//Flush after writing
_sslStream.Flush();
//Read server response from our _sslStream
var response = ReadMessage();
//Not sure when to return true
if (response.Length > 0)
{
Console.WriteLine($"Server response: {response}");
return true;
}
return false;
}
public bool Connect(Uri _endpoint)
{
//Create a TcpClient with our uri
_tcpClient = new TcpClient(_endpoint.Host, _endpoint.Port);
//Create a SslStream
_sslStream = new SslStream(_tcpClient.GetStream(), false, ValidateServerCertificate);
//SIDN requires ssl
_sslStream.AuthenticateAsClient(_endpoint.Host, null, System.Security.Authentication.SslProtocols.Tls12, true);
//Read server greet from our _sslStream
var greet = ReadMessage();
//If we are successfully connected and got a server greet
if (_sslStream.IsAuthenticated && _tcpClient.Connected && greet.Length != 0)
{
Console.WriteLine($"Server greet: {greet}");
return true;
}
return false;
}
编辑:我也在使用 tls,因为我看到这个问题使用了与我相同的注册商,但也没有解决它。