The remote server returned an error: (417) Expectation failed

Today I did a little demo using the Search API on Twitter. Create a new project and write the following test code directly in the Page_Load of the test page.

     protected void Page_Load(object sender, EventArgs e)
{
string urlPost = "http://search.twitter.com/search.json";

string postData = "q=twitterapi";
UTF8Encoding encoding = new UTF8Encoding();
byte[] byteData = encoding.GetBytes(postData);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlPost);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteData.Length;

Stream requestStream = request.GetRequestStream();
requestStream.Write(byteData, 0, byteData.Length);
requestStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Stream receiveStream = response.GetResponseStream();

StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

Response.Write("Response stream received.");
Response.Write(readStream.ReadToEnd());
response.Close();
readStream.Close();
}

Unfortunately, The error “The remote Server has returned an Error: (417) taught failed” appeared.
At first, I thought it was the “great Firewall”, but it was still the same after connecting to THE VPN, so I went online to find a solution, and finally I found the reason.
“This error can be caused by a client network environment (Don’t Support “100 continue”). Add the following to the program’s configuration file and give it a try.” The original address: http://0e2.net/post/1184.html

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

Test, success!!

Reproduced in: https://www.cnblogs.com/aimarAgt/archive/2011/08/17/2143177.html

Read More: