我有以下代码:
string url = "http://www.google.com"; HttpWebRequest client= WebRequest.CreateHttp(new Uri(url)) as HttpWebRequest; client.AllowReadStreamBuffering = true; // Call and handle the response. client.BeginGetResponse( (asResult) => { Dispatcher.BeginInvoke( () => { try { var response = client.EndGetResponse(asResult); System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream()); string responseString = reader.ReadToEnd(); } catch (WebException failure) { throw failure; } }); },null );
执行总是在catch部分结束.但是,看过Fiddler2后,google.com似乎没有任何流量.所以似乎没有提出要求.
我在Retrieve XML from https using WebClient/HttpWebRequest – WP7看到过类似的问题,但我使用的是标准端口,所以不确定这是否相关.我也尝试按照帖子简化代码,但没有成功.
有趣的是,最可能的选择似乎是因为我可能没有按照HttpWebRequest Breaks On WP7在AppManifestWM.xaml文件中定义网络功能,但我的AppManifestWM.xaml文件似乎已定义:
<Deployment xmlns="http://schemas.microsoft.com/windowsphone/2009/deployment" AppPlatformVersion="7.0"> <App xmlns="" ProductID="{ac5b5d62-573c-4134-b290-0ad4f678ad7f}" Title="xxx.WindowsPhone7.Client" RuntimeType="Silverlight" Version="1.0.0.0" Genre="apps.normal" Author="xxx.WindowsPhone7.Client author" Description="Sample description" Publisher="xxx.WindowsPhone7.Client publisher"> <IconPath IsRelative="true" IsResource="false">ApplicationIcon.png</IconPath> <Capabilities> <Capability Name="ID_CAP_NETWORKING" /> <Capability Name="ID_CAP_LOCATION" /> <Capability Name="ID_CAP_SENSORS" /> <Capability Name="ID_CAP_MICROPHONE" /> <Capability Name="ID_CAP_MEDIALIB" /> <Capability Name="ID_CAP_GAMERSERVICES" /> <Capability Name="ID_CAP_PHONEDIALER" /> <Capability Name="ID_CAP_PUSH_NOTIFICATION" /> <Capability Name="ID_CAP_WEBBROWSERCOMPONENT" /> </Capabilities> <Tasks> <DefaultTask Name ="_default" NavigationPage="MainPage.xaml"/> </Tasks> <Tokens> <PrimaryToken TokenID="xxx.WindowsPhone7.ClientToken" TaskName="_default"> <TemplateType5> <BackgroundImageURI IsRelative="true" IsResource="false">Background.png</BackgroundImageURI> <Count>0</Count> <Title>xxx.WindowsPhone7.Client</Title> </TemplateType5> </PrimaryToken> </Tokens> </App> </Deployment>
所以我很茫然.该请求实际上似乎并没有发生,导致我认为某些事情正在阻止它.
更新:
没有任何改变,但认为这个堆栈跟踪可能heko:
System.Net.WebException was unhandled
Message=The remote server returned an
error: NotFound. StackTrace:
at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult
asyncResult)
at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult
asyncResult)
at xxx.WindowsPhone7.Client.Views.AddHuddleUserPage.<>c__DisplayClass2.<>c__DisplayClass4.b__1()
at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo
rtmi,Object obj,BindingFlags
invokeAttr,Binder binder,Object
parameters,CultureInfo culture,
Boolean isBinderDefault,Assembly
caller,Boolean verifyAccess,
StackCrawlMark& stackMark)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object
obj,BindingFlags invokeAttr,Binder
binder,Object[] parameters,
CultureInfo culture,StackCrawlMark&
stackMark)
at System.Reflection.MethodBase.Invoke(Object
obj,Object[] parameters)
at System.Delegate.DynamicInvokeOne(Object[]
args)
at System.MulticastDelegate.DynamicInvokeImpl(Object[]
args)
at System.Delegate.DynamicInvoke(Object[]
args)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority
priority)
at System.Windows.Threading.Dispatcher.OnInvoke(Object
context)
at System.Windows.Hosting.CallbackCookie.Invoke(Object[]
args)
at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[]
args)
at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr
pHandle,Int32 nParamCount,
ScriptParam[] pParams,ScriptParam&
pResult)
状态为System.Net.WebExceptionStatus.UnknownError
谢谢你的时间.
对我来说,原因完全不同:
WMAppManifest.xml刚刚缺少ID_CAP_NETWORKING!