首页新闻找找看学习计划

WindowsPhone调用Rest异步处理如何获取返回值

0
悬赏园豆:50 [待解决问题]

直接上代码:

View Code
 1 public class BaseAuthenicationHttpClient     {
2
3 public static string doRequest(string url, string username, string password)
4 {
5 string rtnStr = String.Empty;
6
7 //ManualResetEvent allDone = new ManualResetEvent(false);
8 try
9 {
10 //注意这里的格式哦,为 "username:password"
11 string usernamePassword = username + ":" + password;
12
13 RestClient client = new RestClient(url);
14
15 //client.Authenticator = new HttpBasicAuthenticator(username, password);
16
17 client.AddDefaultHeader("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(usernamePassword)));
18 client.AddDefaultHeader("User-Agent", "Mozilla/5.0");
19
20 // Http Request的设置
21 RestRequest request = new RestRequest(Method.POST);
22
23 client.ExecuteAsync(request, (response) =>
24 {
25 if (response.ResponseStatus == ResponseStatus.Error)
26 {
27 throw new ITException();
28 }
29 else if (response.ResponseStatus == ResponseStatus.Completed)
30 {
31 if (response.Content != null)
32 {
33 rtnStr = response.Content;
34 }
35 }
36 // 设置 ManualResetEvent,以便主线程可以退出
37 //allDone.Set();
38 });
39
40 // 将 ManualResetEvent 设置为 Wait,
41 // 以便在调用回调前,应用程序不退出
42 //allDone.WaitOne();
43 }
44 catch (IOException e)
45 {
46 throw new ITException("", e);
47 }
48
49 return rtnStr;
50 }
51 }

以上如何等待client.ExecuteAsync异步处理后,返回rtnStr

绿豆眼睛的主页 绿豆眼睛 | 初学一级 | 园豆:152
提问于:2012-03-29 19:47
< >
分享
所有回答(3)
0
dudu | 园豆:39136 (高人七级) | 2012-03-30 18:05
0

private void DoWebClient() { WebClient webClient = new WebClient(); webClient.OpenReadAsync(new Uri("http://www.cnblogs.com/linzheng"));//在不阻止调用线程的情况下,从资源返回数据 webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);//异步操作完成时发生 } void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { using (StreamReader reader = new StreamReader(e.Result)) { string contents = reader.ReadToEnd(); int begin = contents.ToString().IndexOf("<title>"); int end = contents.ToString().IndexOf("</title>"); string note = contents.Substring(contents.ToString().IndexOf("摘要"), 300); webClientTextBlock.Text = contents.ToString().Substring(begin+7, end - begin-7); textBox1.Text = note; } } private void DoHttpWebRequest() { string url = "http://www.cnblogs.com/linzheng"; WebRequest request = HttpWebRequest.Create(url);//创建WebRequest类 IAsyncResult result = (IAsyncResult)request.BeginGetResponse(ResponseCallback, request);//返回异步操作的状态 } private void ResponseCallback(IAsyncResult result) { HttpWebRequest request = (HttpWebRequest)result.AsyncState;//获取异步操作返回的的信息 WebResponse response = request.EndGetResponse(result);//结束对 Internet 资源的异步请求 using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { string contents = reader.ReadToEnd(); int begin = contents.ToString().IndexOf("<title>"); int end = contents.ToString().IndexOf("</title>"); string note = contents.Substring(contents.ToString().IndexOf("摘要"), 300); //通过呼叫UI Thread来改变页面的显示 Dispatcher.BeginInvoke(() => { httpWebRequestTextBlock.Text = contents.ToString().Substring(begin + 7, end - begin - 7); textBox2.Text = note; }); } }


----------

http://www.cnblogs.com/linzheng/archive/2011/03/02/1969282.html

http://www.cnblogs.com/linzheng/archive/2011/03/05/1971485.html
这是林政老师写的两篇关于网络编程之异步请求 希望对你有所帮助

MicroLeo | 园豆:202 (菜鸟二级) | 2012-04-02 06:57
0

楼主找到解决方案了吗,我也正苦恼这个问题

feni | 园豆:202 (菜鸟二级) | 2012-04-21 15:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册