我想了解何時使用HttpWebRequest建立TCP連接時,如何使用ServicePoint合併和重用這些連接。httpwebrequest中如何創建TCP連接,以及它如何與servicepoint相關?
我已經看了看system.dll中,並試圖通過使用ILSpy和反射器的代碼瀏覽,不知何故沒看插座的任何引用,建立TCP連接等
下面我粘貼的反編譯代碼 - 可以任何請給我提示或重定向我,這樣我就可以理解:
- 當TCP連接已創建?
- 這些連接如何保持活躍,彙集並使用ServicePoint重用?
代碼段從HttpWebRequest的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;
}