1. 程式人生 > >C#中POST請求時出現System.Net.WebException: 遠端伺服器返回錯誤: (417)Expectation Failed的解決辦法

C#中POST請求時出現System.Net.WebException: 遠端伺服器返回錯誤: (417)Expectation Failed的解決辦法

在使用POST請求的時候,當要POST的資料大於1024位元組的時候,服務不會直接就發起POST請求,而是會分為倆步: (1)傳送一個請求, 包含一個Expect:100-continue, 詢問Server使用願意接受資料。 (2)接收到Server返回的100-continue應答以後,才把資料POST給Server。

C#中的錯誤提示:System.Net.WebException: 遠端伺服器返回錯誤: (417)Expectation Failed。

解決辦法:

1、在配置檔案中的<configuration>節點裡新增如下配置資訊(推薦)

<system.net>
  <settings>
    <servicePointManager expect100Continue="false"/>
  </settings>
</system.net>

2、在執行程式碼中新增如下程式碼。

System.Net.ServicePointManager.Expect100Continue = false;