I'm using C# and via a webapi I need to send a request to a server and process the response. The data that comes back from the server has the form like:
{"A_Field":a,"B_Field":"B_FieldValue","C_Field":5}
{"TYPE":"Values","Column1":["1234","456789","12345"],"Column2":["abc","defg","high"]}
{"TYPE":"Values","Column1":["45353","23422","6755"],"Column2":["sfs","fhfghg","vzczzc"]}
{"TYPE":"Values","Column1":["4564","5664","12345"],"Column2":["abfsdc","vxvx","cxvbvx"]}
{"TYPE":"Values","Column1":["5664","979","7978"],"Column2":["ryrt","qweq","nbmm"]}
....
{"A_Field":b,"COMPLETE:"YES"}
I need to process the parts that start with {"TYPE":"Values"....} There are an exceptionally large amount of these messages that are returned from the server. So to be able to process each line/message as it is received would be ideal. The last line that has "COMPLETE:"YES"} signals the end of the message. I tried the below code:
try{
using (var handler = new HttpClientHandler())
{
handler.Credentials = new System.Net.NetworkCredential("user", "password");
using (var client = new HttpClient(handler))
{
client.BaseAddress = new Uri("http://<server>:port/api/rest/");
await client.GetAsync(blah_query)
.ContinueWith(taskwithresponse =>
HttpResponseMessage response = taskwithresponse.Result;
}
}
}
catch(Exception e)
{
Console.WriteLine("Error {0}",e.Message);
}
The output gives: Error: One or more errors occurred
This fails when the response is very large. I'm guessing that processing has to be done in the loop? But I believe what is happening here is that it's waiting for the entire message to return. Or is this the completely wrong way to go about it.
I'm not familiar with web programming. But can someone help advise on how I can process the returned data in an optimal way? Is it possible to do it a line/message at a time? A code sample/tutorial would be a great help. Most of what i've seen uses a small amount of data.
Aucun commentaire:
Enregistrer un commentaire