[Solved-3 Solutions] HTTP POST Returns Error: 417 “Expectation Failed.”
Error Description:
- When we try to POST to a URL it results in the following exception:
The remote server returned an error: (417) Expectation Failed.
- Here's a sample code:
var client = new WebClient();
var postData = new NameValueCollection();
postData.Add("postParamName", "postParamValue");
byte[] responseBytes = client.UploadValues("http://...", postData);
string response = Encoding.UTF8.GetString(responseBytes); // (417) Expectation Failed.
click below button to copy the code. By - C# tutorial - team
Solution 1:
- System.Net.HttpWebRequest adds the header 'HTTP header "Expect: 100-Continue"' to every request unless you explicitly ask it not to by setting this static property to false:
System.Net.ServicePointManager.Expect100Continue = false;
click below button to copy the code. By - C# tutorial - team
- Some servers choke on that header and send back the 417 error you're seeing.
- Give that a try.
Solution 2:
<system.net>
<settings>
<servicePointManager expect100Continue="false" />
</settings>
</system.net>
click below button to copy the code. By - C# tutorial - team
Solution 3:
- If you are using "HttpClient", and you don't want to use global configuration to affect all your program you can use:
HttpClientHandler httpClientHandler = new HttpClientHandler();
httpClient.DefaultRequestHeaders.ExpectContinue = false;
click below button to copy the code. By - C# tutorial - team
- I you are using "WebClient" I think you can try to remove this header by calling:
var client = new WebClient();
client.Headers.Remove(HttpRequestHeader.Expect);