开发者

Getting 417 error when using webrequest

开发者 https://www.devze.com 2022-12-29 22:15 出处:网络
I have a schedule task that is making a webrequest. This was all working fine. However all of sudden i\'m g开发者_如何学编程etting the following error log.

I have a schedule task that is making a webrequest. This was all working fine. However all of sudden i'm g开发者_如何学编程etting the following error log.


12/05/2010 20:21:17 Failure reading XML System.Net.WebException: The remote server returned an error: (417) Expectation failed. at System.Net.HttpWebRequest.GetResponse() at DelegateImport.Update.UpdateLiveSite(String delegateId, String badgeId) at DelegateImport.Rss.RssReader()

Here is the code making the web request

                    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();


By default, .NET will tag outgoing POST requests with the header Expect: 100-Continue. If the server doesn't support this, it will fail with a 417 error.

To get .NET to not do this, execute the following before creating your WebRequest object:

System.Net.ServicePointManager.Expect100Continue = false;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号