HttpWebRequest.ServicePoint not the same as ServicePointManager.FindServicePoint

1.1k Views Asked by At

I have a computer with three different network interfaces. I try to send a JSON request via one of those devices. But for some reason, the ServicePoint of the WebRequest is always wrong.

PC IP1: 10.43.130.122

PC IP2: 192.168.2.3

PC IP3: 10.43.140.33

Destination IP: 10.43.130.152

There are three other PCs in the network. All with three network interfaces, all in the same IP range. On all other three, the ServicePoint is set correctly and the JSON request works.

Here is my code. The sp, sp2 and sp3 are just debug code. sp and sp2 are correct (destination ip), but sp3 is always wrong. It is always 192.168.25..9:8919. But as soon as I know the WebRequest object gets its ServicePoint from the ServicePointManager. So how is this possible?

private static bool QueryJson(string url, CancellationToken? cancellationToken, out string result)
{

        lock (webRequestLock)
        {
            HttpWebRequest dataRequest = WebRequest.Create(url) as HttpWebRequest;

            if (dataRequest == null) throw new Exception("dataRequest is not a HttpWebRequest");
            dataRequest.UserAgent = "iSAM Velodyne Interface";

            dataRequest.ContentType = "";
            dataRequest.SendChunked = false;
            dataRequest.ServicePoint.UseNagleAlgorithm = false;
            dataRequest.Method = "GET";
            dataRequest.Accept = "*/*";
            dataRequest.KeepAlive = false;
            dataRequest.ServicePoint.Expect100Continue = false;
            dataRequest.Timeout = TIMEOUT;

            // sp and sp2 ServicePoint have the expected destination IP
            var sp = ServicePointManager.FindServicePoint(dataRequest.RequestUri);
            var sp2 = ServicePointManager.FindServicePoint(new Uri(url));
            // sp3 does does not have the expected IP
            // (Why isn't it getting the same ServicePoint as above?)
            var sp3 = dataRequest.ServicePoint;

            // ...
        }
}
0

There are 0 best solutions below