.net calling post request with ssl

80 Views Asked by At

I have this simple request to XTB broker demo account according to the documentation http://developers.xstore.pro/documentation/

public static async Task Login()
{
    var httpClient = new HttpClient();
    using StringContent jsonContent = new(
        JsonSerializer.Serialize(new
        {
            command = "login",
            arguments = new
            {
                userId = "XXXX",
                password = "XXXX",
            }
        }),
        Encoding.UTF8,
        "application/json");
    var uri = new Uri("https://xapi.xtb.com:5124");

    using HttpResponseMessage response = await httpClient.PostAsync(
        uri,
        jsonContent);

    var jsonResponse = await response.Content.ReadAsStringAsync();
    Console.WriteLine($"{jsonResponse}\n");
}

But I am always getting error:

"IOException: The response ended prematurely."

What I found on the internet is that I need to use ssl connection for request to work, but I am not sure how to implement it inside this avalonia .net project. Is there any way way how can I make ssl request?

1

There are 1 best solutions below

0
On BEST ANSWER

http://developers.xstore.pro/documentation/

Introduction ... The connection is performed by clean socket connection. For real trading SSL connection will be used.

So that means that You cannot use HTTP request, seems that raw socket + SSL stream need to be used. I created code snipped to give idea how probably it should work:

using System;
using System.Diagnostics;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.Json;

namespace program
{
    class Program
    {
        public static bool ValidateServerCertificate(object sender, X509Certificate certificate,
X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            // this need to be validated correctly, need to find somewhere how to validate it
            return true;
        }

        static void Main(string[] args)
        {
            IPHostEntry hostEntry;
            string host = "xapi.xtb.com";
            int demoPort = 5124;

            hostEntry = Dns.GetHostEntry(host);

            //you might get more than one ip for a hostname since 
            //DNS supports more than one record
            if (hostEntry.AddressList.Length < 1) throw new Exception($"cannot resolve host name '{host}'");

            // somehow need to select IP
            var ip = hostEntry.AddressList[1];
            string server = ip.ToString();

            TcpClient client = new TcpClient(server, demoPort);

            using (SslStream sslStream = new SslStream(client.GetStream(), false,
                new RemoteCertificateValidationCallback(ValidateServerCertificate), null))
            {
                sslStream.AuthenticateAsClient(server);

                var jsonToSend = JsonSerializer.Serialize(new
                {
                    command = "login",
                    arguments = new
                    {
                        userId = "XXXX",
                        password = "XXXX",
                    }
                });

                // send/receive data using SslStream
                byte[] toSendBytes = Encoding.UTF8.GetBytes(jsonToSend);
                byte[] receivedBytes = new byte[1024];

                sslStream.Write(toSendBytes);
                int receivedCount = sslStream.Read(receivedBytes);

                string receivedString = Encoding.ASCII.GetString(receivedBytes, 0, receivedCount);

                Console.WriteLine("Received: ");
                Console.WriteLine(receivedString);
            }
        }
    }
}

I get following output:

Received:
{"status":false,"errorCode":"EX000","errorDescr":"Invalid parameters"}

Regards NeuroXiq