阅读发布回复时的C#OutOfMemory异常

时间:2017-06-18 14:28:54

标签: c# api post out-of-memory webclient

我目前正在开发一个简单的应用程序,它利用JSON对象POST到API并获取响应数据。但是,当我运行POST方法时,POST响应非常大,以至于我遇到了OutOfMemory异常。

我目前正在使用WebClient和CookieContainer进行处理:

var test = [];

function clear(a) {
  a.b = 2;
  a = null;
  console.log(a);                         // null
}

clear(test);

console.log(test);                        // [b: 2]
console.log(test.b);                      // 2

我已经调查了这个问题,并将属性AllowStreamBuffering设置为false。

string jsonObject ="...."; //Example JSON string - It's very small

using (var client = new WebClient())
{
     var auth = new NameValueCollection();
       values["username"] = "username";
       values["password"] = "password";

     client.uploadValues(endpoint,auth);

     // This is causing the OutOfMemory Exception
     var response = client.uploadString(endpoint, jsonObject);

}

但是,我仍然遇到这个问题,并且不知道如何控制POST响应。

更新:7/5/2017

感谢@Tim建议,我已经转移到了响应流,但是我遇到了有关实际响应的问题。在使用POST方法将JSON(作为字符串)写入结束点之后,脚本在尝试读取响应时陷入困境。

 client.AllowStreamBuffering() = false;

我想知道JSON是否未被编码。

(旁注:我已经看过将响应逐行写入文件,但这是引起问题的响应 - http://cc.davelozinski.com/c-sharp/fastest-way-to-read-text-files

1 个答案:

答案 0 :(得分:0)

我能够解决自己的问题。对于标题,我忘记编码JSON,并为API正确设置内容类型。

以上是与流完全相同的代码,但是使用更新的头和bufferedStream可以提高处理数据和内存的效率。

String endPoint = @"http://example.com/v1/api/";
String json = @"....";//Example JSON string

Byte[] jsonData = Encoding.UTF8.GetBytes(json);

StreamWriter file = new StreamWriter(@"File Location");

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);
request.Method = "POST";
request.ContentType = "text/plain";
request.KeepAlive = false;
request.AllowReadStreamBuffering = false;
request.ContentLength = jsonData.Length;

/* Pretend this middle part does the Authorization with username and password. */
/* I have actually authenticated using the above method, and passed a key to the request */

 //This part POST the JSON to the API
 Stream writeStream = request.GetRequestStream();
 writeStream.Write(jsonData, 0, jsonData.Length);

 //This conducts the reading/writing into the file
 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 using (Stream receivedStream = response.GetResponseStream())
 using (BufferedStream bs = new BufferedStream(receivedStream))
 using (StreamReader sr = new StreamReader(bs))
 {
       String s;
       while ((s = sr.ReadLine()) != null)
       {
            file.WriteLine(s);
        }

 }

这里有很多方法来自Dave Lozinski关于内存处理,流读取和流写入的博客文章(尽管他使用文件而不是流)。通过上面的帖子寻求帮助是非常有用的。