在httpwebrequest中创建TCP连接的方式和位置,以及它与servicepoint的关系如何?

我试图找出在使用HttpWebRequest时何时建立TCP连接,如何使用ServicePoint汇集和重用这些连接。

我查看了system.dll,并尝试使用ILSpy和Reflector浏览代码,不知何故没有看到任何对套接字的引用,建立tcp连接等。

下面我已经粘贴了反编译的代码 – 任何请给我提示或重定向我,以便我能理解:

  1. 什么时候创建TCP连接?
  2. 如何使用ServicePoint保持这些连接的存活,汇集和重用?

HttpWebRequest of System.dll的代码片段:

public override Stream GetRequestStream() { TransportContext context; return this.GetRequestStream(out context); } public Stream GetRequestStream(out TransportContext context) { if (Logging.On) { Logging.Enter(Logging.Web, this, "GetRequestStream", ""); } context = null; this.CheckProtocol(true); if ((this._WriteAResult == null) || !this._WriteAResult.InternalPeekCompleted) { lock (this) { if (this._WriteAResult != null) { throw new InvalidOperationException(SR.GetString("net_repcall")); } if (this.SetRequestSubmitted()) { throw new InvalidOperationException(SR.GetString("net_reqsubmitted")); } if (this._ReadAResult != null) { throw ((Exception) this._ReadAResult.Result); } this._WriteAResult = new LazyAsyncResult(this, null, null); this.Async = false; } this.CurrentMethod = this._OriginVerb; while (this.m_Retry && !this._WriteAResult.InternalPeekCompleted) { this._OldSubmitWriteStream = null; this._SubmitWriteStream = null; this.BeginSubmitRequest(); } while (this.Aborted && !this._WriteAResult.InternalPeekCompleted) { if (!(this._CoreResponse is Exception)) { Thread.SpinWait(1); } else { this.CheckWriteSideResponseProcessing(); } } } ConnectStream connectStream = this._WriteAResult.InternalWaitForCompletion() as ConnectStream; this._WriteAResult.EndCalled = true; if (connectStream == null) { if (Logging.On) { Logging.Exception(Logging.Web, this, "EndGetRequestStream", this._WriteAResult.Result as Exception); } throw ((Exception) this._WriteAResult.Result); } context = new ConnectStreamContext(connectStream); if (Logging.On) { Logging.Exit(Logging.Web, this, "GetRequestStream", connectStream); } return connectStream; } 

K,在浏览代码一段时间后,我觉得我有点理解抽象。 基本上是servicepoint,servicepoint manager,如何创建tcp连接,连接已经汇总,排队等等总是让我困惑。 以下信息对我有所帮助 – 希望这对其他好奇或试图了解这些细节的人有用:

ServicePoint :对特定主机(目标主机Ip:端口)的“连接”的高级抽象(这就是为什么ex,函数静态ServicePoint FindServicePoint (字符串主机,int端口)在servicePointManger中定义。

ServicePointManager :名称表示其管理服务点的全局(静态)类。

连接(内部类) :基本上这是我认为代表TCP连接的那个。 它基本上派生自System.Net.PoolStream(内部类 – 它具有使用的套接字的定义),它派生自流。

ConnectionGroup(内部类) :每个HttpWebRequest都与一个连接组相关联。 (基本上基于connectionLimit,它最多创建connectionLimit (可以通过ServicePointManager全局配置,也可以使用其servicePoint属性按httpwebrequest配置)每个httpwebrequest的连接对象数)

如果达到连接限制,它只是排队并传递给线路(最有可能 – 但仍然没有得到执行此操作的代码)。

如果要连接到本地计算机上的服务,则servicepoint.connectionlimit不再等于servicepointmanager.defaultconnectionlimit。 它默认为; int.Maxvalue( 21474836477FFFFFFF )(您可以参考: http : //blogs.microsoft.co.il/idof/2011/06/20/servicepointmanagerdefaultconnectionlimit-2-depends/ )

更新:

看起来以下两个链接也很有用:

System.Net.ServicePointManager.DefaultConnectionLimit和.MaxServicePointIdleTime

http://blogs.msdn.com/b/jpsanders/archive/2009/05/20/understanding-maxservicepointidletime-and-defaultconnectionlimit.aspx

最好的祝福!