我有一个正在进行网络请求的计划任务。这一切都很好。但突然间,我得到以下错误日志。
12/05/2010 20:21:17 读取XML失败 System.Net.WebException:远程服务器返回错误:(417)期望失败。 在System.Net.HttpWebRequest.GetResponse() at DelegateImport.Update.UpdateLiveSite(String delegateId,String badgeId) 在DelegateImport.Rss.RssReader()
以下是发出网络请求的代码
WebRequest request = WebRequest.Create (uri);
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
request.Timeout = 30000000;
Stream dataStream = request.GetRequestStream ();
dataStream.Write (byteArray, 0, byteArray.Length);
dataStream.Close ();
WebResponse response = request.GetResponse ();
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream ();
StreamReader reader = new StreamReader (dataStream);
string responseFromServer = reader.ReadToEnd ();
Console.WriteLine (responseFromServer);
reader.Close ();
dataStream.Close ();
response.Close();
答案 0 :(得分:0)
默认情况下,.NET会使用标头Expect: 100-Continue
标记传出的POST请求。如果服务器不支持此功能,则会因417错误而失败。
要让.NET不这样做,请在创建WebRequest对象之前执行以下命令:
System.Net.ServicePointManager.Expect100Continue = false;