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

时间:2013-03-27 13:59:41

标签: c# silverlight windows-phone-7 windows-phone-8 webrequest

我遇到的问题是之前的应用程序无法使用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。

任何可能的原因?

1 个答案:

答案 0 :(得分:0)

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

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

对于WP8: Referer标头的值以格式file:/// Applications / Install // Install /。

引用应用程序的安装目录。

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

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