GetRequestStream在Array中停止

时间:2013-07-09 13:51:21

标签: c# web-services post get-request

我有一个json字符串列表,我试图循环并发布到WebService。它循环通过前两个就好了,然后在第三次迭代时停止(这发生在第14行)。没有错误或任何东西,它似乎停止了。有什么想法吗?

    private static void CacheGetDataUrl(Guid sessionGuid)
    {
        var tokens = Driver.GetDataPostValues();
        foreach (var token in tokens)
        {
            try
            {
                var buffer = Encoding.UTF8.GetBytes(token.Replace("session_identifier", sessionGuid.ToString()));
                var request = (HttpWebRequest)WebRequest.Create(GetDataUrl);
                request.Timeout = 1000000;
                request.Method = "POST";
                request.ContentType = "application/json; charset=utf-8";
                request.ContentLength = buffer.Length;
                using (var postData = request.GetRequestStream())
                {
                    postData.Write(buffer, 0, buffer.Length);
                    postData.Close();
                }
                Console.WriteLine("Response requested for URL {0}", GetDataUrl); 
                Log.Info(String.Format("Response requested for URL {0} using parameters \r\n{1}", GetDataUrl, token));
            }
            catch (Exception)
            {
                Console.WriteLine("Error occurred requesting response for URL {0} using parameters \r\n{1}", GetDataUrl, token);
                Log.Error(String.Format("Error occurred requesting response for URL {0} using parameters \r\n{1}", GetDataUrl, token));
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

使用一些新属性更改方法,并在收到请求后中止请求似乎解决了问题。

    private static void CacheGetDataUrl(Guid sessionGuid)
    {
        var tokens = Driver.GetDataPostValues();
        foreach (var token in tokens)
        {
            try
            {
                var buffer = Encoding.UTF8.GetBytes(token.Replace("session_identifier", sessionGuid.ToString()));
                var request = (HttpWebRequest)WebRequest.Create(GetDataUrl);
                request.KeepAlive = false;
                request.Timeout = System.Threading.Timeout.Infinite;
                request.ProtocolVersion = HttpVersion.Version10;
                request.Method = "POST";
                request.ContentType = "application/json; charset=utf-8";
                request.ContentLength = buffer.Length;
                request.ServicePoint.ConnectionLimit = 250;
                using (var postData = request.GetRequestStream())
                {
                    postData.Write(buffer, 0, buffer.Length);
                    postData.Close();
                }

                request.Abort();

                Console.WriteLine("Response requested for URL {0}", GetDataUrl); 
                Log.Info(String.Format("Response requested for URL {0} using parameters \r\n{1}", GetDataUrl, token));
            }
            catch (Exception)
            {
                Console.WriteLine("Error occurred requesting response for URL {0} using parameters \r\n{1}", GetDataUrl, token);
                Log.Error(String.Format("Error occurred requesting response for URL {0} using parameters \r\n{1}", GetDataUrl, token));
            }
        }
    }