HttpWebRequest排队库,可确保请求传递 [英] HttpWebRequest queueing library, which guarantees request delivery

查看:203
本文介绍了HttpWebRequest排队库,可确保请求传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道是否存在框架类,该类可以处理请求排队.

Just wondering, if there's existed class of framework, which would deal with request queuing.

发送请求时,应将其添加到队列中.如果出现问题(没有互联网),则应存储请求,然后尝试稍后再次发送.

When request is sent, is should be added to the queue. If something is wrong (no internet), request should be stored, and tried to be sent again later.

同时,可以创建更多请求.如果仍然没有网络,则所有新请求都应存储在队列中,并在网络返回时再次发送.

Meanwhile, more requests can be created. If there's still no network, all new requests should be stored in the queue, and sent again while network would return.

要实现相同的功能,想知道它是否已经实现.

Going to make same functional, wondering if it is already implemented.

我正在使用HttpWebRequest向服务器发布/从服务器获取json数据.

I'm using HttpWebRequest for Posting/Getting json data to/from server.

示例案例:

  1. 用户按Button1.发送第一个请求,将其添加到队列中(可使用互联网),应用程序已应答,请求已从队列中删除.

  1. User press Button1. First request is sent, added to queue, (internet is avaivable), app got answer, request is removed from the queue.

用户按Button2.发送第二个请求,将其添加到队列中,没有Internet连接->请求存储在队列中.

User press Button2. Second request is sent, added to queue, no internet connection -> request is stored in the queue.

用户按下Button3.第三个请求将被发送,但是由于队列不为空,因此仅将其存储到队列中. Request2尝试传递,但仍然没有连接.

User press Button3. Third request is going to be send, but as queue is not empty, it is just stored to the queue. Request2 tries to be delivered, but still no connection.

一段时间后,再次建立连接,因此Request2成功,从队列中删除,然后Request3也成功并从队列中删除.

Some time later, connection is established again, so Request2 succeeded, removed from the queue, then Request3 also succeeded and removed from the queue.

重要的是-它应该耐墓碑.如果电话在呼叫请求之前进入睡眠状态,则应将其存储起来.如果请求正在进行中,则应将其取消(或完全不应该存储此消息).

What is important - it should be resistant to tombstoning. If phone goes to sleep before calling request, it should be stored. Is request is in progress, it should be cancelled (or exactly this message should not be stored).

为避免传递异常,我正在使用此包装器:

For avoiding delivery exceptions, i'm using this wrapper:

public async Task<string> GetAsync(string url)
    {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.Method = HttpMethod.Get;
        httpWebRequest.Accept = "application/json";

        try
        {
            var response = (HttpWebResponse)await httpWebRequest.GetResponseAsync();

            if (response == null || response.StatusCode != HttpStatusCode.OK)
                return String.Empty;

            string data;

            using (var responseStream = response.GetResponseStream())
            {
                using (var postStreamReader = new StreamReader(responseStream))
                {
                    data = await postStreamReader.ReadToEndAsync();
                    postStreamReader.Close();
                }
                responseStream.Close();
            }

            return data ?? String.Empty;
        }
        catch (Exception ex)
        {
            return String.Empty;
        }
    }

如果有任何异常,则返回String.Empty,否则,返回服务器答案的字符串.我现在需要的是一种模式,该模式可以确保呼叫成功(结果不为空),否则,将其保持在队列中(以保持顺序),然​​后尝试再次引发呼叫,直到将其发送出去为止.

It returns String.Empty if there's any exceptions, otherwise, it returns server answer's string. What i need now is a pattern, which would guarantee that call was successful (result is not empty), otherwise, keep it in queue (to keep order) and would try to raise call again, until it would be delivered.

就目前而言,我正在考虑在信号量中添加一个队列,并在需要强制推送时调用它.有任何想法是否可以正常工作?

for now, i'm considering having a queue in a semaphore, and call it when i need to force pushing. Any thoughts if it would work fine?

推荐答案

我举了一个简单的示例,该示例使用Queue对象保存请求的URL列表.基本上,它只是一个接一个地发出请求.当请求完成时,总是发送Completed事件.然后由Completed事件的处理程序处理返回的页面或返回的错误情况.

I made simple example which uses Queue object to hold list of requested URLs. Basically it just makes requests one by one. And always sends a Completed event when the request is finished. It is then up to Completed event's handler to handle page that is returned or error conditions that are returned.

此外,还有墓碑支持.基本上,当调用Application_Deactivated或Application_Closing回调时,它将队列保存到隔离存储中.如果应用程序从逻辑删除阶段返回,则将其加载到应用程序构造函数/Application_Activated函数中.希望这可以帮助您入门.

Also, there's tombstoning support. Basically it saves the queue to isolated storage when Application_Deactivated or Application_Closing callbacks are called. And loads it in the app constructor/Application_Activated function if app returns from tombstoned stage. I hope this gets you started.

http://www.mediafire.com/download/cncuwx1kttehv8y/PhoneApp15.zip

这篇关于HttpWebRequest排队库,可确保请求传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆