如何在c#中读取多部分流的http响应

时间:2017-01-10 15:51:03

标签: c# visual-studio-2013 get .net-4.0 httpresponse

我需要使用c#从http服务器接收响应。响应是一个多部分流;标题将包含

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

回应的主体类似于以下内容:

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

我需要收到回复并持续保存这些信息

List<UserData>

,其中包含具有用户信息的UserData类列表。

Http响应网址就像

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

我写了以下代码,但没有结果,请帮忙:

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

1 个答案:

答案 0 :(得分:0)

我之前使用过它,效果很好。 StreamingMultipartFormDataParser

  

Http Multipart Parser完全按照它声称的那样做:解析multipart / form-data。这个特殊的解析器非常适合从流中解析大数据,因为它不会立即读取整个流并为文件数据生成一组流。