【问题标题】:How to read http response of multi-part stream in c#如何在c#中读取多部分流的http响应
【发布时间】:2017-01-10 15:51:03
【问题描述】:

我需要使用 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 响应 url 是这样的

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

【问题讨论】:

  • 也许我错过了一些东西,但是是什么产生了这种反应?您是否制作了 API 或其他内容?根据我的经验,包含此类信息的有效负载响应以 JSON 或 XML 的形式发送 - 这样您就可以解析/处理您收到的数据,但您并没有真正说出您要发回的内容。

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


【解决方案1】:

我以前用过这个,效果很好。 StreamingMultipartFormDataParser

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

【讨论】:

  • 这个项目不兼容,不在我的VS2013中运行
  • 这仅与.net framework 4.5兼容,但我使用的是framework 4.0,请帮忙
猜你喜欢
  • 2010-09-13
  • 2023-01-01
  • 2017-09-21
  • 1970-01-01
  • 2018-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
相关资源
最近更新 更多