今天遇到一案例,
使用 WebClient 下載檔案,
卻拋出以下錯誤訊息:
unable to read data from the transport connection an existing connection was forcibly closed by remote host
解決方式:
在建立 WebClient 前,加入此行,即可
System.Net.ServicePointManager.Expect100Continue = false;
那什麼要將 Expect100Continue 設為 false 呢?
因為 Server 跟 Client 在溝通時,中途會經過很多節點,如 proxy,
但這些節點不一定支援 Expect100Continue 進而拋出此 exception
那 Expect100Continue 又是什麼?
簡單來說是為了減少網路流量所設計的機制,原因如下:( 參考 msdn )
For example, assume the Expect100Continue property is false.When the request is sent to the server, it includes the data.If, after reading the request headers, the server requires authentication and must send a 401 response, the client must resend the data with proper authentication headers.
If this property is true, the request headers are sent to the server.If the server has not rejected the request, it sends a 100-Continue response signaling that the data can be transmitted.If, as in the preceding example, the server requires authentication, it sends the 401 response and the client has not unnecessarily transmitted the data.
沒有留言:
張貼留言