当我使用
时 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
我收到一条错误消息,指出
'System.Net.HttpWebRequest' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'System.Net.HttpWebRequest' could be found (are you missing a using directive or an assembly reference?
我添加了以下参考资料,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using PhoneApp23.Resources;
using System.IO;
using System.Text;
using System.Threading;
我错过了什么吗? 或者我应该怎么做才能使这项工作正常进行!
答案 0 :(得分:6)
在Windows手机中,您需要执行每次可能需要50ms异步的操作。由于webrequest可能需要更长时间,因此microsoft从类中删除了sync方法。相反,您需要使用异步方法:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri("http://www.google.com"));
request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
private void ReadWebRequestCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
using(HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult))
{
using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
{
string results = httpwebStreamReader.ReadToEnd();
//execute UI stuff on UI thread.
Dispatcher.BeginInvoke(() => TextBlockResults.Text = results);
}
}
}