Manish Jain
Manish Jain

Reputation: 1353

How to read http response of multi-part stream in c#

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

Upvotes: 1

Views: 2492

Answers (1)

Rick S
Rick S

Reputation: 6586

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.

Upvotes: 1

Related Questions