流式Api消费

时间:2018-02-18 09:39:16

标签: c# restsharp dotnet-httpclient

我有一个api http://oversea-download.hikvision.com/uploadfile/Leaflet/ISAPI/HIKVISION%20ISAPI_2.0-IPMD%20Service.pdf我想收到哪个使用HTTP get和want连接保持活着。

连接后,它会发送我想要反序列化的XML响应。我已经创建了一个表示数据结构的类,但是我不确定如何在发回数据时不断更新列表或类似内容,例如响应(来自wireshark)如下所示:

wireshark

我不关心数据的解析和更新列表,更多的是我如何继续监听套接字并看到它的XML数据并且需要序列化..而不将流处理成缓冲区和寻找分隔符?

到目前为止,我一直没有成功玩的代码看起来像:

LoadError (cannot load such file -- bcrypt):

app/models/user.rb:3:in `<class:User>'
app/models/user.rb:1:in `<top (required)>'
app/controllers/users_controller.rb:4:in `new'
Started GET "/signup" for 127.0.0.1 at 2018-02-18 15:03:08 +0530
Processing by UsersController#new as HTML
You don't have bcrypt installed in your application. Please add it to your Gemfile and run bundle install
Completed 500 Internal Server Error in 22ms (ActiveRecord: 0.0ms)



LoadError (cannot load such file -- bcrypt):

app/models/user.rb:3:in `<class:User>'
app/models/user.rb:1:in `<top (required)>'
app/controllers/users_controller.rb:4:in `new'

和班级:

#gem 'bcrypt-ruby', '3.1.5', :require => 'bcrypt'
gem 'bcrypt', '~> 3.1.7'

1 个答案:

答案 0 :(得分:1)

无视我的评论,无法对其进行格式化......这种作品......但鉴于我获得了边界和内容类型的文字,我在...中得到了原始处理...

        var messageBuffer = string.Empty;

        var request = WebRequest.Create("http://192.168.0.152:80/ISAPI/Event/notification/alertStream");
        request.Credentials = new NetworkCredential("admin", "ThePassword");
        request.BeginGetResponse(ar =>
        {
            var req = (WebRequest)ar.AsyncState;
            // TODO: Add exception handling: EndGetResponse could throw
            using (var response = req.EndGetResponse(ar))
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                // This loop goes as long as the api is streaming
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();

                    if (line == xmlEndStr)
                    {
                        messageBuffer += line;
                        GotMessage(messageBuffer);
                        messageBuffer = string.Empty;
                    }
                    else if (line.StartsWith("<"))
                    {
                        messageBuffer += line;
                    }
                }
            }
        }, request);

    static void GotMessage(string msg)
    {

        var mySerializer = new XmlSerializer(typeof(EventNotificationAlert));
        var stringReader = new StringReader(msg);

        var eventAlert = (EventNotificationAlert)mySerializer.Deserialize(stringReader);

        Console.WriteLine($"DateTime: {eventAlert.dateTime} Channel: {eventAlert.channelID} Type: {eventAlert.eventType} Description: {eventAlert.eventDescription}");
    }

很高兴听到更好的方法(因为你可以说我对c#不太好!)