轉自:http://blog.csdn.net/hustypf/article/details/8132158
在用c#做zhaopin.com網站自動登陸的時候,一直返回“遠程服務器返回錯誤: (417) Expectation failed” 這個提示,在檢查確認代碼沒有問題后,google了一下找到了解決方案:
在代碼的最開始加入如下一句:
System.Net.ServicePointManager.Expect100Continue = false;
或在配置文件中加入(示例代碼中的粗體文字):
<?xml version=”1.0″ encoding=”utf-8″ ?>
<configuration>
<system.net>
<settings>
<servicePointManager expect100Continue=”false”/>
</settings>
</system.net>
</configuration>
這個異常源自HTTP1.1協議的一個規范: 100(Continue)
100(Continue)狀態代碼的解釋
允許客戶端發request消息body之前先用request header試探一下server,看server要不要接收request body,再決定要不要發request body。
客戶端在Request頭部中包含
Expect:100-continue
Server接到后 如果回100(continue)這個狀態代碼,客戶端就繼續發request body。
這個設置是Http1.1才有。
==================================================================
最近在工作中遇見一個奇怪的問題,有一段程序是需要跟別的公司的程序做段交互。
開發環境:VS.NET2003 WINDOWS SERVER 2003 IIS5 Framework1.1
症狀:這段代碼在測試平台沒有問題,發布到正式平台后,發現報告異常: 417 Expectation Failed
示例代碼如下:
Code
try
{
WebRequest req;
req = WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
Response.Write(url + "<br/>");
ASCIIEncoding enc = new ASCIIEncoding();
Byte[] buf;
buf = enc.GetBytes("CLIENT_ID=" + clienId + "&B_IP=" + c_b_ip);
Response.Write(clienId + "." + c_b_ip + "<br/>");
Stream news;
news = req.GetRequestStream();
news.Write(buf, 0, buf.Length);
news.Close();
Response.Write("Here1<br/>");
WebResponse res;
res = req.GetResponse();
Response.Write("Here2<br/>");
StreamReader sr;
sr = new StreamReader(res.GetResponseStream());
Response.Write("Here3<br/>");
char[] buff = new char[4096];
int count= sr.Read(buff,0,4096);
string red;
red = "";
while(count > 0)
{
string s = new string(buff, 0, count);
Response.Write(s + "\r\n");
red = red + s;
count = sr.Read(buff, 0, 4096);
}
sr.Close();
res.Close();
return red;
}
catch(Exception ex)
{
Response.Write("錯誤信息:"+ex);
return "-1";
}
調用代碼如下:
Code
string r_code;
r_code = GetRandomCode("http://www.cnblogs.com/A_B_random_code.jsp", "CER_KEY","101.11.1.1");
string redirectURL;
string userName;
userName = "zhuanjia1";
redirectURL = "http://www.cnblogs.com/A_B_random_redirect.jsp?un=" + userName + "&r_code=" + r_code;
Response.Write(redirectURL);
輸出這段代碼的異常:
http://www.cnblogs.com/login.jsp<br/>
cnblogs.172.0.0.1<br/>
Here1<br/>
錯誤信息:System.Net.WebException:遠程服務器返回錯誤:<417>Expectation Failed。
as System.Net.HttpWebRequest.CheckFinalStatus<>
as System.Net.HttpWebRequest.EndGetResponse<IAsyncResult asyncResult>
as System.Net.HttpWebRequest.GetResponse<>
正常情況下的信息輸出:
http://www.cnblogs.com/login.jsp<br/>
cnblogs.172.0.0.1<br/>
Here1<br/>
Here2<br/>
Here3<br/>
在第一段代碼中加入如下一句,問題解決:
System.Net.ServicePointManager.Expect100Continue = false;
或在配置文件中加入:
<configuration>
<system.net> <system.net>
<settings> <settings>
<servicePointManager expect100Continue="false" />
這個異常源自HTTP1.1協議的一個規范: 100(Continue)
100(Continue)狀態代碼的解釋
允許客戶端發request消息body之前先用request header試探一下server,看server要不要接收request body,再決定要不要發request body。
客戶端在Request頭部中包含
Expect:100-continue
Server接到后 如果回100(continue)這個狀態代碼,客戶端就繼續發request body。
這個設置是Http1.1才有。
- 頂
- 0
- 踩
