1

I need to receive response from a http server using c#.The response is a multi-part stream; the header would contain

Content-Type: multipart/x-mixed-replace; boundary=userdata

The body of response would be similar to below:

--userdata
Content-type: text/plain
Content-length: <length-of-content>

UserId: <user-Id>
ParentId: <parent-Id>
ParentName: <parent-name>
Time: <time>

--userdata
Content-type: text/plain
Content-length: <length-of-content>

UserId: <user-Id>
ParentId: <parent-Id>
ParentName: <parent-name>
Time: <time>

I need to recieve the response and save these information continuously in

List<UserData>

which contains the list of UserData class having information of user.

Http response url is like

http://username:password@userIp:port-number/transactionStream

I have written following code but no result, Please help:

NetworkCredential networkCredential = new NetworkCredential(this.UserName, this.Password);

string requestingURL = "http://" + this.UserName + ":" + this.Password + "@" +this.UserIp+ ":" + this.PortNo + "/transactionDataStream";

Uri uri = new Uri(requestingURL);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);    
request.Credentials = networkCredential;
HttpWebResponse Answer = (HttpWebResponse)request.GetResponse();
Stream aStream = Answer.GetResponseStream();
StreamReader aStreamReader = new StreamReader(aStream);

string response = aStreamReader.ReadToEnd();
Manish Jain
  • 1,197
  • 1
  • 11
  • 32
  • Maybe I've missed something but what is generating that response? Have you made an API or is it something else? In my experience, payload responses containing this type of information is sent as JSON or XML - that way you can parse/work with the data you are receiving but you've not really said what you're sending back. – Ian Murray Jan 10 '17 at 16:17

1 Answers1

1

I've used this before and it works well. StreamingMultipartFormDataParser

The Http Multipart Parser does it exactly what it claims on the tin: parses multipart/form-data. This particular parser is well suited to parsing large data from streams as it doesn't attempt to read the entire stream at once and produces a set of streams for file data.

Rick S
  • 6,476
  • 5
  • 29
  • 43