Windows Phone 7 (WP7) Development Network Operations (2) httpwebrequest GET Request queue

Source: Internet
Author: User

In daily development, we often encounter the following requirement: request a network data and initiate the next operation request based on the obtained results. An example: automatically search for the most popular mobile phone product and search for this keyword. First, you need to request Baidu to search the rankings, obtain the list, extract the first keyword, and then start a new Baidu search, obtain and display search results.

  Before winfromThis development operation is actually very simple. You only need to execute it in sequence. The pseudocode is as follows:

VaR Results = request ("http://top.baidu.com/rss_xml.php? P = shoujichanpin ");
VaR firstkeyword = analysis (result );
VaR searchresult = request (string. Format (@ "http://www.baidu.com/s? WD = {0} ", firstkeyword ));
Display (searchresult );

In winform, because it is a synchronous operation, the thread is blocked, and the second request must start after the first request is complete and the result is analyzed. If the WP7 application is written in this way, it will not work. The main reason is that WP7 does not provide such synchronous network operations, all of which are asynchronous.

  Solution:

Do you still remember the queue in Data Structure middle school? In this case, we need to use a queue. Simply put, the queue is first-in-first-out and executed in order. At this time, we need to write these request operations into the queue and then process them from start to end. The main code is as follows:

public class OperateQueue
{
public OperateQueue()
{
_http = new Http(Handle);
}

readonly Http _http;
readonly Queue<BaseOperate> _queue = new Queue<BaseOperate>();
public delegate void HandleResult(string result);
private HandleResult _allOverHandle;


public Queue<BaseOperate> Queue
{
get { return _queue; }
}


public HandleResult AllOverHandle
{
get { return _allOverHandle; }
set { _allOverHandle = value; }
}
public void AddTask(BaseOperate operate)
{
Queue.Enqueue(operate);
}
public void StartTask(string querystring)
{
var newOperate = _queue.Peek();
newOperate.Start(_http, querystring);
}


private void Handle(string result)
{
var head = _queue.Dequeue();
var res = head.End(result);
if (_queue.Count>0)
{
var newOperate = _queue.Peek();
newOperate.Start(_http, res);
}
else
{
_allOverHandle(res);
}
}
}
 public class BaseOperate
{
public virtual void Start(Http http, string queryString)
{

}

public virtual string End(string result)
{
return "";
}
}

Analysis: After the queue executes the starttask () method, it will execute operations in the queue in sequence. After each operation is completed, it will call the handle () of the queue, and the handle is responsible for parsing the operation, then, determine whether there are any remaining operations in the queue. If yes, continue. Otherwise, return the result to the caller.

 

Then, we need to rewrite the baseoperate class based on actual functional requirements. For the above requirement analysis, we need two subclasses: operate_getkeyword, operate_search:

Public class operate_getkeyword: baseoperate
{
Public override void start (HTTP, string querystring)
{
VaR url = string. Format (@ "http://top.baidu.com/rss_xml.php? P = {0} ", querystring );
HTTP. startrequest (URL );
}

Public override string end (string result)
{
// Parse the result and directly return the result for ease of demo:
Return "iphone4s ";
}
}
    public class Operate_Search : BaseOperate
{
public override void Start(Http http, string queryString)
{
var url = string.Format(@"http://www.baidu.com/s?wd={0}", queryString);
http.StartRequest(url);
}

public override string End(string result)
{
return result;
}
}

For ease of demo, all operation parameters and Results Use the string type. You can also write a structure class for complex tasks.

 

Finally, it becomes easier to use the main interface:

        private void button1_Click(object sender, RoutedEventArgs e)
{
var queue = new OperateQueue();
queue.AddTask(new Operate_GetKeyword());
queue.AddTask(new Operate_Search());
queue.AllOverHandle = handle;
queue.StartTask("shoujichanpin");
}

private void handle(string res)
{
Dispatcher.BeginInvoke(() => txtView.Text = res);
}

Source code
Http://vdisk.weibo.com/s/3mNxq

 

 

Reprinted please indicate the source:

Windows Phone 7 (WP7) Development Network Operations (2) httpwebrequest GET Request queue

Jin Yanyun

Http://www.cnblogs.com/vistach/archive/2012/03/21/Windows_Phone_WP7_Net_Http_HttpWebRequest_Get_Queue.html

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.