I'm currently working on a simple application that utilizes JSON objects to POST to an API and gets the responding data. However, when I run the POST method, the POST response is so large that I encounter the OutOfMemory Exception.
I'm currently using WebClient and a CookieContainer for the process:
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);
}
I have looked into the issue, and have set the property AllowStreamBuffering to be false.
client.AllowStreamBuffering() = false;
However, I am still encountering the issue, and do not know how to control the POST response.
Update: 7/5/2017
Thanks to @Tim suggestions, I've moved to a stream of the response, however I'm encountering issues regarding the actual response. After writing the JSON (as a string) to the end point with a POST method, the script gets stuck at trying to read the response.
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());
I'm wondering if the JSON is not encoded probably.
(Side Note: I have looked at writing the response, line by line onto a file, but it's the response that is causing the issue -- http://cc.davelozinski.com/c-sharp/fastest-way-to-read-text-files)