request.GetResponse()出错 [英] Error in request.GetResponse()

查看:110
本文介绍了request.GetResponse()出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码



Here is my code

string parameters = @"OrderId,OrderedDate,OrderType,ProductId,SubscriptionPeriod,SubscriptionPrice,SubscriptionVolumeCount,SubscriptionScheduleId,UserId" +
                Environment.NewLine + "1,2013-08-07 19:41:28,1,1300002727,1,800,1,961193,1";

                   // string parameters = strArray[1];
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.Method = "POST";
                    request.KeepAlive = false;

                    byte[] bytes = Encoding.ASCII.GetBytes(parameters);
                    request.ContentLength = bytes.Length;
                    using (Stream os = request.GetRequestStream()) // this line gets the error
                    {
                        os.Write(bytes, 0, bytes.Length);
                        os.Close();
                        using (WebResponse response = request.GetResponse())
                        {
                            if (response == null)
                            {
                                Console.WriteLine("Response is null");
                            }
                            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                            {
                                string s=reader.ReadToEnd().Trim();
                                ErrorLog.WriteLog("FujiApiPost", "FujiApiPostfn", "result:" + s, string.Empty, string.Empty);

                            }
                        }
                    }





错误:-Length ='os。长度'抛出类型'System.NotSupportedException'的异常'



Error:-Length = 'os.Length' threw an exception of type 'System.NotSupportedException'

推荐答案

参考 - HttpWebRequest.GetRequestStream方法 [ ^ ]。

Refer - HttpWebRequest.GetRequestStream Method[^].
Quote:

< td> NotSupportedException

请求缓存验证器指示可以从缓存提供该请求的响应;但是,写入数据的请求不得使用缓存。如果您使用的是错误实现的自定义缓存验证程序,则会发生此异常。

NotSupportedException

The request cache validator indicated that the response for this request can be served from the cache; however, requests that write data must not use the cache. This exception can occur if you are using a custom cache validator that is incorrectly implemented.



也请按照该文章底部给出的示例进行操作。就像在那里完全一样。


Also follow the example given in that article at the bottom. Just do exactly as it is given there.


这篇关于request.GetResponse()出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆