HttpWebRequest BeginGetResponse回调未在WP8上触发(在WP7上工作)

我遇到的问题是以前的应用程序没有使用WP8,它在WP7上运行得很好。

这是我用于http请求的代码:

public void SendMessage() { request = WebRequest.Create(uri) as HttpWebRequest; request.Method = "POST"; request.AllowReadStreamBuffering = true; request.ContentType = "application/octet-stream"; try { // get device info String deviceInfo = String.Format("platform,{0};os,{1};width,{2};height,{3};dpi,{4};", Config.PLATFORM_NAME, Environment.OSVersion.Version.ToString(), System.Windows.Application.Current.Host.Content.ActualWidth.ToString(), System.Windows.Application.Current.Host.Content.ActualHeight.ToString(), 96); request.Headers["X_MX_DEVICE_INFO"] = deviceInfo; } catch (Exception) {} request.BeginGetRequestStream(new AsyncCallback(ProcessRequestStream), null); } private void ProcessRequestStream(IAsyncResult asyncResult) { if (!message.IsCancelled()) { try { using (Stream stream = request.EndGetRequestStream(asyncResult)) { message.GetRequest(stream); } request.BeginGetResponse(new AsyncCallback(ProcessResponseStream), null); } catch (Exception e) { syncContext.Post(OnEnd, e); } } else { syncContext.Post(OnEnd, null); } } private void ProcessResponseStream(IAsyncResult asyncResult) { if (!message.IsCancelled()) { try { response = (HttpWebResponse)request.EndGetResponse(asyncResult); if (HttpStatusCode.OK != response.StatusCode) { throw new Exception("http status error: " + response.ToString()); } syncContext.Post(SetResponse, response); } catch (Exception e) { syncContext.Post(OnEnd, e); } } else { syncContext.Post(OnEnd, null); } } private void SetResponse(object state) { Exception ex = null; try { using (Stream stream = ((HttpWebResponse)state).GetResponseStream()) { message.SetRespone(stream); } } catch (Exception e) { ex = e; } syncContext.Post(OnEnd, ex); } private void OnEnd(object state) { message.OnEnd((Exception)state); } } 

看起来好像没有触发BeginGetResponse的回调。 我已经尝试过Fiddler来看看会有什么样的反应回来,看起来好像什么都没有回来,但只是为了WP8。

任何可能的原因?

我相信这可能与Windows Phone 8上的Referer问题有关。

对于WP7.1:默认情况下,Referer标头的值为null。 您可以为Referer标头指定自定义值。

对于WP8:Referer标头的值以文件格式引用应用程序的安装目录:/// Applications / Install // Install /。

互联网上有一些博客文章,可能的解决方案:

但在你的情况下,我仍然强烈建议分析Fiddler日志。 确保已下载并安装了Fiddler4 。 此外,请确保首先启动fiddler,然后启动WP模拟器。