HttpWebRequest.ServicePoint与ServicePointManager.FindServicePoint不同

时间:2018-08-28 01:26:15

标签: c# webrequest servicepoint

我有一台具有三个不同网络接口的计算机。我尝试通过这些设备之一发送JSON请求。但是由于某些原因,ServicePoint中的WebRequest总是错误的。

  

PC IP1:10.43.130.122

     

PC IP2:192.168.2.3

     

PC IP3:10.43.140.33

     

目标IP:10.43.130.152

网络中还有其他三台PC。全部具有三个网络接口,都在相同的IP范围内。在所有其他三个参数上,ServicePoint均已正确设置,并且JSON请求有效。

这是我的代码。 sp,sp2和sp3只是调试代码。 sp和sp2是正确的(目标ip),但是sp3总是错误的。它始终是192.168.25..9:8919。但是,一旦我知道WebRequest对象就从ServicePoint获得了ServicePointManager。那怎么可能呢?

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 个答案:

没有答案