Https connection using IP address

0 votes
asked Oct 28, 2021 by lanopk (1,620 points)

Please make a basic inquiry about the use of HTTPS.

The connecting server has 5 servers (IP address) in one host name.
I want to connect to the server with the fastest IP among them.

For example, the hostname is "", and I can find multiple IP addresses by calling GetHostAddresses.

Among them, the server with the fastest response is found through the ping test.

And I'm trying to make an Http connection to this server.
But the url has a name, not an ip.
For example, string url = "" .

Where and how can I set it up so that I can connect to the IP I want?

Rebex.Net.HttpRequestCreator creator = new Rebex.Net.HttpRequestCreator();
creator.Settings.SslAllowedVersions = Rebex.Net.TlsVersion.TLS10 | Rebex.Net.TlsVersion.TLS11 | Rebex.Net.TlsVersion.TLS12 | Rebex.Net.TlsVersion.TLS13;
creator.Settings.SslAcceptAllCertificates = true;

string url = "";

// find fastest ip
// ...

var request = creator.Create(url);
request.Method = "GET";
request.KeepAlive = true;

// set ip address
// request.??? = fastestip;

using (var response = request.GetResponse())
Applies to: Rebex HTTPS
commented Oct 29, 2021 by lanopk (1,620 points)
I succeeded by doing the following in .Net Http.
I'm not sure if this is the right way to do it.

string url = "https://{fastestip}/{parameter}";
var request = (HttpWebRequest) WebRequest.Create(url);
request.Host = "";

But Rebex.Net.HttpRequest doesn't have a "Host" setting.

1 Answer

0 votes
answered Oct 29, 2021 by Lukas Pokorny (128,250 points)
selected Nov 3, 2021 by lanopk
Best answer

You can implement a simple custom socket factory that makes it possible to choose the IP you need. Try this code to get started:

public class CustomSocket : ProxySocket, ISocket
    private static readonly ISocketFactory _factory = new CustomSocketFactory();

    public static ISocketFactory Factory { get { return _factory; } }

    private class CustomSocketFactory : ISocketFactory
        ISocket ISocketFactory.CreateSocket()
            return new CustomSocket();

    void ISocket.Connect(string serverName, int serverPort)
        // choose the desired IP address here
        IPHostEntry entry = Dns.GetHostEntry(serverName);
        IPAddress address = entry.AddressList[0];

        // connect to it
        Connect(new IPEndPoint(address, serverPort));

To utilize this in your code, use SetSocketFactory method:

var creator = new HttpRequestCreator();
commented Nov 4, 2021 by lanopk (1,620 points)
Thanks for the reply.
I'm going to try the method you suggested.

Is there any way to change the "Host" header when making a request?
commented Nov 4, 2021 by Lukas Pokorny (128,250 points)
The "Host" header corresponds to the host name in the specified URL. To use a different "Host" header, just specify a different URL.