我有一台具有三个不同网络接口的计算机。我尝试通过其中一种设备发送 JSON 请求。但由于某种原因,the ServicePoint
of theWebRequest
总是错误的。
电脑 IP1:10.43.130.122
电脑 IP2:192.168.2.3
电脑 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;
// ...
}
}