1

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;

            // ...
        }
}
Community
  • 1
  • 1
Dave Scum
  • 11
  • 4
  • 2
    What is the value of `dataRequest.RequestUri`? `sp`? `url`? `sp2`? `sp3`? – mjwills Aug 28 '18 at 01:29
  • The `dataRequest.RequestUri` gets build by the `WebRequest.Create(url)`. `sp` and `sp2` are just examples of how I expect the `SericePoint.Address` to be (`http://10.43.130.152/cgi/status.json`). `sp3` ist what the `ServicePoint` is actually like (192.168.255.9:8919). I don't know how to tell the `WebRequest` to choose the correct `ServicePoint`. And don't understand why I get the correct one, when I ask the `ServicePointManger` myself. – Dave Scum Aug 28 '18 at 03:52
  • Sorry, I was unclear. I wasn't asking for an explanation. I was interested in their actual values. – mjwills Aug 28 '18 at 04:06
  • I don't have access to system from here, so I can only tell from my memory. The `Address` Property of the `sp` and `sp2` `ServicePoint`objects are `http://10.43.130.152/cgi/status.json`. And so is the `dataReequest.RequestUri`. But `sp3` and accordingly `dataRequest.ServicePoint.Address` is something like `http://192.168.255.9:8919`. The `ServicePoint` object is the only differnce I was able to figure out when running the code on all PCs. Am I right at all that this is the reason for the unsuccessful request?(Can't see it in wireshark) PS: This URI works in Firefox without any problems. – Dave Scum Aug 28 '18 at 06:37
  • `so I can only tell from my memory.` Cool - share them when you are at your PC so you can debug and be 100% sure. – mjwills Aug 28 '18 at 10:01

0 Answers0