When I 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.
Using an HttpWebRequest/HttpWebResponse
pair or an HttpClient
doesn't make a difference.
What's causing this exception?
In my situation, this error seems to occur only if my client's computer has a strict firewall policy, which prevents my program from communicating with the web service.
So only solution I could find is to catch the error and inform user about changing the firewall settings manually.
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:
Some servers choke on that header and send back the 417 error you're seeing.
Give that a shot.
Does the form you are trying to emulate have two fields, username and password?
If so, this line:
is not correct.
you would need two lines like:
Edit:
Okay, since that is not the problem, one way to tackle this is to use something like Fiddler or Wireshark to watch what is being sent to the web server from the browser successfully, then compare that to what is being sent from your code. If you are going to a normal port 80 from .Net, Fiddler will still capture this traffic.
There is probably some other hidden field on the form that the web server is expecting that you are not sending.