Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#读取Post响应时发生OutOfMemory异常_C#_Api_Post_Out Of Memory_Webclient - Fatal编程技术网

C#读取Post响应时发生OutOfMemory异常

C#读取Post响应时发生OutOfMemory异常,c#,api,post,out-of-memory,webclient,C#,Api,Post,Out Of Memory,Webclient,我目前正在开发一个简单的应用程序,它利用JSON对象发布到API并获取响应数据。但是,当我运行POST方法时,POST响应非常大,以至于我遇到OutOfMemory异常 我目前正在使用WebClient和CookieContainer来完成以下过程: string jsonObject ="...."; //Example JSON string - It's very small using (var client = new WebClient()) { var auth = n

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

我目前正在使用WebClient和CookieContainer来完成以下过程:

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

}
我已经研究了这个问题,并将属性AllowStreamBuffering设置为false

 client.AllowStreamBuffering() = false;
但是,我仍然遇到这个问题,不知道如何控制帖子的回复

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

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);
    request.Method = "POST";
    request.KeepAlive = false;
    request.AllowReadStreamBuffering = false;

    /* 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
    using (StreamWriter writeStream = new StreamWriter(request.GetRequestStream()))
        {
            writeStream.Write(json);
            writeStream.Flush();
            writeStream.Close();  
        }

    //This bottom part opens up a console, but never reads or loads the data
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());

更新:2017年7月5日

多亏了@Tim的建议,我得到了一系列的回复,但我遇到了关于实际回复的问题。在使用POST方法将JSON(作为字符串)写入端点后,脚本无法读取响应

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

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);
    request.Method = "POST";
    request.KeepAlive = false;
    request.AllowReadStreamBuffering = false;

    /* 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
    using (StreamWriter writeStream = new StreamWriter(request.GetRequestStream()))
        {
            writeStream.Write(json);
            writeStream.Flush();
            writeStream.Close();  
        }

    //This bottom part opens up a console, but never reads or loads the data
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());
我想知道JSON是否可能没有编码



(旁注:我已经考虑过将响应逐行写入文件,但正是响应导致了问题--)

我能够解决自己的问题。对于头部,我忘记了对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关于内存处理、流读取和流写入的博客文章(尽管他使用文件而不是流)。浏览上面的帖子寻求帮助非常有用。

我能够解决自己的问题。对于头部,我忘记了对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关于内存处理、流读取和流写入的博客文章(尽管他使用文件而不是流)。浏览上面的帖子寻求帮助非常有用。

这里的答案似乎与您的问题有关:嗨,蒂姆,谢谢您提供的信息。我还没有尝试使用HttpWebRequest for getResponse(),但如果是解决方案,我会在这里更新它。您可能需要使用流,BeginGetResponse应该允许您将整个响应分块获取。这里的答案似乎与您的问题有关:Hi Tim,感谢您提供的信息。我还没有尝试使用HttpWebRequest for getResponse(),但如果是解决方案,我会在这里更新它。您可能需要使用流,BeginGetResponse应该允许您以块的形式获取整个响应