我需要使用 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>
其中包含具有用户信息的用户数据类的列表。
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();
我以前用过这个,效果很好。 StreamingMultipartFormDataParser
Http Multipart Parser 完全按照它在 tin上声明的方式去做:解析 multipart/form-data。这个特定的解析器非常适合从流中解析大型数据,因为它不会尝试一次读取整个流,而是为文件数据生成一组流。